0% found this document useful (0 votes)
63 views

User-Defined Functions in Matlab: Jake Blanchard University of Wisconsin - Madison Spring 2008

This document discusses user-defined functions in Matlab. It explains that Matlab allows users to create their own functions, which take in inputs and return outputs. It provides examples of how to define functions using the function keyword and save them as m-files. It also discusses how to call user-defined functions, the Matlab path for finding functions, using inline and anonymous functions, adding parameters, and debugging scripts.

Uploaded by

vasantha_btech
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

User-Defined Functions in Matlab: Jake Blanchard University of Wisconsin - Madison Spring 2008

This document discusses user-defined functions in Matlab. It explains that Matlab allows users to create their own functions, which take in inputs and return outputs. It provides examples of how to define functions using the function keyword and save them as m-files. It also discusses how to call user-defined functions, the Matlab path for finding functions, using inline and anonymous functions, adding parameters, and debugging scripts.

Uploaded by

vasantha_btech
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

User-Defined Functions in Matlab

Jake Blanchard
University of Wisconsin - Madison
Spring 2008
Matlab Functions
Matlab permits us to create our own
functions
These are scripts that take in certain
inputs and return a value or set of values
We will need these as we use built-in
functions for problem solving
Format of Function Declaration
function [output arguments]
=function_name(input arguments)
User-Defined Functions
Suppose we want to plot:
sin(3*x)+sin(3.1*x)
Create user-defined function
function r=f(x)
r=sin(3*x)+sin(3.1*x)
Save as f.m
User-Defined Functions (cont)
Now just call it:
x=0:0.1:50;
y=f(x);
plot(x,y)
The Matlab Path
Matlab looks in the current path for
functions (m-files)
The path is shown near the top of the
command window
Practice
Create an m-file that calculates the function
g(x)=cos(x)+cos(1.1*x)
Use it to plot g(x) from x=0 to 100
Note: previous function was
function r=f(x)
r=sin(3*x)+sin(3.1*x)
and plot commands were
x=0:0.1:50;
y=f(x);
plot(x,y)
Practice
Create an m-file that calculates the
function g(x, o)=cos(x)+cos((1+o)x) for a
given value of o
Use it to plot g(x, o) from x=0 to 100 for
o=0.1 and 0.2
Flow Control
if x<10 then
x=x+1
else
x=x^2
end
Flow Control (cont)
for i=1:10
z=z*i
end
Flow Control (cont)
A=0
sum=0
while A < 10,
sum=sum+A;
A=A+1;
end
Practice
On the next slide is a Matlab function that
calculates the sum of cubes of the first N
integers
Download sumofcubes.mand answer
the following questions:
What is the result for N=20?
Modify the script to do the same calculation
with a while loop.
Practice Script
function r=sumofcubes(N)
ans=0;
for i=1:N
ans=ans+i^3;
end
r=ans;
Practice
Now modify this script to add up the
cubes of the even integers.
Note that mod(i,2)=0 when i is an even
number
Inline Functions
One downside to Matlab functions in
m-files is the proliferation of files
resulting from having each function in
its own file
For simple functions, this can be
avoided with an inline function
Example
g=inline('cos(x)+cos(1.1*x)')
x=0:0.01:100;
y=g(x);
plot(x,y)
Parameters
x=0:0.01:100;
gg=inline('cos(x)+cos(delta*x)','x','delta')
delta=1.05
y=gg(x,delta);
plot(x,y)
Command Window Shows Form of
Function
An Alternative Form (Anonymous
Functions)
x=0:0.01:100;
delta=1.05
gg=@(x, delta) cos(x)+cos(delta*x)
y=gg(x, delta);
plot(x,y)
Practice
Consider the function
Plot using an inline function
Use 0<x<10 and a=0.25
Note: syntax can be taken from:
gg=inline('cos(x)+cos(delta*x)','x','delta')
gg=@(x, delta) cos(x)+cos(delta*x)
) sin( * ) * exp( ) ( x x a x f =
Subfunctions
Subfunctions allow us to put two
functions in one file.
The second function will not be available
to other functions.
I will use this to consolidate my files into
one.
Example (save as example.m)
function example
clear all
r=sumofcubes(20);
fprintf('The sum of the first 20 cubes is %i\n',r)
%
function r=sumofcubes(N)
ans=0;
for i=1:N
ans=ans+i^3;
end
r=ans;
Comments in Scripts
Note that the % sign represents a comment
Everything on a line after that will be ignored
Comments can be on their own line or at end
of a line of working code, eg.
y=gg(x); %function defined inline above
An Example with Numerics
Suppose were looking for a $100k, 30-year
mortgage. What interest rate do I need to keep
the payments below $700 per month?
Solve for i
0
) 1 (
1 ) 1 (
700 100000
360
360
=
(

+
+

i i
i
Approach
Create user-defined function
Plot the function
Find point where function is zero
Create the function
function s=f(i)
p=100000;
n=360;
a=700;
s=p-a*((1+i).^n-1)./(i.*(1+i).^n);
Plot the Function
First save file as f.m
Now enter the following:
i=0.001:0.0001:0.01;
y=f(i);
plot(i,y)
The Plot
1 2 3 4 5 6 7 8 9 10
x 10
-3
-12
-10
-8
-6
-4
-2
0
2
4
x 10
4
Result
Zero-crossing is around i=0.006
Annual interest rate is 12*i, or about 7%
Try more accurate solution
12*fzero('f',0.006)
This gives about 7.5%
Debugging Scripts
Cells in Scripts
Defining
Documenting
Incrementing Variables
Publishing Scripts
Demo
Questions?

You might also like