MATLAB Functions - The University of Chicago Booth School of

MATLAB Functions
See Chapter 6 of Gilat
Kipp Martin
University of Chicago
Booth School of Business
February 1, 2012
1
MATLAB Files
Files used in this lecture:
I
mortgage.m
I
mortgageGlobal.m
I
mortgage2.m
I
callMortgage.m
I
persistentVar.m
2
Outline
Introduction: Function Basics
M-File Functions
Using Global Variables
Using Persistent Variables
Subfunctions
3
Introduction: Function Basics
KEY IDEA: You can define your own functions in MATLAB.
I
m-file functions
I
subfunctions
4
Introduction: Function Basics
I
Make use of help!
I
Make use of help!
I
Make use of help!
I
Make use of help!
I
Make use of help!
M-File Functions
An M-file function for calculating mortgage monthly payments.
function [mon_pay] = mortgage(int_rate, prin, years)
% mortgage calculates monthly mortgage payments
% Input Arguments:
% int_rate = annual interest rate
% prin = the amount of the loan
% years = lengh of mortgage in years
% Output Arguments:
% mon_pay = monthly mortgage payment
mon_int_rate = int_rate/12;
num_payments = years*12;
mon_pay = prin*mon_int_rate/(1 - ...
(1 + mon_int_rate)^(-num_payments));
M-File Functions
Function
Definition
Function
Output Argument(s)
Function
Input Argument
Return Argument
Calculation
Help Text
7
H1 Line
for "lookfor"
Using M-File Functions
You use m-file functions just like any other function.
Type the name of the function in at the command window.
>> mortgage(.06, 100000, 30)
ans =
599.5505
Functions call other functions (more on this later).
8
Using M-File Functions
Here is an m-file (callMort.m) that calls the mortgage function
clc
clear
int_rate = .05;
prin = 100000;
years = 30
payment = mortgage(int_rate, prin, years)
9
M-File Functions
Some Key Points:
I
The first line of the m-file must begin with the keyword
function
I
The return values that the function calculates are put inside [
and ] separated by commas. The return values must be
defined in the function body.
I
The function inputs are put inside ( and ) separated by
commas
I
The variables defined inside are local to the function. They
will not be defined in the command window
I
The m-file should have the same name as the function (not
required but a very good idea)
M-File Functions
Documentation: It is good practice to document your m-file
functions.
I
Comments start with a %
I
The first comment line after the name of the function is the
H1 line. It is what will appear in the lookfor search result.
I
The comment lines immediately after the H1 line appear as
the result of the help function name command.
I
It is good practice to define all of your inputs and results in
comments immediately after H1
11
M-File Functions
Type in:
>> lookfor mortgage
mortgage calculates monthly mortgage payments
mortgage2 calculates monthly mortgage payments
using a subfunction
>> help mortgage
mortgage calculates monthly mortgage payments
Input Arguments:
int_rate = annual interest rate
prin = the amount of the loan
years = lengh of mortgage in years
Output Arguments:
mon_pay = monthly mortgage payment
Function Basics – Variables
Key Idea: Variables may be either local or global.
I
Function input and output variables are local – i.e. we pass
by value, not reference
I
Variables defined and used in the function are local. Other
functions, script files, and the command window cannot use
them.
I
If you actual want global variables use the command:
global variable name
13
Function Basics – Naming
I
Fuction names can contain letters, digits, and the underscore
character (no other characters).
I
Function names must start with a letter.
I
Function names can have up to 63 characters in MATLAB 7
and 31 characters in MATLAB 6.
I
Function names are case sensitive
I
Do not create function names that conflict with built-in
function names, for example, do not define a variable sin.
Summary: The same naming conventions apply to both variables
and functions.
M-File Functions
M-File Functions Versus Script Files (See Gilat, page 165 of Third
Edtion)
I
Both script files and m-files are saved with a .m extension
I
The first line of a function file must contain the function
keyword and the definition of the function.
I
All variables not explicitly defined as global in an m-file
function are treated as local.
I
All variables defined in a script file are available in the
command window after executing the script.
I
A script file can use variables previously defined in the
command window.
15
Using Global Variables
If you love risky behavior, and absolutely want to use global
variables here is an illustration.
Modify the morgage function to mortgageGlobal where the
interest rate is now a global variable.
function [mon_pay] = mortgageGlobal(prin, years)
% mortgage calculates monthly mortgage payments
% Input Arguments:
% prin = the amount of the loan
% years = lengh of mortgage in years
% Output Arguments:
% mon_pay = monthly mortgage payment
global gint_rate;
mon_int_rate = gint_rate/12;
num_payments = years*12;
mon_pay = prin*mon_int_rate/(1 - ...
(1 + mon_int_rate)^(-num_payments));
Using Global Variables
Here is the m-file (callMort.m) that does the calling
clc
clear
global gint_rate
int_rate = .05;
gint_rate = .05;
prin = 100000;
years = 30
payment = mortgage(int_rate, prin, years)
payment = mortgageGlobal(prin, years)
Using Global Variables
Concept: Understand the difference between declaring a variable
in an m-file and in a function.
In the first case we are global. In the second we are not.
18
Using Persistent Variables
MATLAB has the equivalent of VBA static variables.
In MATLAB they are called persistent variables.
Key Concept: the lifetime of the persistent variable is over the
lifetime of having MATLAB open.
function persistentVar(inputvalue)
%http://www.mathworks.com/help/techdoc/matlab_prog/f0-38052
persistent SUM_X
if isempty(SUM_X)
SUM_X = 0;
end
SUM_X = SUM_X + inputvalue
19
Using Persistent Variables
Summary: Make sure you understand:
I
Pass by reference
I
Pass by value
I
Variable scope
I
Variable lifetime
Subfunctions
More than one function can be in a single m-file.
function [mon_pay] = mortgage2(int_rate, prin, years)
% mortgage2 calculates monthly mortgage payments
% using a subfunction
% Input Arguments:
% int_rate = annual interest rate
% prin = the amount of the loan
% years = lengh of mortgage in years
% Output Arguments:
% mon_pay = monthly mortgage payment
%
mon_int_rate = int_rate/12;
num_payments = years*12;
% call the function denom() to calculate mon_pay
mon_pay = prin*mon_int_rate/denom(mon_int_rate, ...
num_payments);
Subfunctions
A continuation of the previous file which is saved as mortgage2.m
% define a subfunction
function [denom_val] = denom(mon_int_rate, num_payments)
% denom calculate the denomenator in the mortgage function
% Input Arguments:
% mon_int_rate = the monthly interest rate
% num_payments = the total number of payments
% Output Arguments:
% denom_val = the denominator required in the
% mortgage calculation
%
denom_val = (1 - (1 + mon_int_rate)^(-num_payments));
Subfunctions
Key Points:
I
The variables in each of the functions do not know about each
other, they are all local
I
Other functions cannot access the subfunction directly.
I
lookfor will NOT return the H1 line for the subfunction
I
To get help for the function type in
help function>subfunction
Subfunctions
>> lookfor denom
denom not found.
>> help mortgage2>denom
denom calculate the denomenator in the
mortgage function
Input Arguments:
mon_int_rate = the monthly interest rate
num_payments = the total number of payments
Output Arguments:
denom_val = the denominator required in the
mortgage calculation