Main Content

Code Generation in Linear 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 lsqlin for optimizing movement or planning.

For an example, see Generate Code for lsqlin. For code generation in other optimization solvers, see Generate Code for fmincon, Generate Code for quadprog, Generate Code for lsqcurvefit or lsqnonlin, or Generate Code for fsolve.

Requirements for Code Generation

  • lsqlin supports code generation using either the codegen (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 or standard single-precision floating-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.

  • When solving unconstrained and underdetermined problems in MATLAB, lsqlin calls mldivide, which returns a basic solution. In code generation, the returned solution has minimum norm, which usually differs.

  • lsqlin does not support the problem argument for code generation.

    [x,fval] = lsqlin(problem) % Not supported
  • All lsqlin input matrices such as A, Aeq, lb, and ub must be full, not sparse. You can convert sparse matrices to full by using the full function.

  • The lb and ub arguments must have the same number of entries as the number of columns in C 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 lsqlin and specify them using optimoptions. The options must include the Algorithm option, set to 'active-set'.

    options = optimoptions('lsqlin','Algorithm','active-set');
    [x,fval,exitflag] = lsqlin(C,d,A,b,Aeq,beq,lb,ub,x0,options);
  • Code generation supports these options:

    • Algorithm — Must be 'active-set'

    • ConstraintTolerance

    • MaxIterations

    • ObjectiveLimit

    • OptimalityTolerance

    • StepTolerance

  • Generated code has limited error checking for options. The recommended way to update an option is to use optimoptions, not dot notation.

    opts = optimoptions('lsqlin','Algorithm','active-set');
    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.

  • If you specify an option that is not supported, the option is typically ignored during code generation. For reliable results, specify only supported options.

Single-Precision Code Generation

To generate code for single-precision floating point hardware, follow these guidelines.

  • Ensure that all solver inputs have single-precision values. These inputs include all empty and infinite values. For example, if your bounds in a 3-D problem are lb = [0,-Inf,0] and ub = [1,100,Inf], set the parameters as follows:

    lb = [single(0),-optim.coder.infbound("single"),single(0)];
    ub = [single([1,100]),optim.coder.infbound("single")];
  • Ensure that all empty solver arguments are single-precision. For example:

    Aeq = single([]); % Or single.empty
    beq = single([]); % Or single.empty

Note

This single-precision capability is available for code generation only; it is not available for general MATLAB computations.

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

(MATLAB Coder) | | | |

Related Topics