This example contains a very simple script and
a equally simple subprogram (matlab function)
which demonstrate the modular construction of 
more complex functions.  In order to run the 
program you need to split it apart and save the
two bits to separate files.  The script can be
called anything you want, so long as it has the
.m suffix required for the computer to recognize
that it is a matlab program.  The function part
must be saved under the name retire.m and both 
parts must reside in the same directory that you
launched matlab from.


cut here

_______________________________

clear
echo on
%This example demonstrates the use of
%a simple subprogram to calculate the
%retirement account balance as a function
%of the savings rate.  The savings rate
%is specified by x in %.  The company is
%assumed to match the employees contribution,
%thus the overall savings rate is 2 x.

%First we set up the array of values x
%will take on:

x=[0:.2:15];

%The bit in the middle simply says that
%x will go from 0 to 15 by steps of 0.2
pause

%Next we call the subprogram through a
%function call:

r=retire(x);

pause

%Finally, we plot the function:

plot(x,r)
xlabel('Savings Rate')
ylabel('Return')
grid

pause

%From the plot we see that the desired
%savings balance of $1M is achieved with
%a savings rate of about 12.2%.  A more
%sophisticated program would explore
%the effect of different interest, raise,
%or inflation rates.

echo off


____________________________

function r=retire(x)
%This function calculates the retirement
%fund account in current dollars for a
%fixed initial salary, for a fixed annual
%raise, and for a fixed inflation rate as
%a function of the fraction saved.  It
%assumes that you work for 40 years.

s=40000;
r=2*x*40000/100;
for i=2:40
  r=r*(1+0.07)*(1-0.03)+s*2*x/100;
  s=s*(1+0.04)*(1-0.03);
end;

%This function assumed a 7% annual yield
%on the retirement fund, a 3% inflation
%rate and a 4% annual raise.  The employer
%contribution to the retirement plan was
%assumed to match the employee's contribution.