Main Content

Code Generation forQuadprogBackground

What Is Code Generation?

Code generation is the conversion of MATLAB®code to C/C++ code usingMATLAB Coder™. Code generation requires aMATLAB Coderlicense.

Typically, you use code generation to deploy code on hardware that is not running MATLAB.

For an example, seeGenerate Code for quadprog. For code generation in other optimization solvers, seeGenerate Code for fmincon,生成FSOLVE的代码, orGenerate Code for lsqcurvefit or lsqnonlin.

Code Generation Requirements

  • Quadprogsupports code generation using either thecodegen(MATLAB Coder)function or theMATLAB Coderapp. You must have aMATLAB Coderlicense 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.

  • Quadprog不支持金宝appproblemargument for code generation.

    [x,fval] = quadprog(problem)% Not supported
  • AllQuadprog输入矩阵,例如A,Aeq,lb, andub必须充满,不是稀疏。您可以使用稀疏矩阵将fullfunction.

  • Thelbubarguments must have the same number of entries as the number of columns inHor must be empty[].

  • For advanced code optimization involving embedded processors, you also need an Embedded Coder®license.

  • You must include options forQuadprog和specify them usingoptimoptions. The options must include theAlgorithmoption, set to'active-set'.

    选项= optimoptions('quadprog','Algorithm','active-set');[x,fval,exitflag] = quadprog(H,f,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 useoptimoptions, not dot notation.

    opts = optimoptions('quadprog','Algorithm','active-set');opts = optimoptions(opts,'MaxIterations',1e4);% Recommended选择。MaxIterations = 1 e4;% 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.

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, seeSpeed Up Linear Algebra in Generated Standalone Code by Using LAPACK Calls(MATLAB Coder).

See Also

|(MATLAB Coder)|

相关话题