Title: More Functions in MATLAB
1More Functionsin MATLAB
2Functions that operate on other functions
- A function F() can take another function G() as
an argument by using a special _at_
notationF(_at_G,) F() takes G() as an argument - Matlab documentation refers to these as function
functions - The operator _at_ produces what is called a handle
to the function whose name follows the symbol.
Although the text discusses 3 other methods for
passing a function in an argument list to another
function, the only method acceptable in this
course is the function handle.
3Functions that operate on other functions
- Let's look at three useful Matlab functions
- fzero() - finds a zero of a function
- fminbnd() - finds a minimum of a function
- quad() - numerically evaluates a definite
integral over a function
4fzero() - Finds a zero of a function of one
variable (solves Fname(x)0)
- x fzero(_at_Fname,x0)
- Fname a function name
- x0 either a single
- x value starting point or
- xL xR x value range with Fname(xL)being
opposite in sign from Fname(xR)
5humps(x) - example function
6x fzero(_at_Fname,x0)
- finds x near 1 so humps(x)0
- gtgt fzero(_at_humps,1)
- ans 1.2995
- finds x between 0 and 2 so humps(x) 0
- gtgt fzero(_at_humps,0,2)ans 1.2995
7x2 - 2
8x fzero(_at_Fname,x0)
- First define G(x) in a file G.mfunction y
G(x) - y x2 - 2
- Then
- gtgt x fzero(_at_G,0,2)x 1.4142
- gtgt x fzero(_at_G,-2,0)x -1.4142
9fzero() - Additional input variables
- x fzero(_at_Fname,x0,,P1,P2,...)
- P1,P2,...
- These are additional input variables (or
parameters) - required by Fname
- after the first parameters
- needed by fzero.
10fzero() - Additional input variables
- x fzero(_at_Fname,x0,,P1,P2,...)
-
- P1,P2,...
- These are additional input variables (or
parameters) required by Fname after the first
parameters needed by fzero.
This is a placeholder. Since there can be two
parameters for fzero, and we only are using one,
we need a null placeholder, (null matrix) to hold
this position.
11Poly4(x) x4 Cx2BxA
- function y Poly4(x,C,B,A)
- function to evaluate x4Cx2BxA
- x can be an array but C,B,A are assumed to be
scalars - y x.4Cx.2BxA
This is a sample, user-defined function we will
use to illustrate the ideas of these functions
that pass functions.
12Poly4(x,C,B,A) - with C-3, B0, A0.5
13x fzero(_at_Fname,x0,,P1,P2,...)
- gtgt fzero(_at_Poly4,.5,,-3,0,.5)
- Warning ...
- ans
- 0.4209
- You can do
- warning off
- fzero(...)
- warning on
This warning can be distracting. So
14fzero() - Additional output variables
- x,fval fzero(...)
- x,fval,exitflag fzero(...)
- fval value of fun at solution
- exitflag gt0 if zero is found
- lt0 if zero is not found
15x,fval,exit fzero(...)
- gtgt x,fval,exit fzero(_at_humps,1)
- x
- 1.2995
- fval
- 0
- exit
- 1
16fzero and solving equations
- Given values for A,P, and n, solve the following
for r
17Rewrite Equation in form f(r,n,A,P)0
18Define Matlab function
- function val intfun(r,n,A,P)
- r is interest rate, n is number of months, A is
- amount of loan, P is monthly payment
- Top (r/12) (1r/12) n
- Bot (1r/12) n 1
- val P A Top/Bot
19Use fzero to find one real solution
- Ratefzero(_at_intfun,.1, ,48,21000,500)
- disp('The interest rate that you need is ',
- numstr(Rate),'')
- Command Windowgtgt
- The interest rate you need is 6.7048
20fminbnd() - Finds a local minimum of a function
of one variable
- x fminbnd(_at_Fname,xL,xR)
- Fname a function name
- xL,xR
- range of x to search in
21fminbnd() - Other forms
- Additional output variables
- x,fval fminbnd(...)
- x,fval,exitflag fminbnd(...)
- Additional input variables
- x fminbnd(_at_Fname,xL,xR,,P1,P2,...)
22humps(x)
23x,y,exit fminbnd(...)
- gtgt x,y,exit fminbnd(_at_humps,.4,.8)
- x
- 0.6370
- y
- 11.2528
- exit
- 1
24Poly4(x,C,B,A) - with C-3, B0, A.5
25x fminbnd(_at_Fun, xL,xR,,P1,P2,...)
- gtgt fminbnd(_at_Poly4,1,2,,-3,0,0.5)ans
1.2248 - gtgt fminbnd(_at_Poly4,-1.5,-0.5,,-3,0,.5)ans
-1.2247
26Finding a maximum
- How would we find a local maximum? We might
expect to have a fmaxbnd function, but we do not. - To find the maximum of a function, you must find
the minimum of the negative of the function. - To do so, you'll have to define an M-file which
is the negative of the desired function. - The desired maximum value is the negative of the
minimum value so obtained.
27sin(x)
28Local maximum of sin(x)
- First define Nsin(x) in a file Nsin.m
- function y Nsin(x)
- y -sin(x)
- Then
- gtgt x,y fminbnd(_at_Nsin,0,2pi)
- x
- 1.5708
- y
- -1.0000
- gtgt maxval -y
- maxval
- 1.0000
29Poly4(x,C,B,A) - with C-3, B0, A0.5
30Local maximum of Poly4(x,C,B,A)
- First define Poly4neg in a file Poly4neg.m as
follows - function y Poly4neg(x,C,B,A)
- POLY4NEG Negative of the example 4th order
polynomial - y -Poly4(x,C,B,A)
31Local maximum of Poly4(x,C,B,A)
- gtgt x,y fminbnd(_at_Poly4neg,-1,1,,-3,0,0.5)
- x
- 0
- y
- -0.5000
- gtgt maxval -y
- maxval
- 0.5000
32quad() - Evaluates the definite integral
- area quad(_at_Fun,xL,xR)
- Fun a function name
- xL,xR
- range of x over which to
- integrate
33quad() - Other forms
- Additional input variables
- area quad(_at_Fun,xL,xR,,,P1,P2,...)
- Important
- Fun must be a function that takes an input vector
and returns an output vector containing values of
the function at each element of the input.
There are two parameters for area that we are not
using, so we have two null matrices, which tell
area to use its default values.
34sin(x)
35area quad(_at_Fun,xL,xR)
- gtgt area quad(_at_sin,0,pi)
- area
- 2.0000
- gtgt area quad(_at_sin,0,2pi)
- area
- -1.5389e-016
36Poly4(x,C,B,A) - with C-3, B0, A0.5
37area quad(_at_Fname, xL,xR,,,P1,P2,)
- gtgt quad(_at_Poly4,0.5,1.5,,,-3,0,0.5)
- ans
- -1.2375
38Passing Functions as Arguments to OtherFunctions
in MATLAB
- When we invoke "function functions" such as fzero
and fminbnd, we pass a "handle" to the function
whose zero or minimum we want, using the syntax
_at_Fname, where Fname is the name of an existing
function. - You can write your own "function function" which
expects a function handle as an argument, if you
remember to use the Matlab function feval
whenever you need to evaluate the function whose
handle is being passed.
39Passing Functions as Arguments to OtherFunctions
in MATLAB
- For example, suppose you want to write a function
which will generate the x and y points for a
graph - function x,y Setup(Fname,a,b,npts)
- produces x and y vectors by generating npts,
- evenly spaced values of x
- between a and b and corresponding function
- values y Fname(x). Fname must be a function
handle - xlinspace(a,b,npts) produce x values using
linspace - to evaluate a function handle, must use feval.
- yfeval(Fname,x) this evaluates Fname into y
40Passing Functions as Arguments to OtherFunctions
in MATLAB
- We could then invoke Setup as
- x,ySetup(_at_sin,0,2pi,200)
- to produce 200 points on the sin curve for a
plot. - The first argument to feval is the function
handle while the remaining arguments are those
required by the function being evaluated. In the
example above, we assumed that Fname requires
only a single argument.