Lecture 6 MATLAB programming (4)

advertisement

Lecture

 

6

 

MATLAB

 

programming

 

(4)

Dr.

  Tony   Cahill

Objectives

• User ‐ defined   Functions

– Anonymous   Functions

– Function   name   as   an   input   argument

– Subfunctions and   nested   functions

– Arguments  

– persistent   variable

Anonymous

 

Functions

• You   can   create   a   simple   function   without   creating   an   M ‐ file file_handle=@(argument   list)   expression

For   example,   define   an   anonymous   function   f1:  

>>   f1=@(x,y)   x^2+y^2;

>>   f1(3,4) ans =

25

Anonymous

 

Functions

• Anonymous   function   can   include   variables   exist   in   the   workspace   where   it   is   created:

>>   a=4;b=2;

>>   f2=@(x)   a*x^b;

>>   f2(3) ans =

36

Note   that   if   you   changed   the   variables   a   and   b   later,   their   values   in   the   anonymous   function   does   not change.

Function

 

name

 

as

 

an

 

input

 

argument

(function

 

functions)

• Function   functions:   are   functions   whose   input   arguments   include   the   names   of   other   functions.

• Example:   matlab contains   a   function   function called   fzero,   which   locates   a   zero   of   the   function   that   is   passed   to   it.

Try:   fzero(@cos,[0,pi])

@   means   get   the   ‘handle’   of   the   matlab build ‐ in   function   ‘cos’

Anonymous

 

function

 

as

 

an

 

input

 

argument

• Don’t   need   to   get   the   ‘handle’   of   the   function   if   you   are   using   anonymous   function   as   input.

 

For   example,   using   the   matlab function   ‘fplot’   to   make   a   plot   of   an   anonymous   function

20

15 >>f1=@(x) x^2-x-1;

>>fplot(f1,[0,5]); 10

5

0

-5

0 0.5

1 1.5

2 2.5

3 3.5

4 4.5

5

Example

 

– A

 

simple

 

plotter

function quickplot( fun, xlim )

%QUICKPLOT Generate a quick plot of a function

% generate x for plotting n_steps=100; x=linspace(xlim(1),xlim(2),n_steps);

% linspace is a matlab build-in function, it is equivalent

% to the following code:

%step_size=(xlim(2)-xlim(1))/n_steps;

%x=xlim(1):step_size:xlim(2);

% evaluate the function y=fun(x);

%plot plot(x,y); end

Example

 

– A

 

simple

 

plotter

>>   quickplot(f1,   [0,   5])

???

  Error   using   ==>   mpower

Matrix   must   be   square.

Error   in   ==>   @(x)x^2 ‐ x ‐ 1

Error   in   ==>   quickplot at   13 y=fun(x);

So,   it   looks   like   we   need   to   have   a   vector   version   of   the   function   f1:

>>   f1=@(x)   x .^ 2 ‐ x ‐ 1;

>>   quickplot(f1,   [0,   5])

Evaluate

 

a

 

string

 

as

 

a

 

function

• eval:   evaluate   a   character   string   as   if   it   has   been   typed   from   the   command   window try:   y=eval(‘sin(pi/4)’) x=0:10; y=eval(‘x.*x+3.*x+1’)

Another

 

simple

 

plotter

function quickplot( funstr, xlim )

%QUICKPLOT Generate a quick plot of a function

% funstr: is a string to evaluate n_steps=100; x=linspace(xlim(1),xlim(2),n_steps);

% evaluate the function y=eval(funstr); plot(x,y); end

Test

 

the

 

plotter

• It   allows   input   from   the   user:

Code   for   testplot.m

fun=input( 'Enter the expression you want to plot:' , 's' ); xmin=input( 'Enter the lower limit xmin:' ); xmax=input( 'Enter the upper limit xmax:' ); quickplot(fun,[xmin xmax]);

>>   testplot

Enter   the   expression   you   want   to   plot:sin(x)*5

Enter   the   lower   limit   xmin: ‐ pi

Enter   the   upper   limit   xmax:pi

0

-1

-2

-3

-4

-5

-4

3

2

1

5

4

-3 -2 -1 0 1 2 3 4

Subfunctions

• It   is   possible   to   place   more   than   one   function   in   a   single   file.

– The   first   function   (which   has   the   same   name   as   the   file   itself)   is   the   primary   function.

– The   other   functions   below   it   are   subfunctions.

• Used   as   ‘utility’   functions   to   implement   the   primary   function.

 

• Only   accessible   to   other   functions   in   the   same   file.

Subfunctions

my_function.m

function   my_function(x)

… a=subfunc1(x); b=subfunc2(x);

… end

This   is   the   primary   function function   y=subfunc1(x)

… end function   y=subfunc2(x)

… end

These   are   subfunctions.

Nested

 

functions

• Nested   functions   are   functions   that   are   defined   entirely   in   the   body   of   another   function,   called   the   host   function.

– The   are   only   visible   to   the   host   function   and   the   same   level   nested   functions.

– Host   variables   are   visible   to   the   nested   function   .

– Variables   in   the   nested   function   is   not   visible   to   the   host   function.

Nested

 

functions

my_function.m

function   my_function(x)

… a=nestfunc1(x); b=nestfunc2(x);

… function   y=nestfunc1(x)  

… end function   y=nestfunc2(x)

… end end

Example

Variable   in   the   host   function   is   visible   to   the   nested   function.

function testnest( x ) nestsub1; fprintf( 'in testnest: x=%f\n' ,x); function nestsub1 fprintf( 'in sub1: x=%f\n' ,x); x=x+1; end end

>>   testnest(3) in   sub1:   x=3.000000

in   testnest:   x=4.000000

Example

A   variable   in   the   nested   function   with   the   same   name   as   a   variable   in   the   host   function: function testnest( x ) nestsub1(x); fprintf( 'in testnest: x=%f\n' ,x); function nestsub1(x) fprintf( 'in sub1: x=%f\n' ,x); x=x+1; end end

>>   testnest(3) in   sub1:   x=3.000000

in   testnest:   x=3.000000

Argument

 

related

 

MATLAB

 

functions

• nargin:   returns   the   number   of   actual   input   arguments

• nargout:   returns   the   number   of   actual   output   arguments

Example

function [   r,   theta   ]   =   polar_value(   x,   y   )

%POLAR_VALUE   Converts   (x,y)   to   (r,   theta) if nargin ~=   2 msg='Two   input   arguments   are   required.'; error(msg);

Stop   program   execution   and   print   out   an   error   message end if nargout ~=2 msg='Two   output   arguments   are   preferred.'; warning(msg); Print   out   a   warning   message,   program   continues end if x==0   &   y==0   msg='Both   x   and   y   are   zero:   angle   is   meaning   less.'; warning(msg); end

%   calculate   the   magnitude   and   angle r=sqrt(x.^2+y.^2); theta=atan2(y,x)*180/pi;      %   results   in   degrees end %   end   of   function   polar_value

Persistent

 

variable

• Sometimes   it   is   desirable   to   keep   the   value   of   a   local   variable   in   a   function function   y   =   testpersistent(   x   ) persistent n; if   isempty(n) n=0;      %   initialize   n end n=n+x; fprintf('n=%f\n',n); end

A   persistent   variable   is   empty   when   it   is   first   defined   by   the   persistent   keyword.

It   needs   to   be   initialized   before   participating in   any   operations.

Download