More Functions in MATLAB - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

More Functions in MATLAB

Description:

quad(...) - numerically evaluates a definite integral over a function ... finds x near 1 so humps(x)=0 fzero(_at_humps,1) ans = 1.2995 ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 41
Provided by: ValuedGate2220
Category:

less

Transcript and Presenter's Notes

Title: More Functions in MATLAB


1
More Functionsin MATLAB
2
Functions 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.

3
Functions 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

4
fzero() - 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)

5
humps(x) - example function
6
x 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

7
x2 - 2
8
x 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

9
fzero() - 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.

10
fzero() - 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.
11
Poly4(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.
12
Poly4(x,C,B,A) - with C-3, B0, A0.5
13
x 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
14
fzero() - 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

15
x,fval,exit fzero(...)
  • gtgt x,fval,exit fzero(_at_humps,1)
  • x
  • 1.2995
  • fval
  • 0
  • exit
  • 1

16
fzero and solving equations
  • Given values for A,P, and n, solve the following
    for r

17
Rewrite Equation in form f(r,n,A,P)0
18
Define 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

19
Use 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

20
fminbnd() - 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

21
fminbnd() - Other forms
  • Additional output variables
  • x,fval fminbnd(...)
  • x,fval,exitflag fminbnd(...)
  • Additional input variables
  • x fminbnd(_at_Fname,xL,xR,,P1,P2,...)

22
humps(x)
23
x,y,exit fminbnd(...)
  • gtgt x,y,exit fminbnd(_at_humps,.4,.8)
  • x
  • 0.6370
  • y
  • 11.2528
  • exit
  • 1

24
Poly4(x,C,B,A) - with C-3, B0, A.5
25
x 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

26
Finding 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.

27
sin(x)
28
Local 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

29
Poly4(x,C,B,A) - with C-3, B0, A0.5
30
Local 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)

31
Local 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

32
quad() - Evaluates the definite integral
  • area quad(_at_Fun,xL,xR)
  • Fun a function name
  • xL,xR
  • range of x over which to
  • integrate

33
quad() - 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.
34
sin(x)
35
area 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

36
Poly4(x,C,B,A) - with C-3, B0, A0.5
37
area quad(_at_Fname, xL,xR,,,P1,P2,)
  • gtgt quad(_at_Poly4,0.5,1.5,,,-3,0,0.5)
  • ans
  • -1.2375

38
Passing 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.

39
Passing 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

40
Passing 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.
Write a Comment
User Comments (0)
About PowerShow.com