Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Functions in MATLAB. Functions that operate on other functions A function F() can take another function G() as an argument by using a notation:

Similar presentations


Presentation on theme: "More Functions in MATLAB. Functions that operate on other functions A function F() can take another function G() as an argument by using a notation:"— Presentation transcript:

1 More Functions in MATLAB

2 Functions that operate on other functions A function F() can take another function G() as an argument by using a special @ notation: F(@G,…) F() takes G() as an argument Matlab documentation refers to these as function functions The operator @ 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(@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(@Fname,x0) % finds x near 1 so humps(x)=0 >> fzero(@humps,1) ans = 1.2995 % finds x between 0 and 2 so humps(x) = 0 >> fzero(@humps,[0,2]) ans = 1.2995

7 x 2 - 2

8 x = fzero(@Fname,x0) First define G(x) in a file G.m: function [y] = G(x) y = x^2 - 2; Then: >> x = fzero(@G,[0,2]) x = 1.4142 >> x = fzero(@G,[-2,0]) x = -1.4142

9 fzero() - Additional input variables x = fzero(@Fname,x0,[],P1,P2,...) P1,P2,...: These are additional input variables required by Fname after the first.

10 Poly4(x) = x 4 +Cx 2 +Bx+A function [y] = Poly4(x,C,B,A) %function to evaluate x^4+Cx^2+Bx+A %x can be an array but C,B,A are assumed scalars y= x.^4+C*x.^2+B*x+A;

11 Poly4(x,C,B,A) - with C=-3, B=0, A=.5

12 x = fzero(@Fname,x0,[],P1,P2,...) >> fzero(@Poly4,.5,[],-3,0,.5) Warning:... ans = 0.4209 You can do: warning off fzero(...) warning on

13 fzero() - Additional output variables [x,fval] = fzero(...) [x,fval,exitflag] = fzero(...) fval: value of fun at solution exitflag: >0 if zero is found <0 if zero is not found

14 [x,fval,exit] = fzero(...) >> [x,fval,exit] = fzero(@humps,1) x = 1.2995 fval = 0 exit = 1

15 fzero and solving equations Given values for A,P, and n, solve the following for r:

16 Rewrite Equation in form f(r,n,A,P)=0

17 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) *(1+r/12) ^n; Bot = (1+r/12) ^n – 1; val = P – A *Top/Bot;

18 Use fzero to find one real solution Rate=fzero(@intfun,.1,[ ],48,21000,500); disp(['The interest rate that you need is ', … numstr(Rate),'%']) Command Window>> The interest rate you need is 6.7048%

19 fminbnd() - Finds a local minimum of a function of one variable x = fminbnd(@Fname,xL,xR) Fname: a function name xL,xR: range of x to search in

20 fminbnd() - Other forms Additional output variables: [x,fval] = fminbnd(...) [x,fval,exitflag] = fminbnd(...) Additional input variables: x = fminbnd(@Fname,xL,xR,[],P1,P2,...)

21 humps(x)

22 [x,y,exit] = fminbnd(...) >> [x,y,exit] = fminbnd(@humps,.4,.8) x = 0.6370 y = 11.2528 exit = 1

23 Poly4(x,C,B,A) - with C=-3, B=0, A=.5

24 x = fminbnd(@Fun, xL,xR,[],P1,P2,...) >> fminbnd(@Poly4,1,2,[],-3,0,.5) ans = 1.2248 >> fminbnd(@Poly4,-1.5,-.5,[],-3,0,.5) ans = -1.2247

25 Finding a maximum 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.

26 sin(x)

27 Local maximum of sin(x) First define Nsin(x) in a file Nsin.m: function y = Nsin(x) y = -sin(x); Then: >> [x,y] = fminbnd(@Nsin,0,2*pi) x = 1.5708 y = >> maxval = -y maxval = 1.0000

28 Poly4(x,C,B,A) - with C=-3, B=0, A=.5

29 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);

30 Local maximum of Poly4(x,C,B,A) >> [x,y] = fminbnd(@Poly4neg,-1,1,[],-3,0,.5) x = 0 y = -0.5000 >> maxval = -y maxval = 0.5000

31 quad() - Evaluates the definite integral area = quad(@Fun,xL,xR) Fun: a function name xL,xR: range of x over which to integrate

32 quad() - Other forms Additional input variables: area = quad(@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.

33 sin(x)

34 area = quad(@Fun,xL,xR) >> area = quad(@sin,0,pi) area = 2.0000 >> area = quad(@sin,0,2*pi) area = -1.5389e-016

35 Poly4(x,C,B,A) - with C=-3, B=0, A=.5

36 area = quad(@Fname, xL,xR,[],[],P1,P2,…) >> quad(@Poly4,.5,1.5,[],[],-3,0,.5) ans = -1.2375

37 Passing Functions as Arguments to Other Functions 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 @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.

38 Passing Functions as Arguments to Other Functions 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 x=linspace(a,b,npts); %to evaluate a function handle, must use feval. y=feval(Fname,x);

39 Passing Functions as Arguments to Other Functions in MATLAB We could then invoke Setup as: [x,y]=Setup(@sin,0,2*pi,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.


Download ppt "More Functions in MATLAB. Functions that operate on other functions A function F() can take another function G() as an argument by using a notation:"

Similar presentations


Ads by Google