12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

v6v6 =1 2 3 4 5 6The feval FunctionThe feval comm<strong>and</strong> is like eval, except that it is used for evaluatingnamed functions. An example would be:str = ’sin’;t = linspace(0,2*pi);q = feval(str,t);plt(t,q)If str is a string containing the name <strong>of</strong> a function, then y =feval(str,x) evaluates that function for the input argument x.Another example defines data for plotting by looping over the trigonometricfunctions sin, cos, <strong>and</strong> tan contained within a single matrix <strong>of</strong>characters (the comm<strong>and</strong> zeroaxes is part <strong>of</strong> the companion s<strong>of</strong>twareto this book):str = [’sin’;’cos’;’tan’];for i = 1:3q(i,:) = feval(str(i,:),t);endclfplt(t,q)axis([0 2*pi -6 6])zeroaxesInline ObjectsInline objects allow you to store a function as a string <strong>and</strong> use it muchas you would write it symbolically. This, for example, is how to definethe parabola f(x) =(x + 1)(x − 1):>> f = inline(’(x + 1).*(x - 1)’)f =Inline function:f(x) = (x + 1).*(x - 1)We can now evaluate f(3) by typing:>> f(3)ans =8c○ 2000 by CRC Press LLC

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

Saved successfully!

Ooh no, something went wrong!