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.
Section 4.5 Subfunctions <strong>in</strong> <strong>Matlab</strong> 393<br />
4.5 Exercises<br />
1. Complete <strong>the</strong> program rationalArithmetic. Implement four subfunctions,<br />
rationalAdd, rationalSubtract, rationalMultiply and rationalDivide, <strong>the</strong>n<br />
add code to <strong>the</strong> correspond<strong>in</strong>g case structure that will prompt <strong>the</strong> user for <strong>in</strong>put,<br />
call <strong>the</strong> appropriate subfunction, <strong>the</strong>n format <strong>the</strong> output. Test your function<br />
thoroughly.<br />
2. Although <strong>Matlab</strong> handles <strong>the</strong> arithmetic <strong>of</strong> complex numbers quite nicely,<br />
<strong>in</strong> this exercise you will write your own function to perform complex arithmetic.<br />
First, a bit <strong>of</strong> review. Recall that i = √ −1 and i 2 = −1. With <strong>the</strong>se facts,<br />
coupled with <strong>the</strong> knowledge that <strong>the</strong> usual laws <strong>of</strong> arithmetic apply equally well<br />
to <strong>the</strong> complex numbers (commutative, associative, distributive, etc.), it is not<br />
difficulty to show each <strong>of</strong> <strong>the</strong> follow<strong>in</strong>g results.<br />
i.)<br />
ii.)<br />
(a + bi) + (c + di) = (a + c) + (b + d)i<br />
(a + bi) − (c + di) = (a + c) − (b + d)i<br />
iii.) (a + bi)(c + di) = (ac − bd) − (ad + bc)i<br />
iv.) a + bi<br />
c + di<br />
=<br />
(ac + bd) + (bc − ad)i<br />
c 2 + d 2<br />
It is <strong>in</strong>structive to provide a pro<strong>of</strong> <strong>of</strong> each <strong>of</strong> <strong>the</strong>se results. Here is your task. Name<br />
your primary function complexArithmetic, <strong>the</strong>n perform each <strong>of</strong> <strong>the</strong> follow<strong>in</strong>g<br />
tasks.<br />
a.)<br />
b.)<br />
c.)<br />
Write a subfunction complexMenu that will <strong>of</strong>fer <strong>the</strong> user a choice <strong>of</strong> four<br />
operations, addition, subtraction, multiplication, division, and exit<strong>in</strong>g <strong>the</strong><br />
program. This rout<strong>in</strong>e should ask <strong>the</strong> user for a numerical response and<br />
return <strong>the</strong> choice to <strong>the</strong> call<strong>in</strong>g function <strong>in</strong> <strong>the</strong> output variable n.<br />
The primary function should have a case structure that responds to <strong>the</strong> user’s<br />
choice <strong>in</strong> n. Each item <strong>in</strong> <strong>the</strong> case structure should prompt <strong>the</strong> user to enter<br />
two complex numbers as row vectors, i.e., as [a,b] and [c,d], represent<strong>in</strong>g<br />
a+bi and c+di, respectively. This <strong>in</strong>put should be sent as row vectors to <strong>the</strong><br />
appropriate subfunction, <strong>the</strong>n <strong>the</strong> returned row vector should be formatted<br />
and pr<strong>in</strong>ted to <strong>the</strong> screen with fpr<strong>in</strong>tf <strong>in</strong> a manner you deem fitt<strong>in</strong>g.<br />
Write four subfunctions, complexAdd, complexSubtract, complexMultiply,<br />
and complexDivide. Each subfunction should accept two row vectors as<br />
13 Copyrighted material. See: http://msenux.redwoods.edu/IntAlgText/