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.

390 <strong>Chapter</strong> 4 <strong>Programm<strong>in</strong>g</strong> <strong>in</strong> <strong>Matlab</strong><br />

A few comments are <strong>in</strong> order.<br />

1. The fpr<strong>in</strong>tf commands are self explanatory.<br />

2. The heart <strong>of</strong> this code is <strong>the</strong> call to <strong>the</strong> rationalReduce subfunction, which<br />

takes <strong>the</strong> user’s <strong>in</strong>put for <strong>the</strong> numerator and denom<strong>in</strong>ator, <strong>the</strong>n returns <strong>the</strong><br />

reduced form <strong>of</strong> <strong>the</strong> numerator and denom<strong>in</strong>ator. The ensu<strong>in</strong>g fpr<strong>in</strong>tf commands<br />

format <strong>the</strong> result.<br />

3. The pause command is new, but simply expla<strong>in</strong>ed. It halts program execution<br />

until <strong>the</strong> user strikes a key.<br />

It should now be a simple matter to complete menu items #4 and #5. You<br />

need only multiply two rational numbers as follows,<br />

a<br />

b · c<br />

d = a · c<br />

b · d ,<br />

<strong>the</strong>n reduce <strong>the</strong> result. For <strong>the</strong> division, you need only <strong>in</strong>vert and multiply,<br />

<strong>the</strong>n reduce <strong>the</strong> result.<br />

a<br />

b ÷ c d = a b · d<br />

c = a · d<br />

b · c ,<br />

To accomplish <strong>the</strong>se tasks <strong>in</strong> code requires two subfunctions, rationalMultiply<br />

and rationalDivide. You will also have to write code that requests <strong>in</strong>put, performs<br />

<strong>the</strong> requested operation, <strong>the</strong>n formats <strong>the</strong> output after case 4 and case<br />

5.<br />

Addition and subtraction are a bit trickier.<br />

F<strong>in</strong>d<strong>in</strong>g a Least Common Denom<strong>in</strong>ator. To add and subtract fractions,<br />

<strong>the</strong> program will need to f<strong>in</strong>d a least common denom<strong>in</strong>ator (LCD). For example,<br />

to add 5/12 and 3/8, we make equivalent fractions with a commond denom<strong>in</strong>ator,<br />

<strong>the</strong>n add.<br />

5<br />

12 + 3 8 = 10<br />

24 + 9 24 = 19<br />

24<br />

Additionally, you must <strong>in</strong>sure that <strong>the</strong> answer is reduced to lowest terms.<br />

So, how do we write a subfunction that will produce a least common denom<strong>in</strong>ator?<br />

The follow<strong>in</strong>g fact from number <strong>the</strong>ory reveals <strong>the</strong> result.<br />

Thus,<br />

lcd(a, b) =<br />

ab<br />

gcd(a, b)<br />

lcd(8, 12) = 8 · 12<br />

gcd(8, 12) = 96<br />

4 = 24.

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

Saved successfully!

Ooh no, something went wrong!