Anonymous Functions in MATLAB Niloufer Mackey Example >>

[email protected](x) [4*x(1)-x(2)*x(2);4*x(1)+x(2)] g = @(x)[4*x(1)-x(2)*x(2);4*x(1)+x(2)] The @ character constructs the function handle, in this case g, to the anonymous function. The @ character should be immediately followed by the list of input arguments to the function, enclosed in round parentheses. In this example, we have one input argument, the vector x. Next, enter the expression that deﬁnes the function. Thus, in this example, we have deﬁned the vector valued function ([ ]) [ ] x1 4x1 − x22 g = x2 4x1 + x2 To evaluate g at the point (1, 1) >>g([1 1]) ans = 3 5 Now here is an example of a function that takes an anonymous function as input. function y = testhandle(f, x, h) dx = h*ones(size(x)); y=(f(x) - f(x+dx))./h; end Here is call to testhandle testhandle(g,[1 1], .1) ans = -1.9000 -5.0000

Created: 14 October 2011 Last Updated: Friday 14th October, 2011,

14:46