Clear Filters
Clear Filters

how to add a constraint condition to fsolve?

36 views (last 30 days)
Hi everyone,
These days I want to solve a system of nonlinear equations with matlab. In the equations, there are all four unkonwns, A(1),A(2),A(3)and A(4) to be solved but only three equations. therefore, the 'levenberg-marquardt' algorithm is applied to get the results. However, for physical meaning, an additional constraint is required, i.e. A(3)should be larger than zero. and with the 'levenberg-marquardt' algorithm, in the the obtained result, A(3) is negative.
Does anyone know how to add this constraint condition to fsolve ? Your help will be highly appreciated!

Accepted Answer

John D'Errico
John D'Errico on 19 Nov 2013
While Sean is correct in his answer, there is a trick that ail make it trivial to solve using solve for simple constraints like this.
If A(3) MUST be positive, then in your objective function, square whatever number is passed in and use that in your calculations. The square of a number will always be positive, so in effect we have implemented an inequality constraint.
For the starting value for A(3), pass in the sqrt of what ever value you would have passed in otherwise.
And finally, when the result is returned, square A(3) to get the value used in the computation.
This transformation trick is a nice one, but one that seems to be forgotten too easily.
  3 Comments
John D'Errico
John D'Errico on 20 Nov 2013
Matt, there can always be conditioning issues. Optimization is an art, that is best done when you use the available tools to best effect, while watching for problems. As well, if you are careful and understand the methods involved, you can always construct a problem that will cause failure for any numerical method that works on a black box.
The point is, use the tools you have, but do so carefully. If it succeeds, as the advice I have offered will do most of the time, then it has solved the problem simply and at the cost of little additional effort. When that advice fails, then look to see if you have stumbled on a singularity, and if necessary, look for a different tool.
Matt J
Matt J on 8 Dec 2013
Edited: Matt J on 8 Dec 2013
One other thing to be careful of with this method is to avoid choosing A(3)=0 as the initial guess. The gradient of the objective function F(x) will always be zero at x=0 after making the transformation F(x^2), e.g.,
>> fsolve(@(x)1-x,0,optimset('Display','none'))
ans =
1
versus
>> fsolve(@(x)1-x^2,0,optimset('Display','none'))
ans =
0
versus
>> fsolve(@(x)1-x^2,0.5,optimset('Display','none'))
ans =
1.0000

Sign in to comment.

More Answers (3)

Sean de Wolski
Sean de Wolski on 19 Nov 2013
fsolve does not provide the ability to use constraints. You'll need fmincon for this.

Matt J
Matt J on 19 Nov 2013
Edited: Matt J on 19 Nov 2013
However, for physical meaning, an additional constraint is required, i.e. A(3)should be larger than zero.
If your constraints are simply non-negativity and bound constraints, you should probably use lsqnonlin. lsqnonlin is a bit more specialized than fmincon, and there may be advantages to that.
lsqnonlin is also set up to take exactly the same objective function format as fsolve, whereas fmincon doesn't.

Alan Weiss
Alan Weiss on 20 Nov 2013
Alan Weiss
MATLAB mathematical toolbox documentation

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!