# lsqlin

Solves a linear least squares optimization problem.

### Calling Sequence

xopt = lsqlin(C,d,A,b)
xopt = lsqlin(C,d,A,b,Aeq,beq)
xopt = lsqlin(C,d,A,b,Aeq,beq,lb,ub)
xopt = lsqlin(C,d,A,b,Aeq,beq,lb,ub,x0)
xopt = lsqlin(C,d,A,b,Aeq,beq,lb,ub,x0,options)
[xopt,resnorm,residual,exitflag,output,lambda] = lsqlin( ... )

### Input Parameters

C :

A matrix of doubles, representing the multiplier of x in the expression C⋅x - d. The number of columns in C is equal to the number of elements in x.

d :

A vector of doubles, representing the additive constant term in the expression C⋅x - d. The number of elements in d is equal to the number of rows in C matrix.

A :

A matrix of doubles, containing the coefficients of linear inequality constraints of size (m X n) where 'm' is the number of linear inequality constraints.

b :

A vector of doubles, related to 'A' and represents the linear coefficients in the linear inequality constraints of size (m X 1).

Aeq :

A matrix of doubles, containing the coefficients of linear equality constraints of size (m1 X n) where 'm1' is the number of linear equality constraints.

beq :

A vector of double, vector of doubles, related to 'Aeq' and represents the linear coefficients in the equality constraints of size (m1 X 1).

lb :

A vector of doubles, containing the lower bounds of the variables of size (1 X n) or (n X 1) where 'n' is the number of variables.

ub :

A vector of doubles, containing the upper bounds of the variables of size (1 X n) or (n X 1) where 'n' is the number of variables.

x0 :

A vector of doubles, containing the starting values of variables of size (1 X n) or (n X 1) where 'n' is the number of variables.

options :

A list, containing the option for user to specify. See below for details.

### Outputs

xopt :

A vector of doubles, containing the computed solution of the optimization problem.

resnorm :

A double, containing the objective value returned as a scalar value norm(C⋅x-d)^2.

residual :

A vector of doubles, containing the solution residuals, returned as a vector d-C⋅x.

exitflag :

An integer, containing the flag which denotes the reason for termination of algorithm. See below for details.

output :

A structure, containing the information about the optimization. See below for details.

lambda :

A structure, containing the Lagrange multipliers of the lower bounds, upper bounds and constraints at the optimized point. See below for details.

### Description

Search the minimum of a constrained linear least square problem specified by:

lsqlin calls Ipopt, an optimization library written in C++, to solve the linear least squares problem.

The options should be defined as type "list" and consist of the following fields:

options= list("MaxIter", [---], "CpuTime", [---]);

• MaxIter : A Scalar, specifying the maximum number of iterations that the solver should take.
• CpuTime : A Scalar, specifying the maximum amount of CPU time in seconds that the solver should take.

The default values for the various items are given as:

options = list("MaxIter", [3000], "CpuTime", [600]);

The exitflag allows the user to know the status of the optimization which is returned by Ipopt. The values it can take and what they indicate is described below:

• 0 : Optimal Solution Found
• 1 : Maximum Number of Iterations Exceeded. Output may not be optimal.
• 2 : Maximum amount of CPU Time exceeded. Output may not be optimal.
• 3 : Stop at Tiny Step.
• 4 : Solved To Acceptable Level.
• 5 : Converged to a point of local infeasibility.

For more details on exitflag, see the Ipopt documentation which can be found on http://www.coin-or.org/Ipopt/documentation/

The output data structure contains detailed information about the optimization process. It is of type "struct" and contains the following fields.

• output.iterations: The number of iterations performed.
• output.constrviolation: The max-norm of the constraint violation.

The lambda data structure contains the Lagrange multipliers at the end of optimization. In the current version, the values are returned only when the the solution is optimal. It has type "struct" and contains the following fields.

