### First Steps in quadprog Code Generation

This example shows how to generate code for the quadprog optimization solver. Code generation requires a MATLAB® Coder™ license. For details about code generation requirements, see Code Generation for quadprog Background.

The problem is to minimize the quadratic expression

$\frac{1}{2}{x}^{T}Hx+{f}^{T}x$

where

$H=\left[\begin{array}{ccc}1& -1& 1\\ -1& 2& -2\\ 1& -2& 4\end{array}\right]$

and

$f=\left[\begin{array}{c}2\\ -3\\ 1\end{array}\right]$

subject to the constraints $0\le x\le 1$, $\sum x=1/2$.

Create a file named test_quadp.m containing the following code.

H = [1,-1,1
-1,2,-2
1,-2,4];
f = [2;-3;1];
lb = zeros(3,1);
ub = ones(size(lb));
Aeq = ones(1,3);
beq = 1/2;
x0 = zeros(3,1);

Generate code for the test_quadp file.

After some time, codegen creates a MEX file named test_quadp_mex.mexw64 (the file extension varies, depending on your system). Run the resulting C code.

x =

0
0.5000
0

fval =

-1.2500

### Modify Example for Efficiency

Following some of the suggestions in the topic Optimization Code Generation for Real-Time Applications, configure the generated code to have fewer checks and to use static memory allocation.

cfg = coder.config('mex');
cfg.IntegrityChecks = false;
cfg.SaturateOnIntegerOverflow = false;
cfg.DynamicMemoryAllocation = 'Off';

Create a file named test_quadp2.m containing the following code. This code sets a looser optimality tolerance than the default 1e-8.

H = [1,-1,1
-1,2,-2
1,-2,4];
f = [2;-3;1];
lb = zeros(3,1);
ub = ones(size(lb));
Aeq = ones(1,3);
beq = 1/2;
x0 = zeros(3,1);
'OptimalityTolerance',1e-5);

Generate code for the test_quadp2 file.

Run the resulting code.

x =

0
0.5000
0

fval =

-1.2500

eflag =

1

output =

struct with fields:

algorithm: 'active-set'
firstorderopt: 8.8818e-16
constrviolation: 0
iterations: 3

Changing the optimality tolerance does not affect the optimization process, because the 'active-set' algorithm does not check this tolerance until it reaches a point where it stops.

Create a third file that limits the number of allowed iterations to 2 to see the effect on the optimization process.

H = [1,-1,1
-1,2,-2
1,-2,4];
f = [2;-3;1];
lb = zeros(3,1);
ub = ones(size(lb));
Aeq = ones(1,3);
beq = 1/2;
x0 = zeros(3,1);

To see the effect of these settings on the solver, run test_quadp3 in MATLAB without generating code.

Solver stopped prematurely.

quadprog stopped because it exceeded the iteration limit,
options.MaxIterations = 2.000000e+00.

x =

-0.0000
0.5000
0

fval =

-1.2500

exitflag =

0

output =

struct with fields:

algorithm: 'active-set'
iterations: 2
constrviolation: 1.6441e-18
firstorderopt: 2
message: '↵Solver stopped prematurely.↵↵quadprog stopped because it exceeded the iteration limit,↵options.MaxIterations = 2.000000e+00.↵↵'
linearsolver: []
cgiterations: []

In this case, the solver reached the solution in fewer steps than the default. Usually, though, limiting the number of iterations does not allow the solver to reach a correct solution.