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.

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

Execute <strong>the</strong> cell and <strong>the</strong>n state what happens and why.<br />

4. Let’s try an approach different to <strong>the</strong> attempt <strong>in</strong> Exercise 3. Open <strong>the</strong><br />

editor, enter <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es, and save <strong>the</strong> file as simpleInterest2.m.<br />

function I=simpleInterest2<br />

global P R T<br />

I=P*R*T;<br />

Add <strong>the</strong> follow<strong>in</strong>g cell to VariableScope.m.<br />

%% Exercise #4<br />

clc<br />

clear all<br />

global P R T<br />

P=1000; R=0.06; T=10;<br />

I=simpleInterest2;<br />

fpr<strong>in</strong>tf(’Simple <strong>in</strong>terest ga<strong>in</strong>ed is %.2f.\n’,I)<br />

Execute <strong>the</strong> cell and state <strong>the</strong> result<strong>in</strong>g output. Expla<strong>in</strong> <strong>the</strong> difference between<br />

<strong>the</strong> result <strong>in</strong> Exercise 3 and <strong>the</strong> current exercise.<br />

5. Enter <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es <strong>in</strong> <strong>the</strong> editor and save <strong>the</strong> file as clearFunction.<br />

function clearFunction<br />

clear all<br />

Add <strong>the</strong> follow<strong>in</strong>g cell to VariableScope.m.<br />

%% Exercise #5<br />

clc<br />

clear all<br />

P=1000; R=0.06; T=10;<br />

clearFunction;<br />

whos

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

Saved successfully!

Ooh no, something went wrong!