| Optimization Toolbox | ![]() |
Optimization of Inline Objects Instead of M-Files
The routines in the Optimization Toolbox also perform optimization on inline objects, avoiding the need to write M-files to define functions.
To represent a mathematical function at the command line, create an inline object from a string expression. For example, you can create an inline object of the humps function (use the command type humps to see the M-file function humps.m).
f = inline('1./((x-0.3).^2 + 0.01) + 1./((x-0.9).^2 + 0.04)-6');
You can then evaluate f at 2.0.
f(2.0) ans = -4.8552
And you can pass f to an optimization routine to minimize it.
x = fminbnd(f, 3, 4)
You can also create functions of more than one argument with inline by specifying the names of the input arguments along with the string expression. For example, to use lsqcurvefit, you need a function that takes two input arguments, x and xdata
f= inline('sin(x).*xdata +(x.^2).*cos(xdata)','x','xdata')
x = pi; xdata = pi*[4;2;3];
f(x, xdata)
ans =
9.8696e+000
9.8696e+000
-9.8696e+000
% Assume ydata exists x = lsqcurvefit(f,x,xdata,ydata)
Other examples that use this technique:
x = fsolve(inline('x*x*x-[1,2;3,4]'),ones(2,2))
x = lsqnonlin(inline('x*x-[3 5;9 10]'),eye(2,2))
fgoalattain where the function has additional arguments to pass to the optimization routine. For example, if the function to be minimized has additional arguments A, B, and C,fun = inline('sort(eig(A+B*x*C))','x','A','B','C');
x = fgoalattain(fun,-ones(2,2),[-5,-3,-1],[5, 3, 1],...
[ ],[ ],[ ],[ ],-4*ones(2),4*ones(2),[ ],[ ],A,B,C);
| Output Headings: Large-Scale Algorithms | Typical Problems and How to Deal with Them | ![]() |