More Functions in MATLAB. Functions that operate on other functions A function F() can take another...

Post on 18-Jan-2016

224 views 0 download

Tags:

Transcript of More Functions in MATLAB. Functions that operate on other functions A function F() can take another...

More Functionsin MATLAB

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.

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

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)

humps(x) - example function

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-20

0

20

40

60

80

100

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

x2 - 2

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

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

fzero() - Additional input variables

x = fzero(@Fname,x0,[],P1,P2,...)

P1,P2,...:

These are additional input variables required by Fname

after the first.

Poly4(x) = x4 +Cx2+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;

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

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-2

-1

0

1

2

3

4

5

x = fzero(@Fname,x0,[],P1,P2,...)

>> fzero(@Poly4,.5,[],-3,0,.5)Warning: ...ans = 0.4209

You can do:warning offfzero(...)

warning on

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

[x,fval,exit] = fzero(...)

>> [x,fval,exit] = fzero(@humps,1)

x =

1.2995

fval =

0

exit =

1

fzero and solving equations

Given values for A,P, and n, solve the following for r:

112

1

121

12n

n

r

rr

AP

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

0

112

r1

12

r1

12

r

APn

n

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;

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%

fminbnd() - Finds a local minimum of a function of one variablex = fminbnd(@Fname,xL,xR)

Fname: a function name

xL,xR:

range of x to search in

fminbnd() - Other forms

Additional output variables:

[x,fval] = fminbnd(...)

[x,fval,exitflag] = fminbnd(...)

Additional input variables:

x = fminbnd(@Fname,xL,xR,[],P1,P2,...)

humps(x)

0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-20

0

20

40

60

80

100

[x,y,exit] = fminbnd(...)

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

x =

0.6370

y =

11.2528

exit =

1

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

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-2

-1

0

1

2

3

4

5

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

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.

sin(x)

0 1 2 3 4 5 6-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

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.5708y = -1.0000>> maxval = -ymaxval = 1.0000

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

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-2

-1

0

1

2

3

4

5

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

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

quad() - Evaluates the definite integral

area = quad(@Fun,xL,xR)

Fun: a function name

xL,xR:

range of x over which to

integrate

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.

sin(x)

0 1 2 3 4 5 6-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

area = quad(@Fun,xL,xR)

>> area = quad(@sin,0,pi)

area =

2.0000

>> area = quad(@sin,0,2*pi)

area =

-1.5389e-016

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

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2-2

-1

0

1

2

3

4

5

area = quad(@Fname, xL,xR,[],[],P1,P2,…)

>> quad(@Poly4,.5,1.5,[],[],-3,0,.5)

ans =

-1.2375

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 @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.

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

x=linspace(a,b,npts);

%to evaluate a function handle, must use feval.

y=feval(Fname,x);

Passing Functions as Arguments to OtherFunctions 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.