linear least square with nonlinear constraints
4 views (last 30 days)
Show older comments
Vikas Saroha
on 4 Mar 2022
Commented: Vikas Saroha
on 7 Mar 2022
I currently have a system of equations y = A*x. y is 18 x 1 vector while matrix A is of size 18x12. x is 12 x 1 parameter vector which is to be determined. Additionally, there are three nonlinear constraints which I want to add. Using fmincon I am not able to formulate the objective function in terms of x(1)..........x(12) for 18 equations. Is there other way to describe the objective function like A*[x(1); x(2);...........x(12)] or how I can solve this problem in matlab?
1 Comment
Accepted Answer
John D'Errico
on 4 Mar 2022
Edited: John D'Errico
on 4 Mar 2022
Fmincon requires a single objective to optimize. So if you have a set of 18 equations, in 12 unknowns, then you formulate the objective in terms of the sum of squares of the errors of the equations. For example. Suppose I wanted to solve the simple linear least squares problem
A*x = y
using fmincon, where we have essentially 10 equations and 5 unknowns. (I'm solving a smaller problem here because it will be easier to look at the results.) For now, assume there are no constraints at all. Since I am far too lazy to actually come up with a real problem, here is my test example:
A = randn(10,5);
y = randn(10,1);
So an entirely valid problem. Now we wish to solve the initially unconstrained problem. Just as a test to see the result was successful, we need to know the solution using simple linear least squares.
x0 = A\y
Now, let me try using fmincon.
obj = @(x) norm(A*x-y);
xstart = randn(5,1); % too lazy to think of a better set
[x1,fval,exitflag] = fmincon(obj,xstart)
Well, I'll be a horned toad! It worked! I got the same result. Of course, it was way more computational effort, the MATLAB equivalent of using a Mack truck to take a pea to Boston. But fmincon gave the same result.
I could have defined the objective function as (A*x-y)'*(A*x-y). But norm does just as well, and it is shorter to write, and since norm by default is the 2-norm, it is effectively the same thing. Well, it would be, if I squared the norm. But since squaring a positive number is a monotonic transformation, the location of the minimum is unchanged.
Now, suppose I wanted to enforce a nonlinear constraint? Trivial. Lets see. I'll decide to enforce that the sum of the squares of the x(i) must be less than 1. I'll throw in an equality constraint too. How about that the sum of the elements must be 0.5? The function nonlcons
[C,CEQ] = nonlcons(x1)
So the solution generated using the simple linear least squares fails these requirements.
[x2,fval,exitflag] = fmincon(obj,xstart,[],[], [],[], [],[], @nonlcons)
Does it satisfy the constraints? Of course, at least to within trash on the order of the tolerances employed.
[C,CEQ] = nonlcons(x2)
function [C,CEQ] = nonlcons(X)
C = sum(X.^2) - 1; % this must be less than zero.
CEQ = sum(X) - .5;
end
More Answers (0)
See Also
Categories
Find more on Systems of Nonlinear Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!