Main Content

Generate Code forlsqlin

Linear Least-Squares Problem to Solve

Create pseudorandom data for the problem of minimizing the norm ofC*x – dsubject to bounds and linear inequality constraints. Create a problem for 15 variables, subject to the boundslb = –1andub = 1and subject to 150 linear constraintsA*x <= b.

N = 15;% Number of variablesrngdefault% For reproducibilityA = randn([10*N,N]); b = 5*ones(size(A,1),1); Aeq = [];% No equality constraintsbeq = []; ub = ones(N,1); lb = -ub; C = 10*eye(N) + randn(N); C = (C + C.')/2;% Symmetrize the matrixd = 20 * randn (N, 1);

Solve Usinglsqlin

Code generation requires the'active-set'algorithm, which requires an initial pointx0. To solve the problem in MATLAB®using the algorithm required by code generation, set options and an initial point.

x0 = 0(大小(d));选择=optimoptions('lsqlin','Algorithm','active-set');

To solve the problem, calllsqlin.

[x,fv,~,ef,output,lam] = lsqlin(C,d,A,b,Aeq,beq,lb,ub,x0,options);
Minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.

Afterlsqlinsolves this problem, look at the number of nonzero Lagrange multipliers of each type. See how many solution components are unconstrained by subtracting the total number of nonzero Lagrange multipliers.

nl = nnz(lam.lower); nu = nnz(lam.upper); ni = nnz(lam.ineqlin); nunconstrained = N - nl - nu - ni; fprintf('Number of solution components at lower bounds: %g\n',nl); fprintf('Number of solution components at upper bounds: %g\n',nu); fprintf('Number of solution components at inequality: %g\n',ni); fprintf('Number of unconstrained solution components: %g\n',nunconstrained);
Number of solution components at lower bounds: 3 Number of solution components at upper bounds: 2 Number of solution components at inequality: 5 Number of unconstrained solution components: 5

Code Generation Steps

To solve the same problem using code generation, complete the following steps.

  1. Write a function that incorporates all of the preceding steps. To produce less output, set theDisplayoption to'off'.

    function[x,fv,lam] = solvelsqlin N = 15;% Number of variablesrngdefault% For reproducibilityA = randn([10*N,N]); b = 5*ones(size(A,1),1); Aeq = [];% No equality constraintsbeq = []; ub = ones(N,1); lb = -ub; C = 10*eye(N) + randn(N); C = (C + C.')/2;% Symmetrize the matrixd = 20 * randn (N, 1);x0 = 0(大小(d));选择=optimoptions('lsqlin','Algorithm','active-set',...'Display','off'); [x,fv,~,ef,output,lam] = lsqlin(C,d,A,b,Aeq,beq,lb,ub,x0,options); nl = nnz(lam.lower); nu = nnz(lam.upper); ni = nnz(lam.ineqlin); nunconstrained = N - nl - nu - ni; fprintf('Number of solution components at lower bounds: %g\n',nl); fprintf('Number of solution components at upper bounds: %g\n',nu); fprintf('Number of solution components at inequality: %g\n',ni); fprintf('Number of unconstrained solution components: %g\n',nunconstrained);end
  2. Create a configuration for code generation. In this case, use'mex'.

    cfg = coder.config('mex');
  3. Generate code for thesolvelsqlinfunction.

    codegen-configcfgsolvelsqlin
  4. Test the generated code by running the generated file, which is namedsolvelsqlin_mex.mexw64or similar.

    [x2,fv2,lam2] = solvelsqlin_mex;
    Number of solution components at lower bounds: 1 Number of solution components at upper bounds: 5 Number of solution components at inequality: 6 Number of unconstrained solution components: 3
  5. The number of solution components at various bounds has changed from the previous solution. To see whether these differences are important, compare the solution point differences and function value differences.

    disp([norm(x - x2), abs(fv - fv2)])
    1.0e-12 * 0.0007 0.2274

    The differences between the two solutions are negligible.

See Also

||(MATLAB Coder)|

Related Topics