how to add a constraint condition to fsolve?
36 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
John D'Errico
2013-11-19
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 个评论
John D'Errico
2013-11-20
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
2013-12-8
编辑:Matt J
2013-12-8
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
更多回答(3 个)
Sean de Wolski
2013-11-19
fsolve does not provide the ability to use constraints. You'll need fmincon for this.
0 个评论
Matt J
2013-11-19
编辑:Matt J
2013-11-19
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!