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
):
You can then evaluate f
at 2.0:
And you can pass f
to an optimization routine to minimize it:
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 first 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
and you then call lsqcurvefit
.
Other examples that use this technique:
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
,
![]() | Output Headings: Large-Scale Algorithms | Typical Problems and How to Deal with Them | ![]() |