K. Alavi: firstname.lastname@example.org MATLAB Functions, feval, inline functions 1. Function Definition Line a. For 3 outputs u, v, w and 2 inputs x, y function[u,v,w] = yourfun (x,y) b. For 1 output W and three inputs x, y, z function[W]= myfun(x,y,z]) OR equivalently function W = myfun (x,y,z]) c. For function with no output and 3 inputs function[ ]= ellipse(L,S,theta) OR equivalently Function = ellipse(L,S,theta) This could be for a function that draws an ellipse with major & minor axes L and S and rotation theta 2. Function Call General: we have to give values for the inputs and use variable name that store the values of the outputs. Below are examples of how the above functions are called a. [A,B,C] = yourfun(-5,4) This means that input values are x=-5, y=4, and output values for A, B, and C, are calculated according to the function prescription b. [M] = myfun(2,4,-3) OR equivalently M = myfun(2,4,-3) c. ellipse(10,5,pi/6) 3. The feval Command This command evaluates the values of the output variables for a function for given values of the inputs. For example we can find values for the outputs of yourfun for input values of x=-5 and y=4 using feval in the following manner: [a,b,c] = feval(‘yourfun’,-4,5) or alternatively we can define a function handle for yourfun, call it func1 and use it with feval: func1 =@yourfun [a,b,c] = feval(func1,-4,5) 4. The inline Command This command is used to generate a function in the command window or within a script file or a function file. For example for a function of 3 variables x, y, z we have Function-name =inline(“math formula’,’x’,’y’,’z’) Here the formula and the input variable names are in single quotes. For example the following inline function to evaluate distance between two vectors Ra and Rb with angle ab between them: Distance=inline(‘sqrt(Ra.^2+Rb.^2... -2*Ra.*Rb.*cos(ab))’,’Ra’,’Rb’,’ab’) Note that because the statement was too long we used ... for continuation. In the command file the following will appear: Inline function: Distance(Ra,Rb,ab)= sqrt(Ra.^2+Rb.^2... -2*Ra.*Rb.*cos(ab)) To use this, we give values for Ra, Rb, and ab, say, Ra=3, Rb=4, and ab=pi/4 type Distance(3,4,pi/4). The value for Distance will be then evaluated for the given input values. Note if you use arrays for Ra, Rb, and ab, you can get distance for as many vector pairs as you want.
Pages to are hidden for
"MATLAB Functions, feval, inline functions,"Please download to view full document