Chapter 4: Programming in Matlab - College of the Redwoods
Chapter 4: Programming in Matlab - College of the Redwoods
Chapter 4: Programming in Matlab - College of the Redwoods
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
312 <strong>Chapter</strong> 4 <strong>Programm<strong>in</strong>g</strong> <strong>in</strong> <strong>Matlab</strong><br />
>> for k=A, k, end<br />
k =<br />
1<br />
3<br />
k =<br />
2<br />
4<br />
Note that k is assigned a column <strong>of</strong> matrix A at each iteration <strong>of</strong> <strong>the</strong> loop.<br />
This column assignment does not contradict <strong>the</strong> use we have already <strong>in</strong>vestigated,<br />
i.e., for k=start:<strong>in</strong>crement:f<strong>in</strong>ish. In this case, <strong>the</strong> <strong>Matlab</strong> construct<br />
start:<strong>in</strong>crement:f<strong>in</strong>ish creates a row vector and k is assigned a new column <strong>of</strong><br />
<strong>the</strong> row vector at each iteration. Of course, this means that k is assigned a new<br />
element <strong>of</strong> <strong>the</strong> row vector at each iteration.<br />
As an example <strong>of</strong> use, let’s plot <strong>the</strong> family <strong>of</strong> functions def<strong>in</strong>ed by <strong>the</strong> equation<br />
y = 2Cte −t2 , (4.2)<br />
where C is one <strong>of</strong> a number <strong>of</strong> specific constants. The follow<strong>in</strong>g code produces<br />
<strong>the</strong> family <strong>of</strong> curves shown <strong>in</strong> Figure 4.6. At each iteration <strong>of</strong> <strong>the</strong> loop, C is<br />
assigned <strong>the</strong> next value <strong>in</strong> <strong>the</strong> row vector [-5,-3,-1,0,1,3,5].<br />
t=l<strong>in</strong>space(-4,4,200);<br />
for C=[-5,-3,-1,0,1,3,5]<br />
y=-2*C*t.*exp(-t.^2);<br />
l<strong>in</strong>e(t,y)<br />
end<br />
Break and Cont<strong>in</strong>ue<br />
There are two sitautions that frequently occur when writ<strong>in</strong>g loops.<br />
1. If a certa<strong>in</strong> state is achieved, <strong>the</strong> programmer wishes to term<strong>in</strong>ate <strong>the</strong> loop<br />
and pass control to <strong>the</strong> code that follows <strong>the</strong> end <strong>of</strong> <strong>the</strong> loop.<br />
2. The programmer doesn’t want to exit <strong>the</strong> loop, but does want to pass control<br />
to <strong>the</strong> next iteration <strong>of</strong> <strong>the</strong> loop, <strong>the</strong>reby skipp<strong>in</strong>g any rema<strong>in</strong><strong>in</strong>g code that<br />
rema<strong>in</strong>s <strong>in</strong> <strong>the</strong> loop.<br />
As an example <strong>of</strong> <strong>the</strong> first situation, consider <strong>the</strong> follow<strong>in</strong>g snippet <strong>of</strong> code.<br />
<strong>Matlab</strong>’s break command will cause <strong>the</strong> loop to term<strong>in</strong>ate if a prime is found <strong>in</strong><br />
<strong>the</strong> loop <strong>in</strong>dex k.