How to add a constraint when using fsolve?
조회 수: 22 (최근 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
채택된 답변
추가 답변 (2개)
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)
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
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
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
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.
참고 항목
카테고리
Help Center 및 File 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!