• lambda.lower: The Lagrange multipliers for the lower bound constraints.
• lambda.upper: The Lagrange multipliers for the upper bound constraints.
• lambda.eqlin: The Lagrange multipliers for the linear equality constraints.
• lambda.ineqlin: The Lagrange multipliers for the linear inequality constraints.

A few examples displaying the various functionalities of lsqlin have been provided below. You will find a series of problems and the appropriate code snippets to solve them.

### Example

We begin with a simple objective function subjected to three inequality constraints.

 //Example 1:An example with inequality constraints. //Initializing C and D. C = [1 1 1; 1 1 0; 0 1 1; 1 0 0; 0 0 1] d = [89; 67; 53; 35; 20]; //We specify the linear inequality constraints below. A = [3 2 1; 2 3 4; 1 2 3]; b = [191 209 162]; //Run lsqlin [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin(C,d,A,b);

### Example

Here we build up on the previous example by adding equality constraints. We add the following constraint to the problem specified above:

 //Example 2: Using inequality and equality constraints. //Initializing C and D. C = [1 1 1; 1 1 0; 0 1 1; 1 0 0; 0 0 1] d = [89; 67; 53; 35; 20]; //We specify the linear inequality constraints below. A = [3 2 1; 2 3 4; 1 2 3]; b = [191 209 162]; //We specify the linear equality constraints below. Aeq = [1 2 1]; beq = 10; //Run lsqlin [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin(C,d,A,b,Aeq,beq)

### Example

In this example, we proceed to add the upper and lower bounds to the objective function.

 //Example 3: Using equality, inequality constraints and variable bounds //Initializing C and D. C = [1 1 1; 1 1 0; 0 1 1; 1 0 0; 0 0 1] d = [89; 67; 53; 35; 20]; //We specify the linear inequality constraints below. A = [3 2 1; 2 3 4; 1 2 3]; b = [191 209 162]; //We specify the linear equality constraints below. Aeq = [1 2 1]; beq = 10; //The upper and lower bounds for the objective function are defined in simple vectors as shown below. lb = [0.1,3,1]; ub = [4,5,1]; //Run lsqlin [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin(C,d,A,b,Aeq,beq,lb,ub)

### Example

In this example, we proceed to provide an initial value for x to facilitate the computation. We also further enhance the functionality of lsqlin by setting input options. This provides us with the ability to control the solver parameters such as the maximum number of solver iterations and the max. CPU time allowed for the computation.

 //Example 4: Using equality, inequality constraints and variable bounds, initializing x and options. //Initializing C and D. C = [1 1 1; 1 1 0; 0 1 1; 1 0 0; 0 0 1] d = [89; 67; 53; 35; 20]; //We specify the linear inequality constraints below. A = [3 2 1; 2 3 4; 1 2 3]; b = [191 209 162]; //We specify the linear equality constraints below. Aeq = [1 2 1]; beq = 10; //The upper and lower bounds for the objective function are defined in simple vectors as shown below. lb = [0.1,3,1]; ub = [4,5,1]; //Initializing x. x0 = [1,2,3] //Setting the options options = list("MaxIter", [5000], "CpuTime", [1000]); //Run lsqlin [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin(C,d,A,b,Aeq,beq,lb,ub,x0,options)

### Example

Infeasible Problems: Find x in R^3 such that it minimizes: We add the following constraint to the objective function specified above:

 //Example 5: Infeasible problem //Initializing C and D. C = [1 1 1; 1 1 0; 0 1 1; 1 0 0; 0 0 1] d = [89; 67; 53; 35; 20]; //We specify the linear inequality constraints below. A = [3 2 1; 2 3 4; 1 2 3]; b = [191 209 162]; //We specify the linear equality constraints below. Aeq = [1 2 3]; beq = 200; //Run lsqlin [xopt,resnorm,residual,exitflag,output,lambda] = lsqlin(C,d,A,b,Aeq,beq)

### Authors

• Harpreet Singh