more functions in matlab. functions that operate on other functions a function f() can take another...

39
More Functions in MATLAB

Upload: isabel-sherman

Post on 18-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

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

More Functionsin MATLAB

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

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.

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

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

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

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)

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

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

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

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

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

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

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

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

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

fzero() - Additional input variables

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

P1,P2,...:

These are additional input variables required by Fname

after the first.

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

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;

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

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

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

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

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

You can do:warning offfzero(...)

warning on

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

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

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

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

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

x =

1.2995

fval =

0

exit =

1

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

fzero and solving equations

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

112

1

121

12n

n

r

rr

AP

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

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

0

112

r1

12

r1

12

r

APn

n

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

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;

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

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%

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

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

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

fminbnd() - Other forms

Additional output variables:

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

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

Additional input variables:

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

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

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

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

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

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

x =

0.6370

y =

11.2528

exit =

1

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

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

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

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

quad() - Evaluates the definite integral

area = quad(@Fun,xL,xR)

Fun: a function name

xL,xR:

range of x over which to

integrate

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

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.

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

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

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

area = quad(@Fun,xL,xR)

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

area =

2.0000

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

area =

-1.5389e-016

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

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

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

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

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

ans =

-1.2375

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

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.

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

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

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

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.