02.03.2016 Views

MATLAB by rudra pratap

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

106<br />

Programming in <strong>MATLAB</strong>: Scripts and Functions<br />

Executing a function inside another function<br />

Usually, it is a straightforward process, so much so that you do not have to pay any<br />

special attention to it. In the function sol vexf, we used a built-in function, det,<br />

to calculate the determinant of A. We used the function just the way we would<br />

use it at the <strong>MATLAB</strong> prompt or in a script file. This is true for all functions,<br />

built-in or user-written. The story is different only when you want the function<br />

name to be dynamic, that is, if the function to be executed inside may be different<br />

with different executions of the calling function. 2 In such cases, the actual name of<br />

the function is passed to the calling function through the input list and a dummy<br />

name is used inside the calling function. The mechanism of passing the function<br />

name and evaluating it inside the calling function is quite different from that for a<br />

variable. We explain it below.<br />

A function in the input list<br />

When a function needs to be passed in the input list of another function, the name<br />

of the function to be passed must appear as a character string in the input list. For<br />

example, the built-in function fzero finds a zero of a user-supplied function of a<br />

single variable (see Section 5.6, page 168, for more details) . The call syntax of the<br />

function is fzero (f , x) where f is the name of the function or a function handle<br />

and x is an initial guess. There are several ways in which we can code the function<br />

f and pass it in the input list of fzero. We describe a few common ways of doing<br />

so on an example function:<br />

f(r) = r3 - 32r2 + (r - 22)r + 100.<br />

Fo r on tine help<br />

function .. handl e<br />

Use inline function: We can make f(r) be an inline function (see Section 3.5.1<br />

on page 83 for a detailed discussion) and pass its name to fzero as follows:<br />

fr = inline ('r-3 - 32*r-2 + (r-22)*r + 100');<br />

rO = fzero (fr, 5) ;<br />

Use function file: We can code f(r) in a function file called fu nr.m as follows:<br />

function f = funr(r) ;<br />

% FUNR evaluates function f = r-3 -32 r-2+ (r-22)r+100.<br />

f = r-3 - 32*r-2 + (r-22)*r + 100;<br />

Now we may call fzero with the statement:<br />

rO = fzero('funr' ,5).<br />

Note the single quotes around the name of the input function funr.<br />

Use function handle: A function handle is a convenient function identifier (a<br />

variable) created <strong>by</strong> the user. It is created with the ill symbol. We can use<br />

it for any function-built-in or a user-written M-file function (but not an<br />

inline function) . As examples, let us do the same thing with funr as we did<br />

previously but with a function handle.<br />

2 A function that uses another function inside its body is called a calling function. For example,<br />

sol vexf in the example earlier is a calling function for the function det .

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!