MATLAB: How to set non decreasing output in lsqlin

constrained linear fitforestryleast squaresMATLABmodeling

I am trying use lsqlin to make a linear fit to some forestry data while applying two constraints on the output model. I have vectors of percentage of canopy coverage at varying heights assembled into a matrix [m (plots) x n (percent coverage at height bin k)] as the input independent variable, and the model is intended to predict above ground biomass measurements which are in a vector of length m measured plots. The two constraints I have are that the output be 0 at position 0, x(0) = 0 and that the output be non-decreasing.
It is unclear in the documentation of lsqlin on how to go about setting these constraints. I have used the backslash operator and lsqnonneg to create simple models, but they do not have the capability of adding these constraints that I have found.

Best Answer

You matrix A is [m x n]. Therefore your solution x will have [n x 1] dimension. You can apply these constraints using equality and inequality constraints as follow
1) Constraint that x(1)=0, (note in MATLAB indexing start from 1), is same as
1*x(1) + 0*x(2) + 0*x(3) + .... + 0*x(n) = 0
which becomes
Aeq = [1 0 0 .... 0]; % n-1 zeros
beq = 0;
2) Now coming to inequality constraints, you require
x(2) > x(1)
x(3) > x(2)
...
...
x(n) > x(n-1)
which is equivalent to
x(1) - x(2) < 0;
x(2) - x(3) < 0;
...
...
x(n-1) - x(n) < 0;
In matrix for these constraints become
A = [1 -1 0 0 .... 0; % fill with zeros to make total columns equal to n
0 1 -1 0 .... 0;
0 0 1 -1 .... 0;
....
....
0 0 .... 0 1 -1]; % total of n-1 rows will be formed
B = [0;
0;
0;
....
....
0]; % n-1 rows
But these matrices A, b, Aeq and beq in the lsqlin() to solve the constrained problem