%A quick introduction to MATLAB.
%The command window (>>>) let's us run small snippets of code. Everything we
%do, we can do in the command window, but it doesn't store any memory. It's
%useful for quick calculations or testing code, but everything we will
%write and save will be in the "script window". This can be saved,
%modified, and run over and over again. Let's give that a shot.
1 + 1;
a = 4; %Semicolon prevents output of value in the command window.
2*a %This will print.
%We can also define vectors in a similar way to variables.
b = [3,4,7,1,0];
%We can index the b vector with parenthesis. Let's look at entry 3, 2, and
%1 in three separate commands.
b(3); %Remove the semicolon to show the output.
b(2);
b(1);
%We can use MATLAB to simulate a random walk in one dimension. Our walker
%will start at zero and at each time step, the walker will flip a coin to
%decide where to go -- to the left or to the right. To do this simulation,
%we have to assign values to a few parameters. How many coinflips are we
%going to do?
NSteps = 1000; %Number of coin flips we will do.
%How do we do the coinflip? We can use MATLABs random number generator to
%generate a random number between zero and one. Let's generate a random
%number.
RandomNumber = rand;
%Let's say that the coinflip was a "heads" if the probability was > 0.5.
%It's a tails otherwise. We can use MATLABs "if" statements to check if a
%condition is true.
if a > 3
display('Hello') %Will only print 'Hello' if a > 3.
end
%Let's apply this to our coinflip.
if RandomNumber < 0.5
Step = -1;
else
Step = 1;
end %This is necessary to tell MATLAB to end the logic measurement.
%We need to do this many, many times however (1000 times)!. We can either
%1) Write this code 1000 times or 2)Write a "for loop" to do this for us.
%For loops are incredibly useful to perform repetitive tasks. Let's do an
%example
for i=1:10
i; %This will print i for each iteration between 1 and 10 through the loop.
end
%Let's apply this to our random walk.
%We need to keep track of where we are as well! It's pointless to just
%flip the coin without knowing where we were in the previous flip. We'll
%define the initial position of our walker.
Position=0;
for i=1:NSteps
%Perform the coinflip
RandomNumber = rand;
%Now we'll decide if that flip was heads or tails.
if RandomNumber <0.5
Step = -1;
else
Step = 1;
end
%Add our current step to our position.
Position = Position + Step;
end
%What's the position?
Position;
%Note that the position changes everytime we run it! This shows that the
%walk is random. All we have done is calculate the position for one walk.
%What would the mean position be if we ran this many times? We can make
%another vector of our positions after each walk and then calculate the
%mean. We can do this with another for loop.
%Let's define the number of steps.
NSteps = 60;
%How many random walks should we do?
NWalks = 1000;
%We need to create an empty vector to which we will add our final position
%after each walk.
for j=1:NWalks
%Zero the position value. Each individual walk.
Position(j) = 0;
%Now we can do the coin flipp NStep times.
for i=1:NSteps
%Perform the coinflip
RandomNumber = rand;
%Now we'll decide if that flip was heads or tails.
if RandomNumber <0.5
Step = -1;
else
Step = 1;
end %Ends the coin flip decision
%Add our current step to our position.
Position(j) = Position(j) + Step;
end %End each individual random walk
end %End the simulation of many randomWalks.
%Now we have to calculate the mean position. MATLAB has a "mean" function,
%but we can also do that by hand. We'll do the latter first.
sum(Position) / NWalks; %By hand.
mean(Position); %Using the predefined MATLAB function.
%The mean is boring! How would be calculate the mean of the square
%position? We have to be a little careful with MATLABs syntax. In MATLAB,
%to multiply or divide each element in an array, you have to precede the
%function with a dot ".". To do element operations, you simply omit the
%dot.
%This will calculate the mean square position ().
mean(Position.^2);