How to add a constraint when using fsolve?
21 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sabrina Chui
am 14 Dez. 2020
Kommentiert: Sabrina Chui
am 15 Dez. 2020
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 Kommentare
Akzeptierte Antwort
Weitere Antworten (2)
Matt J
am 14 Dez. 2020
Bearbeitet: Matt J
am 15 Dez. 2020
The fifth equation involves only x(3) and can be solved immediately
x3=(2*48/0.001)^(1/3)
The 3rd and 4th equations then imply that x4=x3
x4=x3
and can be used to solve for x5,
x5=1-(-1./(x3+x4-14)*96/0.95/-0.003/x3^2)
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 Kommentare
Matt J
am 15 Dez. 2020
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
am 14 Dez. 2020
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 Kommentare
Matt J
am 15 Dez. 2020
Bearbeitet: Matt J
am 15 Dez. 2020
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.
Siehe auch
Kategorien
Mehr zu Global or Multiple Starting Point Search finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
