Code Generation in Nonlinear Least Squares: Background
What Is Code Generation?
Code generation is the conversion of MATLAB® code to C code using MATLAB Coder™. Code generation requires a MATLAB Coder license.
Typically, you use code generation to deploy code on hardware that is not running
MATLAB. For example, you can deploy code on a robot, using
lsqnonlin
for optimizing movement or planning.
For an example, see Generate Code for lsqcurvefit or lsqnonlin. For code generation in other optimization solvers, see Generate Code for fmincon, Generate Code for quadprog, Generate Code for lsqlin, or Generate Code for fsolve.
Requirements for Code Generation
lsqcurvefit
andlsqnonlin
support code generation using either thecodegen
(MATLAB Coder) function or the MATLAB Coder app. You must have a MATLAB Coder license to generate code.The target hardware must support standard double-precision floating-point computations. You cannot generate code for single-precision or fixed-point computations.
Code generation targets do not use the same math kernel libraries as MATLAB solvers. Therefore, code generation solutions can vary from solver solutions, especially for poorly conditioned problems.
All code for generation must be MATLAB code. In particular, you cannot use a custom black-box function as an objective function for
lsqcurvefit
orlsqnonlin
. You can usecoder.ceval
to evaluate a custom function coded in C or C++. However, the custom function must be called in a MATLAB function.Code generation for
lsqcurvefit
andlsqnonlin
currently does not support linear or nonlinear constraints.lsqcurvefit
andlsqnonlin
do not support theproblem
argument for code generation.[x,fval] = lsqnonlin(problem) % Not supported
You must specify the objective function by using function handles, not strings or character names.
x = lsqnonlin(@fun,x0,lb,ub,options) % Supported % Not supported: lsqnonlin('fun',...) or lsqnonlin("fun",...)
All input matrices
lb
andub
must be full, not sparse. You can convert sparse matrices to full by using thefull
function.The
lb
andub
arguments must have the same number of entries as thex0
argument or must be empty[]
.If your target hardware does not support infinite bounds, use
optim.coder.infbound
.For advanced code optimization involving embedded processors, you also need an Embedded Coder® license.
You must include options for
lsqcurvefit
orlsqnonlin
and specify them usingoptimoptions
. The options must include theAlgorithm
option, set to'levenberg-marquardt'
.options = optimoptions('lsqnonlin','Algorithm','levenberg-marquardt'); [x,fval,exitflag] = lsqnonlin(fun,x0,lb,ub,options);
Code generation supports these options:
Algorithm
— Must be'levenberg-marquardt'
FiniteDifferenceStepSize
FiniteDifferenceType
FunctionTolerance
MaxFunctionEvaluations
MaxIterations
SpecifyObjectiveGradient
StepTolerance
TypicalX
Generated code has limited error checking for options. The recommended way to update an option is to use
optimoptions
, not dot notation.opts = optimoptions('lsqnonlin','Algorithm','levenberg-marquardt'); opts = optimoptions(opts,'MaxIterations',1e4); % Recommended opts.MaxIterations = 1e4; % Not recommended
Do not load options from a file. Doing so can cause code generation to fail. Instead, create options in your code.
Usually, if you specify an option that is not supported, the option is silently ignored during code generation. However, if you specify a plot function or output function by using dot notation, code generation can issue an error. For reliability, specify only supported options.
Because output functions and plot functions are not supported, solvers do not return the exit flag –1.
Generated Code Not Multithreaded
By default, generated code for use outside the MATLAB environment uses linear algebra libraries that are not multithreaded. Therefore, this code can run significantly slower than code in the MATLAB environment.
If your target hardware has multiple cores, you can achieve better performance by using custom multithreaded LAPACK and BLAS libraries. To incorporate these libraries in your generated code, see Speed Up Linear Algebra in Generated Standalone Code by Using LAPACK Calls (MATLAB Coder).
See Also
lsqnonlin
| lsqcurvefit
| codegen
(MATLAB Coder) | optimoptions
| optim.coder.infbound