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
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Section 4.4 Variable Scope <strong>in</strong> <strong>Matlab</strong> 373<br />
%% Exercise #2<br />
clc<br />
I=P*r*t;<br />
fpr<strong>in</strong>tf(’Simple <strong>in</strong>terest ga<strong>in</strong>ed is %.2f.\n’,I)<br />
State <strong>the</strong> output <strong>of</strong> <strong>the</strong> script. Use whos to exam<strong>in</strong>e <strong>the</strong> base workspace<br />
variables. Anyth<strong>in</strong>g new? Expla<strong>in</strong>.<br />
3. Clear <strong>the</strong> workspace by enter<strong>in</strong>g <strong>the</strong> command clear all at <strong>the</strong> <strong>Matlab</strong> prompt<br />
<strong>in</strong> <strong>the</strong> command w<strong>in</strong>dow. Type whos to exam<strong>in</strong>e <strong>the</strong> base workspace and <strong>in</strong>sure<br />
that it is empty. At <strong>the</strong> command prompt, enter and execute <strong>the</strong> follow<strong>in</strong>g command.<br />
>> P=1000; r=0.06; t=10;<br />
Exam<strong>in</strong>e <strong>the</strong> workspace:<br />
>> whos<br />
Name Size Bytes Class<br />
P 1x1 8 double array<br />
r 1x1 8 double array<br />
t 1x1 8 double array<br />
Open <strong>the</strong> editor, enter <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es, and save <strong>the</strong> function M-file as<br />
simpleInterest1.m.<br />
function I=simpleInterest1<br />
I=P*r*t;<br />
fpr<strong>in</strong>tf(’Simple <strong>in</strong>terest ga<strong>in</strong>ed is %.2f.\n’,I)<br />
Add <strong>the</strong> follow<strong>in</strong>g cell to VariableScope.m.<br />
%% Exercise #3<br />
clc<br />
I=simpleInterest1;<br />
fpr<strong>in</strong>tf(’Simple <strong>in</strong>terest ga<strong>in</strong>ed is %.2f.\n’,I)