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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Section 4.5 Subfunctions <strong>in</strong> <strong>Matlab</strong> 387<br />

second step <strong>of</strong> our hand calculation above. If <strong>the</strong> rema<strong>in</strong>der b is zero, <strong>the</strong>n a<br />

is <strong>the</strong> GCD, which we assign to <strong>the</strong> output variable d, <strong>the</strong>n issue a return.<br />

3. Iterate until done.<br />

Now that we have a function that will f<strong>in</strong>d <strong>the</strong> greatest common divisor, let’s<br />

craft a function that will reduce a fraction to lowest terms.<br />

Reduc<strong>in</strong>g to Lowest Terms. Our function will take as <strong>in</strong>put <strong>the</strong> numerator<br />

and denom<strong>in</strong>ator <strong>of</strong> <strong>the</strong> rational number. It will <strong>the</strong>n call rationalGCD to f<strong>in</strong>d<br />

<strong>the</strong> greatest common divisor <strong>of</strong> <strong>the</strong> numerator and denom<strong>in</strong>ator. It will <strong>the</strong>n<br />

divide both numerator and denom<strong>in</strong>ator by <strong>the</strong> greatest common divisor and<br />

output <strong>the</strong> results. Open <strong>the</strong> <strong>Matlab</strong> editor, enter <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es, <strong>the</strong>n save<br />

<strong>the</strong> file as rationalReduce.m.<br />

function [num,den]=rationalReduce(num,den)<br />

d=rationalGCD(num,den);<br />

num=num/d;<br />

den=den/d;<br />

Let’s test this function on <strong>the</strong> fraction 336/60, which we know reduces to 28/5.<br />

>> [num,den]=rationalReduce(336,60)<br />

num =<br />

28<br />

den =<br />

5<br />

This is <strong>the</strong> correct response. The call<strong>in</strong>g script <strong>of</strong> function will need to take<br />

responsibility for formatt<strong>in</strong>g <strong>the</strong> output.<br />

Add<strong>in</strong>g Subfunctions to Primary Function. We’ve tested rationalGCD<br />

and rationalReduce and found <strong>the</strong>m to produce <strong>the</strong> correct results. We will<br />

now add each <strong>of</strong> <strong>the</strong>se functions as subfunctions to our exist<strong>in</strong>g primary function<br />

rationalArithmetic. Open <strong>the</strong> file rationalArithmetic and add <strong>the</strong> follow<strong>in</strong>g<br />

l<strong>in</strong>es at <strong>the</strong> very bottom <strong>of</strong> <strong>the</strong> file rationalArithmetic.m.<br />

function [num,den]=rationalReduce(num,den)<br />

d=rationalGCD(num,den);<br />

num=num/d;<br />

den=den/d;

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

Saved successfully!

Ooh no, something went wrong!