15.11.2014 Views

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

SHOW MORE
SHOW LESS

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> 331<br />

first “gett<strong>in</strong>g” <strong>the</strong> XData for <strong>the</strong> dart with get(dart,’XData’), <strong>the</strong>n we append<br />

<strong>the</strong> current value <strong>of</strong> x with [get(dart,’XData’),x]. The set command is used<br />

to set <strong>the</strong> updated list <strong>of</strong> x-values. A similar task updates <strong>the</strong> dart’s y-values.<br />

At each iteration <strong>of</strong> <strong>the</strong> loop, we also <strong>in</strong>crement <strong>the</strong> hits counter if <strong>the</strong> dart lies<br />

with<strong>in</strong> <strong>the</strong> border <strong>of</strong> <strong>the</strong> circle, that is, if x 2 + y 2 < 1. You can uncomment <strong>the</strong><br />

drawnow command to animate <strong>the</strong> dart throw<strong>in</strong>g at <strong>the</strong> expense <strong>of</strong> wait<strong>in</strong>g for<br />

1000 darts to be thrown to <strong>the</strong> screen.<br />

F<strong>in</strong>ally, we output <strong>the</strong> requested data to <strong>the</strong> command w<strong>in</strong>dow.<br />

fpr<strong>in</strong>tf(’%d <strong>of</strong> %d darts fell <strong>in</strong>side <strong>the</strong> circle.\n’,hits,N)<br />

fpr<strong>in</strong>tf(’The ratio <strong>of</strong> hits to throws is %f\n\n’,hits/N)<br />

fpr<strong>in</strong>tf(’The ratio <strong>of</strong> <strong>the</strong> area <strong>of</strong> <strong>the</strong> circle to <strong>the</strong> area\n’)<br />

fpr<strong>in</strong>tf(’<strong>the</strong> area <strong>of</strong> <strong>the</strong> square is pi/4, or approximately %f.\n\n’,pi/4)<br />

rel_err=abs(hits/N-pi/4)/abs(pi/4);<br />

fpr<strong>in</strong>tf(’The relative error <strong>in</strong> approximat<strong>in</strong>g pi/4 wiht <strong>the</strong> ratio\n’)<br />

fpr<strong>in</strong>tf(’<strong>of</strong> hits to total darts thrown is %.2e.\n’,rel_err)<br />

15. We beg<strong>in</strong> by us<strong>in</strong>g logical relations to craft <strong>the</strong> piecewise function.<br />

N=500;<br />

x=l<strong>in</strong>space(-pi,pi,N);<br />

y=0*(x=0);<br />

l<strong>in</strong>e(x,y,...<br />

’L<strong>in</strong>eStyle’,’none’,...<br />

’Marker’,’.’)<br />

Next, we compose two anonymous functions for <strong>the</strong> coefficients a n and b n .<br />

a=@(n) (1-cos(n*pi))/(n^2*pi);<br />

b=@(n) 1/n;<br />

Note that a 0 = π/2, so a 0 /2 = π/4, which must be added to <strong>the</strong> sum <strong>of</strong> <strong>the</strong><br />

first five terms <strong>of</strong> <strong>the</strong> series. Thus, we’ll start our runn<strong>in</strong>g sum at s = π/4. The<br />

for loop <strong>the</strong>n sums <strong>the</strong> first five terms. We use ano<strong>the</strong>r l<strong>in</strong>e command to plot<br />

<strong>the</strong> result.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!