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.
Section 4.2 Control Structures <strong>in</strong> <strong>Matlab</strong> 329<br />
primes=2;<br />
for m=3:100<br />
if any(mod(m,primes)==0)<br />
cont<strong>in</strong>ue<br />
else<br />
primes=[primes,m];<br />
end<br />
end<br />
We create a format <strong>of</strong> 5 fields, each <strong>of</strong> width 5, <strong>the</strong>n fpr<strong>in</strong>tf <strong>the</strong> vector primes<br />
us<strong>in</strong>g this format.<br />
fmt=’%5d %5d %5d %5d %5d\n’;<br />
The result<strong>in</strong>g list <strong>of</strong> primes is nicely formatted <strong>in</strong> 5 columns.<br />
2 3 5 7 11<br />
13 17 19 23 29<br />
31 37 41 43 47<br />
53 59 61 67 71<br />
73 79 83 89 97<br />
11. We open a figure w<strong>in</strong>dow and set a lighter background color <strong>of</strong> gray.<br />
fig=figure;<br />
set(fig,’Color’,[0.95,0.95,0.95])<br />
We draw a circle <strong>of</strong> radius one, <strong>in</strong>crease its l<strong>in</strong>e width, change its color to<br />
dark red, set <strong>the</strong> axis equal, <strong>the</strong>n turn <strong>the</strong> axes <strong>of</strong>f.<br />
t=l<strong>in</strong>space(0,2*pi,200);<br />
x=cos(t);<br />
y=s<strong>in</strong>(t);<br />
l<strong>in</strong>e(x,y,’L<strong>in</strong>eWidth’,2,’Color’,[0.625,0,0])<br />
axis equal<br />
axis <strong>of</strong>f