Chapter 14 Function Mfiles
14.1 MFile Construction Rules
There is another type of mFile besides a MATLAB script file. It is a MATLAB
function mFile.
Typically, a MATLAB function file contains a sequence of MATLAB
commands which process specific information passed to it in the form of input variables
and generate output based on the information.
A function mFile behaves like a black
box from the user's standpoint because all that is evident are the variables passed to it
from the MATLAB Workspace and those which are returned from it to the Workspace.
Intermediate variables created in the function mFile are not stored in the MATLAB
Workspace and hence will not overwrite any variables of the same name which happen to
be present in the Workspace.
Function mFiles are created with the MATLAB Editor/Debugger and saved as
text files with a .m extension similar to script files.
All MATLAB builtin mathematical
functions are function mFiles.
A simple function mFile called
floan.m
is shown in Example 14.1.1.
Its main
purpose is to calculate a mathematical function value based on values passed to it from a
script file called
Loan_Interest.m
and return the computed value to the calling
program. The calling script file
Loan_Interest.m
is included as well in order to see
where the function calls occur.
Example 14.1.1
function i_R = floan(AA,PP,NN,ii)
% floan is a function called by the script file 'Loan_Interest.m'
% It accepts a monthly payment AA, loan amount PP, a loan duration NN
% (in years) and an interest rate ii. It returns a single value
% determined from evaluation of the function. The script file
% 'Loan_Interest.m' is a root solving program which attempts to find
% the root iR of f(A,P,N,i)=0.
The user selects from one of two root
% solvers, the Bisection Method or the Method of False Position.
The
% root iR found in 'Loan_Interest.m' is the annualized interest (in
% percent).
N_months=12*NN;
% Convert from years to months
if ii~=0
% Check if ii not equal 0
x=1+ii;
i_R=AA/PP(ii*x^N_months)/(x^N_months1); % Calculate function
% value when ii not equal 0
else
%
ii equals 0
i_R=AA/PP  1/N_months % Calculate function value when ii equals 0
end % if ii~=0
% Check if ii not equal 0
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document% Loan_Interest.m
% A script file that calculates the iterest rate on a loan
% It calls function floan.m with inputs P,A,N and i
% P:Loan amount
% A:Monthly installment
% N:Duration of loan expressed in years
% i:An interest rate (decimal value)
% iR: Annual per cent interest rate on loan
%
clear,clc
echo off
P=35000;
% Loan amount
A=750;
% Monthly payment
N=5; % Loan duration (in years)
method='F';
iLinitial=0;
iUinitial=1;
eSTOP=0.001;
iL=iLinitial;
iU=iUinitial;
fiL=floan(A,P,N,iL);
fiU=floan(A,P,N,iU);
count=0;
i=0;
eA=eSTOP+1;
iold=i;
%%%%% Calculate next i and f(i) %%%%%
if method=='B'
i=(iL+iU)/2;
else
i=iU(fiU*(iLiU)/(fiLfiU));
end % if
fi=floan(A,P,N,i);
inew=i;
%%%%% Determine new endpoint and f(i) at new endpoint %%%%%
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '11
 klee

Click to edit the document details