How to solve multiple equation with several conditions in it?
조회 수: 3 (최근 30일)
이전 댓글 표시
I have attached a given problem below in code I just want to solve the value of P1, P2, P3, P4, P5, P6, P7, P8, P9. Can anyone help me with this. Thanks in advance
clc
clear all
close all
Pbestrating = [8 5 4 2 9 7 3 1 6]
Pworstrating = [3 5 6 8 1 2 7 9 4]
syms P1 P2 P3 P4 P5 P6 P7 P8 P9 chi
Eq1 = P1-Pbestrating(1,1)*P1 <= chi;
Eq2 = P1-Pbestrating(1,2)*P2 <= chi;
Eq3 = P1-Pbestrating(1,3)*P3 <= chi;
Eq4 = P1-Pbestrating(1,4)*P4 <= chi;
Eq5 = P1-Pbestrating(1,5)*P5 <= chi;
Eq6 = P1-Pbestrating(1,6)*P6 <= chi;
Eq7 = P1-Pbestrating(1,7)*P7 <= chi;
Eq8 = P1-Pbestrating(1,8)*P8 <= chi;
Eq9 = P1-Pbestrating(1,9)*P9 <= chi;
Eq10 = P1-Pworstrating(1,1)*P5 <= chi;
Eq11 = P2-Pworstrating(1,2)*P5 <= chi;
Eq12 = P3-Pworstrating(1,3)*P5 <= chi;
Eq13 = P4-Pworstrating(1,5)*P5 <= chi;
Eq14 = P5-Pworstrating(1,6)*P5 <= chi;
Eq15 = P6-Pworstrating(1,7)*P5 <= chi;
Eq16 = P7-Pworstrating(1,8)*P5 <= chi;
Eq17 = P8-Pworstrating(1,9)*P5 <= chi;
Eq18 = P9-Pworstrating(1,1)*P5 <= chi;
Eq19 = P1+P2+P3+P4+P5+P6+P7+P8+P9 == 1
P1>=0;
P2>=0;
P3>=0;
P4>=0;
P5>=0;
P6>=0;
P7>=0;
P8>=0;
P9>=0;
댓글 수: 0
채택된 답변
John D'Errico
2024년 3월 31일
편집: John D'Errico
2024년 3월 31일
Is chi known? If not, then chi is one of your unknowns, and it must be solved for. Regardless, there will be infinitely many solutions. You have posed a linear problem. If any solution exists, then it is likely true there will be infinitely many. But the solution locus will be a 10 dimensional object, likely of infinite size, since chi will be unbounded. So you CANNOT solve for THE value.
It would be my guess that you might prefer a solution such that chi is as small as possible. The point being that if a solution exists, pretend it has chi == 1, then trivially that solution also satisfies a problem where chi == 2, or ANY larger value. As I said, chi is unbounded. And that means you would arguably want to find a solution that minimizes chi.
The solution itself is relatively easy. You could just linprog, but that would force you to build arrays for the constraints. Since you have typed out the constraints, I'll show how to solve it using a problem based optimization.
Pbestrating = [8 5 4 2 9 7 3 1 6];
Pworstrating = [3 5 6 8 1 2 7 9 4];
PROB = optimproblem;
P = optimvar('P',9,1,LowerBound = 0);
chi = optimvar('chi');
PROB.Constraints.C1 = P(1)-Pbestrating(1,1)*P(1) <= chi;
PROB.Constraints.C2 = P(1)-Pbestrating(1,2)*P(2) <= chi;
PROB.Constraints.C3 = P(1)-Pbestrating(1,3)*P(3) <= chi;
PROB.Constraints.C4 = P(1)-Pbestrating(1,4)*P(4) <= chi;
PROB.Constraints.C5 = P(1)-Pbestrating(1,5)*P(5) <= chi;
PROB.Constraints.C6 = P(1)-Pbestrating(1,6)*P(6) <= chi;
PROB.Constraints.C7 = P(1)-Pbestrating(1,7)*P(7) <= chi;
PROB.Constraints.C8 = P(1)-Pbestrating(1,8)*P(8) <= chi;
PROB.Constraints.C9 = P(1)-Pbestrating(1,9)*P(9) <= chi;
PROB.Constraints.C10 = P(1)-Pworstrating(1,1)*P(5) <= chi;
PROB.Constraints.C11 = P(2)-Pworstrating(1,2)*P(5) <= chi;
PROB.Constraints.C12 = P(3)-Pworstrating(1,3)*P(5) <= chi;
PROB.Constraints.C13 = P(4)-Pworstrating(1,5)*P(5) <= chi;
PROB.Constraints.C14 = P(5)-Pworstrating(1,6)*P(5) <= chi;
PROB.Constraints.C15 = P(6)-Pworstrating(1,7)*P(5) <= chi;
PROB.Constraints.C16 = P(7)-Pworstrating(1,8)*P(5) <= chi;
PROB.Constraints.C17 = P(8)-Pworstrating(1,9)*P(5) <= chi;
PROB.Constraints.C18 = P(9)-Pworstrating(1,1)*P(5) <= chi;
PROB.Constraints.C19 = sum(P) == 1;
PROB.Objective = chi;
PROB.ObjectiveSense = 'minimize';
Psol = solve(PROB)
Solving problem using linprog.
Optimal solution found.
Psol =
struct with fields:
P: [9×1 double]
chi: -0.21379
See that solve realizes the problem is linear, and therefore uses linprog. This yields a solution as:
Psol.P
ans =
0.030541
0.048866
0.061082
0.12216
0.33595
0.034904
0.081443
0.24433
0.040721
That was easy enough. We found a solution that had the smallest possible value of chi. If chi were to take on any smaller value, then no solution would exist. If chi is any larger than the critical value identified, then infintiely many solutions will be possible. And that made the solution now findable, instead of having infinitely many possible solutions.
If in fact, chi actually has some specific value, then you can specify it. And now you would be in the situation, that had you specified chi = 1, for example, then the solution locus would be a 9 dimensional polyhedron in the P variables. Any point inside or on the boundary of that polyhedron would be a valid solution. We resolve any such issues by minimizing chi.
댓글 수: 5
John D'Errico
2024년 4월 1일
편집: John D'Errico
2024년 4월 1일
Yes. I thought the crux of it was that chi must be minimized, as it makes no sense otherwise. What I don'r know is if you have written the correct equations. IF this truly is a set you think to be a solution for this set of equations, we can test that claim.
(Unfortunately, Answers is not allowing me to write and execute code inline today. This is a bug they have not repaired since yesterday, so I need to do this offline and copy my work in.)
P = [0.048 0.077 0.096 0.192 0.027 0.055 0.128 0.315 0.064];
First, test to see if the sum is correct.
sum(P)
ans =
1.002
So we can assume the elements of your vector P have been rounded to 3 digits, but effectively, they do sum to 1.
Next, what is the implicit value of chi, based on this set? In my solution, it was -0.21379.
Pbestrating = [8 5 4 2 9 7 3 1 6];
Pworstrating = [3 5 6 8 1 2 7 9 4];
If we substitute that vector into your constraints, we need to take the largest of these to see what was the implicit value of chi.
max([P(1)-Pbestrating(1,1)*P(1);
P(1)-Pbestrating(1,2)*P(2);
P(1)-Pbestrating(1,3)*P(3);
P(1)-Pbestrating(1,4)*P(4);
P(1)-Pbestrating(1,5)*P(5);
P(1)-Pbestrating(1,6)*P(6);
P(1)-Pbestrating(1,7)*P(7);
P(1)-Pbestrating(1,8)*P(8);
P(1)-Pbestrating(1,9)*P(9) ;
P(1)-Pworstrating(1,1)*P(5);
P(2)-Pworstrating(1,2)*P(5);
P(3)-Pworstrating(1,3)*P(5);
P(4)-Pworstrating(1,5)*P(5);
P(5)-Pworstrating(1,6)*P(5);
P(6)-Pworstrating(1,7)*P(5);
P(7)-Pworstrating(1,8)*P(5);
P(8)-Pworstrating(1,9)*P(5);
P(9)-Pworstrating(1,1)*P(5)])
ans =
0.207
And that is SIGNIFICANTLY larger than the result I found. For that set in the vector P, if chi is any lower than 0.207, then at least one of those constraints would fail to be satisfied. This is why I used max there. For example, with that vector P, and chi just slightly smaller...
P = [0.048 0.077 0.096 0.192 0.027 0.055 0.128 0.315 0.064];
[P(1)-Pbestrating(1,1)*P(1);
P(1)-Pbestrating(1,2)*P(2);
P(1)-Pbestrating(1,3)*P(3);
P(1)-Pbestrating(1,4)*P(4);
P(1)-Pbestrating(1,5)*P(5);
P(1)-Pbestrating(1,6)*P(6);
P(1)-Pbestrating(1,7)*P(7);
P(1)-Pbestrating(1,8)*P(8);
P(1)-Pbestrating(1,9)*P(9) ;
P(1)-Pworstrating(1,1)*P(5);
P(2)-Pworstrating(1,2)*P(5);
P(3)-Pworstrating(1,3)*P(5);
P(4)-Pworstrating(1,5)*P(5);
P(5)-Pworstrating(1,6)*P(5);
P(6)-Pworstrating(1,7)*P(5);
P(7)-Pworstrating(1,8)*P(5);
P(8)-Pworstrating(1,9)*P(5);
P(9)-Pworstrating(1,1)*P(5)] <= 0.206
ans =
18×1 logical array
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
Do you see that here one of those constraints now fails? My point is, what you claim to be the known solution is not in fact a solution that minimizes chi. The solution I found was considerably better., resulting in a considerably lower value for chi.
That implies either you have shown us the wrong set of equations, OR the vectors Pbestrating and Pworstrating were not the same, as those which would have generated that solution. I'm sorry, but there is really no alternative. That which you claim to be the solution is in fact far worse as a solution than what I generated. chi is much larger for that set. And that implies you have made a mistake in some way in what you have told us. It is of course, concievable that I copied your constraints incorrectly, but I just copied the entire block of code from what you wrote, so that is unlikely.
참고 항목
카테고리
Help Center 및 File Exchange에서 Financial Data Analytics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!