More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is...

20
More on Functions… Lecture 8

Transcript of More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is...

Page 1: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

More on Functions…

Lecture 8

Page 2: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Preserving Data between Calls to a function

Persistent statement is declared in order to preserve some local information within a function between calls to afunction

persistent var1,var2…

Example: Running averages

function [ave,std]=runstats(x)

%runstats generate running ave /std deviation

persistent n

Persistent sum_x

Persistent sum_x2

if x == ‘reset’

n=0

Sum_x=0

Sum_x2=0

else

n=n+1

Sum_x = Sum_x

Sum_x2=Sum_x2+x^2

end

Page 3: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Preserving Data between Calls to A function

if n == 0 ave=0; std=0;elseif n == 1 ave=sum_x; std=0;else ave=sum_x/n; std=sqrt((n*sum_x2-sum_x^2)/(n*(n-1)));end

script file

[ave,std] = runstats(‘reset’)

nvals=input(‘enter number of values’);

for ii=1:nvals

x=input(‘enter a value’, ‘s’);

[ave,std]=runstats(x)

end

Page 4: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Function functionsA function includes the name of other functions in the input argument list.

Function Name Description

fminbnd Minimize a function of one variable

fzero find a zero of a function of one variable

quad Numerically integrate a function

ezplot easy to use function plotter

fplot plot a function by name

Common MATLAB Function functions

Page 5: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Function functions

Common MATLAB Function functions

>> fzero(‘cos’,[0 pi])ans = 1.5708

>> fzero('exp(x)-2',[0 1])

ans =

0.6931

Locates a zero of the function cos between 0 and pi.

Locates a zero of the function ex-2 between 0 and 1.

Page 6: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Function functions

Common MATLAB Function functions

>>fminbnd('x^3-2*x', 0,1)

ans =

0.816496985529690

>> quad(@fun,0,1)

ans =

-0.166666666666667

Locates a minimum between 0 and 1.

integrate function in ‘fun’ using simpson’s rule

function y=fun(x)y=x.^2-x;

‘fun’ includes any one variable function

Page 7: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Function functions

Common MATLAB Function functions

>>ezplot('x^3-2*x')quick plot of function in the string

-6 -4 -2 0 2 4 6-250

-200

-150

-100

-50

0

50

100

150

200

250

x

x3-2 x

-6 -4 -2 0 2 4 6

-1

-0.5

0

0.5

1

x

sin

>>ezplot(@sin)

Page 8: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Function functions

Common MATLAB Function functions

>> fplot('x^3-x',[-5 5])quick plot of function in the string

>> fplot(@sin,[-2*pi 2*pi])

-5 -4 -3 -2 -1 0 1 2 3 4 5-150

-100

-50

0

50

100

150

-6 -4 -2 0 2 4 6-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 9: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Function functions

eval and feval functions

eval evaluates a character string as though it had been typed in the Command Window.

feval evaluates a named function at a specific input value.

>>x=eval('sin(pi/4)')

x =

0.707106781186547

eval (string)

>>y=eval('x^2-2*x')

??? Error using ==> evalUndefined function or variable 'x'.

Page 10: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Function functions

eval and feval functions

eval evaluates a character string as though it had been typed in the Command Window.

feval evaluates a named function at a specific input value.

>>x=feval('sin’,pi/4)

x =

0.707106781186547

feval (fun,value)

>> y=feval(@fun,2)y = 2

function y=fun(x)y=x.^2-x;

Page 11: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Example: ascending sort

function out = ssort(a)nvals=length(a);for i=1:nvals-1iptr=i; for j=i+1:nvals if a(j)<a(iptr); temp = a(j); a(j) = a(iptr); a(iptr)= temp; end end

endout=a;

nvals=input('enter number of values to sort');array=zeros(1,nvals);for i=1:nvals string=['enter value' int2str(i) ': ']; array(i)= input(string);endsorted =ssort(array);disp(' Sorted data ');for i=1:nvals sorted(i)end

Page 12: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Function Handles

You can create a function handle to any function by using either @ before the function name. You can name the handle if you wish and use the handle to reference the function. For example, to create a handle to the sine function;

>> sine_handle = @sin;>> plot ([0:0.01:6], sine_handle, [0:0.01:6])

Page 13: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Methods for Calling Functions

There are four ways to invoke, or call, a function into a function.1.As a character string identifying the appropirate function M-file:2.As a function handle,3.As an inline function object4.As a string

1. function y= fun1(x)y = x.^2-4;The function may be called as follows, to compute the zero over the range 0<x<3.

[x,value]=fzero(‘fun1’,[0,3])

Page 14: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

2. As a function handle:[x,value] = fzero(@fun1,[0,3])

3. As an inline function object:>>fun1=‘x.^2-4’;>>fun_inline =inline(fun1);>>[x,value] = fzero(@fun1,[0,3]);

4. As a string expression>>fun1=‘x.^2-4’;>>[x,value] = fzero(@fun1,[0,3]);Or>>[x,value] = fzero(=‘x.^2-4’,[0,3]);

Page 15: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Anonymous Functions

Page 16: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Anonymous Functions

You can pass the handle of an anonymous function to another function

Multiple input arguments

Page 17: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Calling one function within another

Page 18: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.

Nested functions

Page 19: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.
Page 20: More on Functions… Lecture 8. Preserving Data between Calls to a function Persistent statement is declared in order to preserve some local information.