How to add a constraint when using fsolve?

15 次查看(过去 30 天)
Hi, I'm currently trying to solve a system of five nonlinear equations using fsolve. However, I know that fsolve doesn't really allow you to add constraints. I need the fifth variable to be less than or equal to 24, but I don't even know where to even begin to get this problem solved. I also need all my variables to be greater than or equal to zero, but that's been a problem I've been able to solve by making my starting points around 10 instead of zero. To give greater context, these equations are all partial derivatives that are then equated to zero.
And here are the solutions:
However, the last variable (the one equal to 95.8047) can't be greater than 24. Please help! I'd really appreciate it! Thank you!
  4 个评论
Sabrina Chui
Sabrina Chui 2020-12-14
x0=[10,10,10,10,10]
fun0=@(x)[-0.003*(x(1))^2+(x(1)+x(2)-14)^(-1);
-0.003*(x(2))^2+(x(1)+x(2)-14)^(-1);
0.95*((-0.003)*(1-x(5)/96)*(x(3))^2+(x(3)+x(4)-14)^(-1));
0.95*((-0.003)*(1-x(5)/96)*(x(4))^2+(x(3)+x(4)-14)^(-1));
-2+0.001/48*(x(3))^3];
fsolve(fun0,x0)
Sabrina Chui
Sabrina Chui 2020-12-14
Here's the code, sorry about posting it as an image the first time. Alex, how do you know that there will be no exact real number solution? How could you tell right away? Does this mean I need to change my actual objective function that I'm taking partials from? Thank you!

请先登录,再进行评论。

采纳的回答

Sabrina Chui
Sabrina Chui 2020-12-15
I ended up installing the Global Optimization Toolbox, which allowed me to run a Global Search. I did end up changing some parameters in my model to make it run better which is why some of the numbers are a little different from the original question post. Here is the code:
gs = GlobalSearch;
minmin = @(x)(-(-0.001*x(1)^3-0.001*x(2)^3+2*10*log(x(1)+x(2)-14)-x(5)^0.7-x(6)^0.7+0.95*(-0.001*(1-x(5)/40)*x(3)^3-0.001*(1-x(5)/40)*x(4)^3)+2*10*log(x(3)+x(4)-14)));
problem = createOptimProblem('fmincon','x0',[10,10,10,10,24,24],...
'objective',minmin,'lb',[0,0,0,0,0,0],'ub',[Inf,Inf,Inf,Inf,24,24]);
x = run(gs,problem)
And here were my results:
Thank you so much Matt and Walter for helping me with this!

更多回答(2 个)

Matt J
Matt J 2020-12-14
编辑:Matt J 2020-12-15
The fifth equation involves only x(3) and can be solved immediately
x3=(2*48/0.001)^(1/3)
x3 = 45.7886
The 3rd and 4th equations then imply that x4=x3
x4=x3
x4 = 45.7886
and can be used to solve for x5,
x5=1-(-1./(x3+x4-14)*96/0.95/-0.003/x3^2)
x5 = 0.7929
which satisfies the constraint x5<=24.
Comparison of the first two equations implies that x1=x2=z, which reduces the problem to the single variable root finding problem
0.003*z^2 = (2*z-14)^(-1)
which can be solved with fzero() to give,
x1=x2=9.0396
  3 个评论
Matt J
Matt J 2020-12-15
If we subtract the 3rd equation from the 4th equation, we obtain,
0.95 * (-0.003) * (1-x(5)/96) * ( x(4)^2 -x(3)^2 ) = 0
In order for this equation to be satisfied, either x(5)=96, which you say is forbidden, or x(4)=x(3).

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2020-12-14
residue = @(x)sum(fun0(x).^2)
Now use an optimizer to minimize residue using lb all 0, and ub all inf except 24 for the appropriate entry.
Caution: this approach does not guarantee that you will find a zero, and can need to be tested with multiple initial conditions, such as by using a MultiStart from Global Optimization Toolbox
  12 个评论
Matt J
Matt J 2020-12-15
编辑:Matt J 2020-12-15
Setting partial derivatives to zero is only a valid optimality condition for unconstrained problems. It is no longer valid once you impose bounds. So, GlobalSearch is more successful than your fun3 formulation for two reasons. First, it is guiding its search with proper optimality conditions. Second, it has mechanisms which help it avoid local minima that fmincon alone does not.
Sabrina Chui
Sabrina Chui 2020-12-15
Okay, thank you so much for clearing that up! I feel a lot better about using GlobalSearch now!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Global or Multiple Starting Point Search 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by