................................................................................................. MATLAB BASICS (de Zirbel) http://www-math.bgsu.edu/~zirbel/ ................................................................................................. 1: 2*9 Matlab can be used as a calculator. 2: sin(1) 3: 2^999 4: x=sin(1) You can store values with variable names. 5: x This displays the value of x. 6: x=rand(10,1) x is a collection of 10 random numbers, uniformly distributed between 0 and 1. x is like a column of cells in a spreadsheet. It is called a vector or a 10 by 1 matrix. 7: x+5 Add 5 to each entry of x. 8: 1000*x Multiply each entry of x by 100. 9: x=rand(10,7) A 10 by 7 matrix of random numbers. To repeat this, or any other command, hit the up arrow until you see the previous command that you wish to repeat. 10: x=rand(1000,1) Go ahead, try something larger than 1000. 11: x=rand(1000,1); Use a semicolon to suppress the output. 12: help Get a list of standard help topics. 13: help elmat Help about elementary matrices, including rand. 14: help rand Help specific to rand. 15: x(1:20,1) Pull out the first 20 rows of x. 16: help punct Help on the use of punctuation symbols in Matlab. 17: max(x) 18: mean(x) 19: sum(x) 20: median(x) 21: cumsum(x) A vector of cumulative sums of entries of x. 22: y=sort(rand(10,1)) Sort another 10 random numbers. 23: hist(x) Make a histogram of the values of x. 24: hist(x,30) Use 30 bins instead of the default 10. 25: y=-log(x) Take the natural logarithm of the entries of x. 26: hist(y,30) Most of the values of y are near 0, but some are as large as 6. The numbers in y have what is called the exponential distribution. 27: z=randn(1000,1); Generate 1000 normally distributed numbers. 28: hist(z,30) The histogram is bell-shaped. You might want to try this with more than 1000 numbers. ................................................................................................. Plotting data points 29: plot(x(1:10),'*'); Plot the first 10 data points. 30: plot(x-0.5); Plot all data points, connected by lines. 31: hold on Add subsequent plots to this one. 32: plot(cumsum(x-0.5),'r'); Add the running sum to the plot. 33: zoom on Click on the graph to zoom in; double click to return to the original scale. 34: clf Clear the current gure. 35: z=randn(1000,1); Generate 1000 normally distributed numbers. 36: w=z+randn(1000,1); Generate numbers dependent on z. 37: plot(z,w,'*'); Make a scatterplot of these pairs. 38: axis([-3 3 -4 4]); Display x values from -3 to 3, y from -4 to 4. Graphing functions 39: clf 40: ezplot('sin(x)',[0 3*pi]); Plot the sine function. 41: hold on 42: t=0:0.01:3*pi; Define a vector of time points, spaced 0.01. 43: t t is a row vector. 44: t=t' t is now a column vector. 45: plot(t,sin(5*t),'r'); Plot sin(t) against t in red. Apparently you cannot use ezplot to plot functions in color. 46: title('sin(x) and sin(5x)') Add a better title to the plot. ................................................................................................. Coin flipping 47: 3<5 The answer is 1 because the inequality is satisfied. 48: 5<3 The answer is 0. 49: x=rand(20,1) You typed a command similar to this a while ago. Type x= and then the up arrow to bring it back, then edit it to change 1000 to 20. 50: x>0.5 Check the inequality for each entry of x. 51: z=1+(x>0.5) z will equal 1 or 2. This is like flpping coins, with 1 for heads and 2 for tails. 52: show(z,'HT') Use a program called show, which needs two arguments, a vector of integers greater than 0 and a string to tell what letters to use to display each number. 53: show(1+(rand(1500,1)>0.5),'HT') Generate 1500 coin flips. Now press the up arrow and return, repeatedly. That's a lot of coin flips! What is the longest run of heads that you can find? 54: show(1+(rand(1500,1)>0.5),'O-') You can make that question easier to answer with different characters. Use the up arrow so you can simply edit the previous command. This works well enough for coin flipping, in which there are only two outcomes. But for other random experiments like rolling a die, a program called rando.m is more useful. 55: d=[1 1 1 1 1 1]/6 A row vector (1 by 6 matrix) of probabilities for the outcomes of rolling a die. 56: sum(d) Make sure these sum to 1! 57: rando(d) Use these probabilities to simulate rolls of a die. Use the up arrow to repeat this command several times. 58: vrando([1 1 1 1]/4,20) A vector version of rando. Each number has equal probability of occuring. 59: show(vrando([1 1 1 1]/4,100),'BGSU') Generate B, G, S, and U randomly. Does BGSU occur before BUGS? ................................................................................................. Writing a Matlab program You are going to create a new Matlab program called mywalk.m, which will simulate a 100-step random walk. At the top of the command window is a blank white sheet; click on that to open a new editor window. In that window, type the commands below on separate lines, then save the program as mywalk.m by clicking on the disk icon. 60: n = 100; The number of steps to take. 61: x = rand(n,1); A vector of uniformly distributed numbers. 62: y = 2*(x > 0.5) - 1; Convert these to -1 and +1. 63: z = cumsum(y); The running sum of the y values. 64: clf 65: plot(z) Plot values of z against 1, 2, 3, ... Back in the command window, type mywalk to run the program, then the up arrow to run it again and again. If there is an error, check to see that you have typed everything correctly. 66: After you run this a few times, you might want to be able to generate longer strings of letters all at once. A good way to do that is to make BGSU markov.m into a Matlab function, so that you can call it with a parameter. To do this, follow the next two steps: 67: Replace the line n = 100; from your program with: function [z] = mywalk(n) Then mywalk is a function which needs a parameter n (the length of the sequence to generate) and returns a vector z. For a function, the variables it introduces, like y, are internal to the function unless they are explicitly passed back when the function is done. Just like the functions sin and rand, the function mywalk gives back a value, the vector z. When you go back to the command window to run it, type this: 68: mywalk(1000) Run the program mywalk with parameter 1000. Matrices 69: M=rand(6,6) A 6 by 6 matrix of random numbers. 70: M(2,:) Pull out the second row of the matrix. 71: M(:,4) Pull out the fourth column of the matrix. 72: diag(M) Pull out the diagonal entries of the matrix. 73: sum(M) Sum the columns of the matrix. 74: sum(M')' Sum the rows of the matrix. The apostrophe denotes transpose. ................................................................................................. Markov chains In line 59, the letters in the sequence were independent. We should be able to set up a situation in which a B is usually followed by a G but never by a U. After the letter B, we could use the vector [0.2 0.6 0.2 0] to choose the next letter. After the letter G, we could use a different vector, etc. To do this, we'll create a new program called BGSU markov.m. Open a new editor window and type the following commands. Then type BGSU markov in the command window. 75: P=[[0.2 0.6 0.2 0]; [0 0.2 0.6 0.2]; [0.2 0 0.2 0.6]; [0.6 0.2 0 0.2]]; P is a 4 by 4 matrix. Each row tells the probabilities we are going to use for the next letter. Row 1 tells what probabilities to use after the number 1 (corresponding to the letter B), row 2 tells what probabilities to use after the number 2 (for G), and so on. 76: X(1) = rando([1 1 1 1]/4); Choose the first state randomly. 77: for i=1:399, 78: X(i+1) = rando(P(X(i),:)); This is clever: P(X(i),:) is row X(i) of the matrix P, whatever the value of X(i) happens to be at the time. The probabilities in this row are then given to rando to generate the next state. 79: end 80: show(X,'BGSU'); 81: hist(X,4);