MATH 2370 Lecture Notes - Lecture 5: Generic Function, Entire Function, Matlab
Document Summary
>>out =@(x,y)sin(x)/(exp(y); %x and y are local inputs. Equivalent to: function z = out(x,y) z = sin(x)/exp(y) end. Another use: function functions ex: we want to compute the secant slope in a function for any math function we might want to pass to it function s=secslope(x1,x2,func) y1=func(x1) y2=func(x2) s=(y2-y1)/(x2-x1) end function y=sinexp(x) y=sin(x)/exp(x); end. Note: function definitions could be done in same script or separate m-file. Using an anonymous function as input ex: >> sinexp=@(x) sin(x)/exp(x); %definition. ^don"t need an @ symbol because we already "turned" it into a variable before. ^make it a function handle because built in are not anonymous. Note: the "generic" function name (in the input) and the function (handle) replacing it must have the same input/output structure ex: function y=mainfunc(a,n,func) b=a/2; x=3*ones(n,1) Intro to programming with matlab page 1 x=3*ones(n,1) [u1,v1,w1]=func(a,b,x) %meant to have a, b as scalars and x as a vector, output is 3 vectors b=2*a y=x/2.