# lsqnonneg

Solves a nonnegative linear least squares optimization problem.

### Calling Sequence

xopt = lsqnonneg(C,d) xopt = lsqnonneg(C,d,options) [xopt,resnorm,residual,exitflag,output,lambda] = lsqnonneg( ... )

### 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.

- 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 at the optimized point. See below for details.

### Description

Solves nonnegative least-squares curve fitting problems specified by :

lsqlin calls Ipopt, an optimization library written in C++, to solve the nonnegative least-squares curve fitting 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.

A few examples displaying the various functionalities of lsqnonneg 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.

// Example 1: A basic lsqnonneg problem C = [1 1 1; 1 1 0; 0 1 1; 1 0 0; 0 0 1] d = [89; 67; 53; 35; 20;] [xopt,resnorm,residual,exitflag,output,lambda] = lsqnonneg(C,d) |

### Example

In this example, we further enhance the functionality of qpipopt 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 2: A basic lsqnonneg problem with solver options. C = [1 1 1; 1 1 0; 0 1 1; 1 0 0; 0 0 1] d = [89; 67; 53; 35; 20;] options = list("MaxIter", [5000], "CpuTime", [1000]); [xopt,resnorm,residual,exitflag,output,lambda] = lsqnonneg(C,d,options) |

### Authors

- Harpreet Singh