Difference between revisions of "Matlab Loops"
m (add some comments and a link) |
|
(One intermediate revision by the same user not shown) | |
(No difference)
|
Latest revision as of 15:02, 16 November 2013
Contents
Motivating Example
Suppose you wanted to create a program to see how often a given number turns up in Craps. If we roll two dice, the smallest number that can arise is 2, and the largest is 12. While there are more clever ways of doing this using probability, we take a brute-force method. Let's choose two random numbers between 1 and 6 and do this over again until we achieve the number we are betting on.
bet = 4; % the number we are looking for (between 2 and 12)
dice = round( rand(2,1)*5+1 ); % two numbers between 1 and 6
if ( sum(dice) != bet )
% now what???
end
|
The problem is that we want to keep rolling the dice until we get our number (4 in this case). How do we do this? We use loops. There are two kinds of loops: for loops and while loops.
For Loops
A for loop is executed a set number of times. The basic syntax of a for loop is:
for counter = lo:inc:hi
% do some work
end
counter | A variable that ranges from lo to hi |
lo | Indicates the starting point for the counter |
hi | Indicates the stopping point for the counter |
inc | Indicates the increment for the counter. You can increment forward, backward, and in any size increment. |
Each time through the loop, the counter variable will increment by inc. The loop exits when the counter exceeds hi. The following table shows several simple examples of a for loop.
Matlab Code | Details | Output | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
for i=5:8
disp(i);
end
|
|
5 6 7 8 | |||||||||||||||
x = 1:7;
for i=2:2:length(x)
x(i) = 2*x(i);
end
disp(x);
|
|
| |||||||||||||||
x = 3:7;
for i=length(x):-2:1
fprintf('x(%1.0f) = %1.0f\n',i,x(i));
end
|
|
x(5) = 7 x(3) = 5 x(1) = 3 |
Factorial Example
Let's consider another example: calculate the factorial of a number using a for loop. The factorial is given as
We could implement this using a for loop as shown in the following:
Matlab Code | Results at the end of each pass through the for loop | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
n = 7; % we want to find n!
nfact=1; % starting value.
for i=n:-1:2
nfact = nfact * i;
end
|
|
Projectile Motion Example
Suppose that we want to examine the effect of the angle on the path of a projectile. The equations for projectile motion (neglecting air resistance) can be written as
Assuming that the ground is level and that we launch from the point , the projectile will hit the ground again when . From the above equation for , we conclude that this happens at and .
Let's create a Matlab script that will plot the trajectory of a projectile over the time of flight until it reaches the ground again. Here is an algorithm:
- Set the initial speed,
- Set a vector of angles, , that we want to consider.
- For Each Angle:
- Calculate and .
- Calculate the time of flight,
- Calculate the and positions over the time of flight and add these to the plot.
- Label the plot.
This algorithm may be implemented in Matlab as follows:
clear; clc; close all; vo = 100; % initial velocity, m/s a = -9.8; % acceleration, m/s^2 angle = 5:10:85; % angles in degrees angle = angle*pi/180; % convert to radians figure; hold on; % create a figure and hold it for multiple lines. % loop over each angle and create the plot for its trajectory. for i=1:length(angle) vox = vo*cos( angle(i) ); % initial x-velocity for this angle voy = vo*sin( angle(i) ); % initial y-velocity for this angle tf = -2*voy/a; % time of flight for this angle t = linspace(0,tf); % a vector of time points for our plot. x = vox * t; % x-position at each point in time. y = voy * t + 0.5*a*t.^2; % y-position at each point in time. plot( x, y ); % plot this trajectory. % annotate the plot by adding a label to the line at the peak of the % trajectory indicating the angle that this line corresponds to. % find the index for the max height. There may be more than one if % we had a point on either side of the maximum. In this case, % choose the first one. maxpt = find( y==max(y) ); if( length(maxpt)>1 ) maxpt=maxpt(1); end; text( x(maxpt), y(maxpt), num2str(angle(i)*180/pi) ); end grid on; axis tight; xlabel('x (m)'); ylabel('y (m)'); title('Projectile Trajectories at Various Angles');
See notes on plotting and building arrays for explanations of those topics used in the above example.
This code produces the following plot:
Exponential Decay Example
Compounds undergoing [http://en.wikipedia.org/wiki/Exponential_decay exponential decay] obey the following equation, which describes the concentration, as a function of time:
where is the time constant. This is often used in carbon dating. The half life is defined as the time required for the concentration to decrease by half. Using the previous equation, and substituting we find
Use Matlab to create a plot of the concentration as a function of time for various values of . Indicate the half life on the plot. Assume that the initial concentration is (arbitrary units).
An algorithm may be:
- Define a vector for
- Calculate the half life from the above equation for each .
- Determine how long in time to plot. Let's go twice the longest half life.
- For Each value of ,
- Calculate as a function of .
- Plot as a function of .
- Put a point indicating the half life on the plot.
- Label the plot
Below is the matlab code to implement the above algorithm. If you have questions about plotting, see the tutorial on plotting in matlab.
clear; clc; close all; k = [0.1 0.2 0.5 1]; % set the rate constants thalf = -log(0.5)./k; % determine half life for each rate constant t = linspace(0,max(thalf*2)); % set the time over which we will calculate c co = 10.2; % set the initial concentration figure; hold on; % loop over each rate constant. Plot its concentration % profile as well as the half-life time. for i=1:length(k); c = co*exp(-k(i)*t); plot(t,c); % plot the c vs time line plot(thalf(i),co/2,'rs'); % plot the point where we reach the half-life end plot(t,co/2*ones(length(t)),'r-'); % plot the half-life horizontal line % label the plot xlabel('time'); ylabel('Concentration'); title('Concentration vs. Time for Various Rate Contsants'); grid on; axis tight;
The results of this are shown in the following figure:
While Loops
Unlike a for loop (which is executed a set number of times), the while loop continues to execute until some condition is met. The basic syntax of a while loop is:
while condition
% do some work. Note that "condition" must change inside the loop!
end
|
Factorial Example
Here is an example of how to calculate the factorial of a number using a while loop.
Matlab Code | Results at the end of each pass through the while loop | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
n = 7; % we want to find n!
nfact=1; % starting value.
while n>1
nfact = nfact*n
n = n-1
end
|
|
Craps Example
Here is another example of rolling two dice. We keep rolling the dice until we get them to add up to 7. We print out how many rolls it took.
clear; clc;
luckyNum = 7; % our lucky number.
lucky = 0; % 0 is "false" - we will use this to determine when we hit our lucky number.
nrolls = 0; % keep track of how many times we roll the dice.
while ~lucky
% pick 2 random integers between 1 and 6. The "round" function rounds to the nearest integer.
dice = round( rand(2,1)*5+1 );
nrolls = nrolls+1; % increment the counter for how many rolls we have done.
lucky = sum(dice)==luckyNum; % decide if we rolled a lucky 7 or not...
end
fprintf('Rolled a lucky %1.0f after %1.0f rolls\n',luckyNum,nrolls);
|
Here we have used the ~ operator, which is the logical "NOT" operator.