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.

3.5 Command-Line Functions<br />

83<br />

3.5 Command-Line Functions<br />

3.5.1 Inline functions<br />

A mathematical function, such as F(x) or F(x, y), usually requires just the values<br />

of the independent variables for computing the value of the function. We frequently<br />

need to evaluate such functions in scientific calculations. One way to evaluate such<br />

functions is <strong>by</strong> programming them in function files (see Section 4.2 on page 102<br />

for details). However, there is a quicker way of programming functions if they are<br />

not too complicated. This is done <strong>by</strong> defining inline functions-functions that are<br />

created on the command line. You define these functions using the built-in function<br />

inline.<br />

The syntax for creating an inline function is particularly simple:<br />

F = inline ('function formuLa')<br />

Thus, a function such as F(x) = x 2 sin(x) can be coded as<br />

F = inline ('x-2 * sin(x) ')<br />

However, note that our inline function can only take a scalar x as an input argument.<br />

We can modify it easily <strong>by</strong> changing the arithmetic operator to accept array<br />

argument: F=inline ( 'x. -2. *sin (x) ' ) . Once the function is created, you can use<br />

it as a function independently (e.g., type F(5) to evaluate the function at x = 5)<br />

or in the input argument of other functions that can evaluate it. See Fig. 3.4 for<br />

more examples and usage of inline functions. There are several examples of these<br />

functions in Section 3.8 on plotting simple graphs.<br />

'"F'o r a.n:ti rie 'help·<br />

type:<br />

b._;3 p inline<br />

3.5.2 Anonymous functions<br />

Anonymous functions are functions without names, created and referred <strong>by</strong> their<br />

handles. A function handle, created internally <strong>by</strong> <strong>MATLAB</strong> and stored in a userdefined<br />

variable, is basically the identity of the function.<br />

An anonymous function is created <strong>by</strong> the command<br />

f = ©(input List) mathematicaL expression<br />

where f is the function handle. The input list can contain a single variable or several<br />

variables separated <strong>by</strong> commas. After creating the function, you can use it with its<br />

handle to evaluate the function or pass it as an argument to other functions.<br />

Examples:<br />

fx = ©(x) x-2 - sin(x) ;<br />

fx(5)<br />

fxy = ©(x,y) x-2 + y-2;<br />

fxy(2,3)<br />

fx - @(x) x.- 2 - 5in (x)<br />

x= [O: .i:pi/2] ; plot (x,fx (x))<br />

creates a function f ( x) = x 2 - sin x,<br />

evaluates f(x) at x = 5,<br />

creates a function f(x, y) = x 2 + y 2 ,<br />

evaluates f(x, y) at x = 2 and y= 3,<br />

vcctorizcs the function .f ( x), and<br />

plots f(x) over 0 to n/2.<br />

For a tutorial on anonymous functions, see Lesson 7 in Chapter 2. Anonymous<br />

functions are handy for comm:.md-line computation. For computing more involved<br />

functions that may require several intermediate calculations, you should usc function<br />

files (see Lesson 5 in Chapter 2 and Section 4.2 on page 102).

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

Saved successfully!

Ooh no, something went wrong!