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
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
380 <strong>Chapter</strong> 4 <strong>Programm<strong>in</strong>g</strong> <strong>in</strong> <strong>Matlab</strong><br />
Call<strong>in</strong>g Functions From Script Files<br />
We are go<strong>in</strong>g to tackle a programmng project that will perform rational arithmetic<br />
for <strong>the</strong> user <strong>of</strong> <strong>the</strong> program. Options will <strong>in</strong>clude reduc<strong>in</strong>g, add<strong>in</strong>g, subtract<strong>in</strong>g,<br />
multiply<strong>in</strong>g, and divid<strong>in</strong>g fractions <strong>in</strong>put by <strong>the</strong> user <strong>of</strong> our program.<br />
Contemplat<strong>in</strong>g <strong>the</strong> design <strong>of</strong> such a program, here are some possible smaller<br />
tasks that need implmentation.<br />
1. Present <strong>the</strong> user with a menu <strong>of</strong> choices: reduction, addition, subtraction,<br />
multiplication, and division.<br />
2. Rout<strong>in</strong>es will need to be written for each possible user choice on <strong>the</strong> menu.<br />
3. The program will need to query <strong>the</strong> user for a menu choice, <strong>the</strong>n react <strong>in</strong> <strong>the</strong><br />
appropriate manner.<br />
Note that this design is prelim<strong>in</strong>ary. In <strong>the</strong> process <strong>of</strong> writ<strong>in</strong>g a complex<br />
program, difficulties with <strong>the</strong> orig<strong>in</strong>al design will surely arise. When that occurs,<br />
we will need to make modifications <strong>of</strong> <strong>the</strong> orig<strong>in</strong>al design, which <strong>in</strong> turn will create<br />
<strong>the</strong> need for design <strong>of</strong> new modules not considered <strong>in</strong> <strong>the</strong> first design.<br />
However, one clear place that we can start is on <strong>the</strong> design <strong>of</strong> a menu <strong>of</strong><br />
choices for <strong>the</strong> user <strong>of</strong> our program. Remember, <strong>the</strong> menu should query what<br />
action <strong>the</strong> user wants to perform, reduction, addition, subtraction, or division.<br />
With this thought <strong>in</strong> m<strong>in</strong>d, we write <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es and save <strong>the</strong> script as<br />
rationalArithmetic.m.<br />
clc<br />
fpr<strong>in</strong>tf(’Rational Arithmetic Menu:\n\n’)<br />
fpr<strong>in</strong>tf(’1). Reduce a fraction to lowest terms.\n’)<br />
fpr<strong>in</strong>tf(’2). Add two fractions.\n’)<br />
fpr<strong>in</strong>tf(’3). Subtract two fractions.\n’)<br />
fpr<strong>in</strong>tf(’4). Multiply two fractions.\n’)<br />
fpr<strong>in</strong>tf(’5). Divide two fractions.\n’)<br />
fpr<strong>in</strong>tf(’6). Exit program.\n\n’)<br />
n=<strong>in</strong>put(’Enter number <strong>of</strong> your choice: ’);<br />
Go to <strong>the</strong> <strong>Matlab</strong> prompt <strong>in</strong> <strong>the</strong> command w<strong>in</strong>dow and execute <strong>the</strong> script by<br />
typ<strong>in</strong>g <strong>the</strong> follow<strong>in</strong>g.<br />
>> rationalArithmetic