solving nonlinear equation including max function

9 次查看(过去 30 天)
Hi,
i want to solve this system of equations using matlab. can you please help me to do this?

采纳的回答

Torsten
Torsten 2022-11-11
编辑:Torsten 2022-11-11
options = optimset('TolX',1e-10,'TolFun',1e-10);
fun = @(x,y)[x - max(0.9*y,-1+0.9*x);y - max(2+0.9*x,2+0.9*x)]
fun = function_handle with value:
@(x,y)[x-max(0.9*y,-1+0.9*x);y-max(2+0.9*x,2+0.9*x)]
sol = fsolve(@(z)fun(z(1),z(2)),[1 1],options)
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
sol = 1×2
9.4737 10.5263
fun(sol(1),sol(2))
ans = 2×1
0 0

更多回答(3 个)

John D'Errico
John D'Errico 2022-11-11
编辑:John D'Errico 2022-11-11
In general, things like the max function introduce non-differentiable points, and even discontinuities into a problem. And that makes the use of solvers, that generally rely on smoothness for their objectives, problematic. But this is only a 2 variable probem. And it should be relatively easy to work things at least, so we can understand what is happening.
Assume two variables, I'll call them A and B, instead of your notation, which is difficult to type. We have two equations:
A = max(0 + 0.9*B, -1 + 0.9*A)
B = max(2 + 0.9*A, 2 + 0.9*A)
The second equation is strange, since the max function has both halves identical. Are you sure this is the case, or is it a typo? Assuming it is correct as written, then the max is irrelevant in equation 2.
B = 2 + 0.9*A
Substitute back into the first equation, we would find
A = max(0 + 0.9*(2 + 0.9*A), -1 + 0.9*A)
A = max(0.81*A + 9/5, 0.9*A - 1)
There would seem to be two cases here. Either A = -10, or A = 9/5*1/0.19. But A=-10 is not a solution to that equation. So we have
A = (9/5)/0.19
A =
9.473684210526315
Now we can recover B, as
B = 2 + 0.9*A
B =
10.526315789473683
Could I have used solve instead of paper and pencil?
syms A B
solve(A == max(0 + 0.9*B, -1 + 0.9*A), B == max(2 + 0.9*A, 2 + 0.9*A))
ans =
struct with fields:
A: [0×1 sym]
B: [0×1 sym]
Apparently solve fails, if we try it. I would expect solve to fail, and it did, as things like max are often not symbolic friendly operations.
And looking up, I see that Torsten has used fsolve, which did result in (the same) valid solution. However, I also note that if equation 2 is not the trivial one you have, then we have multiple discontinuities that appear in the problem.

Matt J
Matt J 2022-11-11
编辑:Matt J 2022-11-11
Reorganizing the equations into the form A*x<=b, Aeq*x=beq and using this FEX download,
A=[ -1 0.9;
-0.1 0];
b=[0;1];
Aeq=[0.9 -1];
beq=-2;
lb=[1,1]*-1e6; %lcon2vert must solve over a bounded region,
ub=[1,1]*1e6; %so apply generous box constraints.
[A,b, Aeq,beq]=addBounds(A,b, Aeq,beq,lb,ub);
V=lcon2vert(A,b,Aeq,beq)
accept=all( V>lb/2 & V<ub/2 ,2);
V=V(accept,:) %Accept only solutions that are interior to the box bounds.
V =
9.4737 10.5263

Bruno Luong
Bruno Luong 2022-11-11
编辑:Bruno Luong 2022-11-11
The dumb method (buts surely reliable and give all possible solution) is to assume one of the 4 combinations
  1. rhs max in (1) is the first term, rhs max in (2) is the first term
  2. rhs max in (1) is the first term, rhs max in (2) is the second term
  3. rhs max in (1) is the second term, rhs max in (2) is the first term
  4. rhs max in (1) is the second term, rhs max in (2) is the second term
Each of them is a linear 2 x 2 system to solve. Solve then 4 times then check if the corresponding assumption meets.
Of course (2) seem having a typo made by the OP.

Community Treasure Hunt

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

Start Hunting!

Translated by