Function M-files - Chapter 14 Function M-files 14.1 M-File...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 14 Function M-files 14.1 M-File Construction Rules There is another type of m-File besides a MATLAB script file. It is a MATLAB function m-File. 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 m-File 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 m-File 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 m-Files are created with the MATLAB Editor/Debugger and saved as text files with a .m extension similar to script files. All MATLAB built-in mathematical functions are function m-Files. A simple function m-File 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_months-1); % 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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
% 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*(iL-iU)/(fiL-fiU)); end % if fi=floan(A,P,N,i); inew=i; %%%%% Determine new endpoint and f(i) at new endpoint %%%%%
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 14

Function M-files - Chapter 14 Function M-files 14.1 M-File...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online