my code does not have an answer

5 ビュー (過去 30 日間)
marjan mashayekhpour
marjan mashayekhpour 2024 年 12 月 31 日
回答済み: John D'Errico 2025 年 1 月 1 日
syms a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11
k1=-184.4;
k2=-207.5;
k3=-212.07;
k4=-69.8;
k5=-186.9;
k6=-367.5;
eq1=a1+a4-1;
eq2=2*a2+a5+2*a8-4;
eq3=2*a3+a6+a10-18.8;
eq4=a8^2*a7-k1*a2^2*a11;
eq5=2*a1+a2+a4+a5+a6+2*a7+a9-5;
eq6=a4^2*a7-k2*a1^2*a11;
eq7=a8*a5^2-k3*a2^2*a11;
eq8=a6^2-k4*a3*a7;
eq9=a9^2-k5*a7*a11;
eq10=a10^2-k6*a3*a11;
eq11=a1+a2+a3+a4+a5+a6+a7+a8+a9+a10-a11;
s=solve(eq1,eq2,eq3,eq4,eq5,eq6,eq7,eq8,eq9,eq10,eq11)
disp eq1,eq2,eq3,eq4,eq5,eq6,eq7,eq8,eq9,eq10,eq11
  3 件のコメント
Walter Roberson
Walter Roberson 2024 年 12 月 31 日
s=solve([eq1,eq2,eq3,eq4,eq5,eq6], [a1, a2, a10, a11, a4,a5,a6],'MaxDegree',4)
takes a bit of time but eventually calculates 8 solutions, each of which is very long.
Going much beyond that to additional equations and variables is computationally not very feasible.
Walter Roberson
Walter Roberson 2025 年 1 月 1 日
Trying
s=solve([eq1,eq2,eq3,eq4,eq5,eq6,eq7], [a1, a2, a10, a11, a4,a5,a6,a7],'MaxDegree',4)
eventually told me no solutions found.

サインインしてコメントする。

回答 (2 件)

Manikanta Aditya
Manikanta Aditya 2024 年 12 月 31 日
編集済み: Manikanta Aditya 2024 年 12 月 31 日
The reason why you did not see any output for the above code as it sounds like the system of equations is quite complex, leading to long computation times.
I have modified the code as below, this gives output and as expected.
% Define the equations as a function
function F = myEquations(x)
k1 = -184.4;
k2 = -207.5;
k3 = -212.07;
k4 = -69.8;
k5 = -186.9;
k6 = -367.5;
F(1) = x(1) + x(4) - 1;
F(2) = 2*x(2) + x(5) + 2*x(8) - 4;
F(3) = 2*x(3) + x(6) + x(10) - 18.8;
F(4) = x(8)^2 * x(7) - k1 * x(2)^2 * x(11);
F(5) = 2*x(1) + x(2) + x(4) + x(5) + x(6) + 2*x(7) + x(9) - 5;
F(6) = x(4)^2 * x(7) - k2 * x(1)^2 * x(11);
F(7) = x(8) * x(5)^2 - k3 * x(2)^2 * x(11);
F(8) = x(6)^2 - k4 * x(3) * x(7);
F(9) = x(9)^2 - k5 * x(7) * x(11);
F(10) = x(10)^2 - k6 * x(3) * x(11);
F(11) = x(1) + x(2) + x(3) + x(4) + x(5) + x(6) + x(7) + x(8) + x(9) + x(10) - x(11);
end
% Initial guesses
initial_guesses = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
% Solve the system of equations
options = optimoptions('fsolve', 'Display', 'iter', 'MaxIterations', 1000);
[x, fval, exitflag] = fsolve(@myEquations, initial_guesses, options);
Norm of First-order Trust-region Iteration Func-count ||f(x)||^2 step optimality radius 0 12 299674 2.93e+05 1 1 24 24715.3 1 5.38e+04 1 2 36 2430.42 2.5 7.46e+03 2.5 3 37 2430.42 6.25 7.46e+03 6.25 4 49 1572.58 1.5625 1.39e+03 1.56 5 61 1229.18 1.5625 335 1.56 6 73 1199.23 1.5625 1.76e+03 1.56 7 74 1199.23 1.5625 1.76e+03 1.56 8 86 937.66 0.390625 923 0.391 9 98 856.939 0.390625 657 0.391 10 110 680.61 0.390625 741 0.391 11 122 519.46 0.976562 2.17e+03 0.977 12 123 519.46 2.44141 2.17e+03 2.44 13 135 371.219 0.610352 596 0.61 14 147 342.446 1.52588 1.81e+03 1.53 15 159 303.039 1.52588 864 1.53 16 171 297.634 1.52588 1.92e+03 1.53 17 172 297.634 1.52588 1.92e+03 1.53 18 184 278.679 0.38147 932 0.381 19 185 278.679 0.953674 932 0.954 20 197 270.121 0.238419 1.08e+03 0.238 21 198 270.121 0.596046 1.08e+03 0.596 22 210 265.45 0.149012 479 0.149 23 222 262.987 0.149012 564 0.149 24 234 262.026 0.149012 234 0.149 25 246 261.229 0.149012 321 0.149 26 258 260.917 0.149012 145 0.149 27 270 260.589 0.149012 214 0.149 28 282 260.489 0.149012 73.5 0.149 29 294 260.239 0.149012 234 0.149 30 306 260.081 0.149012 71.8 0.149 31 318 259.323 0.149012 518 0.149 32 330 258.621 0.149012 66.2 0.149 33 342 257.258 0.149012 313 0.149 34 354 256.644 0.149012 203 0.149 35 366 256.515 0.149012 132 0.149 36 378 256.305 0.149012 179 0.149 37 379 256.305 0.149012 179 0.149 38 391 256.165 0.0372529 63.8 0.0373 39 403 256.077 0.0372529 144 0.0373 40 415 255.987 0.0372529 55.3 0.0373 41 427 255.927 0.0372529 136 0.0373 42 439 255.846 0.0372529 54.8 0.0373 43 451 255.792 0.0372529 134 0.0373 44 463 255.714 0.0372529 54.2 0.0373 45 475 255.662 0.0372529 131 0.0373 46 487 255.585 0.0372529 53.7 0.0373 47 499 255.535 0.0372529 129 0.0373 48 511 255.461 0.0372529 53.2 0.0373 49 523 255.413 0.0372529 126 0.0373 50 535 255.341 0.0372529 52.7 0.0373 51 547 255.294 0.0372529 123 0.0373 52 559 255.224 0.0372529 52.2 0.0373 53 571 255.179 0.0372529 121 0.0373 54 583 255.111 0.0372529 51.7 0.0373 55 595 255.067 0.0372529 119 0.0373 56 607 255.001 0.0372529 51.2 0.0373 57 619 254.957 0.0372529 117 0.0373 58 631 254.894 0.0372529 50.8 0.0373 59 643 254.851 0.0372529 115 0.0373 60 655 254.789 0.0372529 50.3 0.0373 61 667 254.748 0.0372529 113 0.0373 62 679 254.687 0.0372529 49.9 0.0373 63 691 254.646 0.0372529 111 0.0373 64 703 254.587 0.0372529 49.5 0.0373 65 715 254.547 0.0372529 110 0.0373 66 727 254.49 0.0372529 49.1 0.0373 67 739 254.45 0.0372529 108 0.0373 68 751 254.395 0.0372529 48.6 0.0373 69 763 254.356 0.0372529 107 0.0373 70 775 254.301 0.0372529 48.5 0.0373 71 787 254.263 0.0372529 106 0.0373 72 799 254.209 0.0372529 48.4 0.0373 73 811 254.172 0.0372529 104 0.0373 74 823 254.12 0.0372529 48.4 0.0373 75 835 254.082 0.0372529 103 0.0373 76 847 254.032 0.0372529 48.3 0.0373 77 859 253.995 0.0372529 102 0.0373 78 871 253.945 0.0372529 48.2 0.0373 79 883 253.909 0.0372529 101 0.0373 80 895 253.86 0.0372529 48.2 0.0373 81 907 253.824 0.0372529 100 0.0373 82 919 253.777 0.0372529 48.1 0.0373 83 931 253.741 0.0372529 99 0.0373 84 943 253.695 0.0372529 48 0.0373 85 955 253.66 0.0372529 98.1 0.0373 86 967 253.614 0.0372529 47.9 0.0373 87 979 253.579 0.0372529 97.2 0.0373 88 991 253.535 0.0372529 47.7 0.0373 89 1003 253.5 0.0372529 96.3 0.0373 90 1015 253.457 0.0372529 47.6 0.0373 91 1027 253.423 0.0372529 95.5 0.0373 92 1039 253.38 0.0372529 47.5 0.0373 93 1051 253.346 0.0372529 94.7 0.0373 94 1063 253.305 0.0372529 47.4 0.0373 95 1075 253.271 0.0372529 93.9 0.0373 96 1087 253.231 0.0372529 47.2 0.0373 97 1099 253.197 0.0372529 93.1 0.0373 98 1111 253.157 0.0372529 47.1 0.0373 Solver stopped prematurely. fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 1.100000e+03.
% Display the solutions
disp('Solutions:');
Solutions:
disp(x);
0.4016 0.3097 -0.0833 1.0009 1.9953 4.6653 3.4067 0.6657 -9.6718 1.9634 -0.1462
  • The equations are defined in a function myEquations.
  • Provided initial guesses to help the solver.
  • Used fsolve to solve the system numerically.
These are steps, I took to solve the equations. I hope this helps you.
  3 件のコメント
Manikanta Aditya
Manikanta Aditya 2025 年 1 月 1 日
Yeah, I agree.
John D'Errico
John D'Errico 2025 年 1 月 1 日
However, I notice that fsolve did not actually converge to a solution from that start point. There is no assurance a solution exists at all to any completely general system.
And that means you NEED to test what comes out.
function F = myEquations(x)
k1 = -184.4;
k2 = -207.5;
k3 = -212.07;
k4 = -69.8;
k5 = -186.9;
k6 = -367.5;
F(1) = x(1) + x(4) - 1;
F(2) = 2*x(2) + x(5) + 2*x(8) - 4;
F(3) = 2*x(3) + x(6) + x(10) - 18.8;
F(4) = x(8)^2 * x(7) - k1 * x(2)^2 * x(11);
F(5) = 2*x(1) + x(2) + x(4) + x(5) + x(6) + 2*x(7) + x(9) - 5;
F(6) = x(4)^2 * x(7) - k2 * x(1)^2 * x(11);
F(7) = x(8) * x(5)^2 - k3 * x(2)^2 * x(11);
F(8) = x(6)^2 - k4 * x(3) * x(7);
F(9) = x(9)^2 - k5 * x(7) * x(11);
F(10) = x(10)^2 - k6 * x(3) * x(11);
F(11) = x(1) + x(2) + x(3) + x(4) + x(5) + x(6) + x(7) + x(8) + x(9) + x(10) - x(11);
end
% Initial guesses
initial_guesses = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
% look at the objective function at the start point.
myEquations(initial_guesses)
ans = 1×11
1.0000 1.0000 -14.8000 185.4000 4.0000 208.5000 213.0700 70.8000 187.9000 368.5000 9.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Solve the system of equations
options = optimoptions('fsolve', 'Display', 'iter', 'MaxIterations', 1000);
[x, fval, exitflag] = fsolve(@myEquations, initial_guesses, options);
Norm of First-order Trust-region Iteration Func-count ||f(x)||^2 step optimality radius 0 12 299674 2.93e+05 1 1 24 24715.3 1 5.38e+04 1 2 36 2430.42 2.5 7.46e+03 2.5 3 37 2430.42 6.25 7.46e+03 6.25 4 49 1572.58 1.5625 1.39e+03 1.56 5 61 1229.18 1.5625 335 1.56 6 73 1199.23 1.5625 1.76e+03 1.56 7 74 1199.23 1.5625 1.76e+03 1.56 8 86 937.66 0.390625 923 0.391 9 98 856.939 0.390625 657 0.391 10 110 680.61 0.390625 741 0.391 11 122 519.46 0.976562 2.17e+03 0.977 12 123 519.46 2.44141 2.17e+03 2.44 13 135 371.219 0.610352 596 0.61 14 147 342.446 1.52588 1.81e+03 1.53 15 159 303.039 1.52588 864 1.53 16 171 297.634 1.52588 1.92e+03 1.53 17 172 297.634 1.52588 1.92e+03 1.53 18 184 278.679 0.38147 932 0.381 19 185 278.679 0.953674 932 0.954 20 197 270.121 0.238419 1.08e+03 0.238 21 198 270.121 0.596046 1.08e+03 0.596 22 210 265.45 0.149012 479 0.149 23 222 262.987 0.149012 564 0.149 24 234 262.026 0.149012 234 0.149 25 246 261.229 0.149012 321 0.149 26 258 260.917 0.149012 145 0.149 27 270 260.589 0.149012 214 0.149 28 282 260.489 0.149012 73.5 0.149 29 294 260.239 0.149012 234 0.149 30 306 260.081 0.149012 71.8 0.149 31 318 259.323 0.149012 518 0.149 32 330 258.621 0.149012 66.2 0.149 33 342 257.258 0.149012 313 0.149 34 354 256.644 0.149012 203 0.149 35 366 256.515 0.149012 132 0.149 36 378 256.305 0.149012 179 0.149 37 379 256.305 0.149012 179 0.149 38 391 256.165 0.0372529 63.8 0.0373 39 403 256.077 0.0372529 144 0.0373 40 415 255.987 0.0372529 55.3 0.0373 41 427 255.927 0.0372529 136 0.0373 42 439 255.846 0.0372529 54.8 0.0373 43 451 255.792 0.0372529 134 0.0373 44 463 255.714 0.0372529 54.2 0.0373 45 475 255.662 0.0372529 131 0.0373 46 487 255.585 0.0372529 53.7 0.0373 47 499 255.535 0.0372529 129 0.0373 48 511 255.461 0.0372529 53.2 0.0373 49 523 255.413 0.0372529 126 0.0373 50 535 255.341 0.0372529 52.7 0.0373 51 547 255.294 0.0372529 123 0.0373 52 559 255.224 0.0372529 52.2 0.0373 53 571 255.179 0.0372529 121 0.0373 54 583 255.111 0.0372529 51.7 0.0373 55 595 255.067 0.0372529 119 0.0373 56 607 255.001 0.0372529 51.2 0.0373 57 619 254.957 0.0372529 117 0.0373 58 631 254.894 0.0372529 50.8 0.0373 59 643 254.851 0.0372529 115 0.0373 60 655 254.789 0.0372529 50.3 0.0373 61 667 254.748 0.0372529 113 0.0373 62 679 254.687 0.0372529 49.9 0.0373 63 691 254.646 0.0372529 111 0.0373 64 703 254.587 0.0372529 49.5 0.0373 65 715 254.547 0.0372529 110 0.0373 66 727 254.49 0.0372529 49.1 0.0373 67 739 254.45 0.0372529 108 0.0373 68 751 254.395 0.0372529 48.6 0.0373 69 763 254.356 0.0372529 107 0.0373 70 775 254.301 0.0372529 48.5 0.0373 71 787 254.263 0.0372529 106 0.0373 72 799 254.209 0.0372529 48.4 0.0373 73 811 254.172 0.0372529 104 0.0373 74 823 254.12 0.0372529 48.4 0.0373 75 835 254.082 0.0372529 103 0.0373 76 847 254.032 0.0372529 48.3 0.0373 77 859 253.995 0.0372529 102 0.0373 78 871 253.945 0.0372529 48.2 0.0373 79 883 253.909 0.0372529 101 0.0373 80 895 253.86 0.0372529 48.2 0.0373 81 907 253.824 0.0372529 100 0.0373 82 919 253.777 0.0372529 48.1 0.0373 83 931 253.741 0.0372529 99 0.0373 84 943 253.695 0.0372529 48 0.0373 85 955 253.66 0.0372529 98.1 0.0373 86 967 253.614 0.0372529 47.9 0.0373 87 979 253.579 0.0372529 97.2 0.0373 88 991 253.535 0.0372529 47.7 0.0373 89 1003 253.5 0.0372529 96.3 0.0373 90 1015 253.457 0.0372529 47.6 0.0373 91 1027 253.423 0.0372529 95.5 0.0373 92 1039 253.38 0.0372529 47.5 0.0373 93 1051 253.346 0.0372529 94.7 0.0373 94 1063 253.305 0.0372529 47.4 0.0373 95 1075 253.271 0.0372529 93.9 0.0373 96 1087 253.231 0.0372529 47.2 0.0373 97 1099 253.197 0.0372529 93.1 0.0373 98 1111 253.157 0.0372529 47.1 0.0373 Solver stopped prematurely. fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 1.100000e+03.
fval
fval = 1×11
0.4025 -0.0539 -12.3378 -1.0752 0.9160 -1.4814 -0.3225 1.9610 0.4520 8.3300 4.7997
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
As you can see, while fsolve did manage to reduce the elements of the objective towards zero, it did not even come close to converging, stopping due to a limit on the number of iterations.
In fact, I tried it again, but now pushing the limits a bit wider, and it still fails to converge.
options = optimoptions('fsolve', 'Display', 'final', 'MaxIterations', 100000,'MaxFunction',100000);
[x, fval, exitflag] = fsolve(@myEquations, initial_guesses, options);
Solver stopped prematurely. fsolve stopped because it exceeded the function evaluation limit, options.MaxFunctionEvaluations = 1.000000e+05.
fval
fval = 1×11
-0.1386 -0.8963 -12.9144 -0.1150 0.6326 0.1956 0.2166 1.0254 0.1645 5.4649 3.0749
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Again, fsolve is not converging.
ALWAYS TEST a result. Did it converge? In this case, it appears there may well be no solution at all.

サインインしてコメントする。


John D'Errico
John D'Errico 2025 年 1 月 1 日
The failure of both vpasolve and fsolve suggests it is lkely no solutions exist at all for your problem. But merely the failure of those two codes is not conclusive. So I'll throw GA at it, to see if it can do any better.. A problem is, I don't know how far out the code must search. What are reasonable bounds?
function F = myEquations(x)
k1 = -184.4;
k2 = -207.5;
k3 = -212.07;
k4 = -69.8;
k5 = -186.9;
k6 = -367.5;
F(1) = x(1) + x(4) - 1;
F(2) = 2*x(2) + x(5) + 2*x(8) - 4;
F(3) = 2*x(3) + x(6) + x(10) - 18.8;
F(4) = x(8)^2 * x(7) - k1 * x(2)^2 * x(11);
F(5) = 2*x(1) + x(2) + x(4) + x(5) + x(6) + 2*x(7) + x(9) - 5;
F(6) = x(4)^2 * x(7) - k2 * x(1)^2 * x(11);
F(7) = x(8) * x(5)^2 - k3 * x(2)^2 * x(11);
F(8) = x(6)^2 - k4 * x(3) * x(7);
F(9) = x(9)^2 - k5 * x(7) * x(11);
F(10) = x(10)^2 - k6 * x(3) * x(11);
F(11) = x(1) + x(2) + x(3) + x(4) + x(5) + x(6) + x(7) + x(8) + x(9) + x(10) - x(11);
end
obj = @(x) norm(myEquations(x));
lb = repmat(-200,[1,11]);
ub = repmat(200,[1,11]);
options = optimoptions('ga', 'Display', 'final', 'MaxGenerations', 100000);
[xfinal,fval,exitflag] = ga(obj,11,[],[],[],[],lb,ub,[],options)
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
xfinal = 1×11
6.2669 16.0795 5.8288 -3.6840 -28.2627 7.1904 -0.1270 0.0019 -0.4366 -0.8565 -0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval = 3.9367
exitflag = 1
myEquations(xfinal)
ans = 1×11
1.5829 -0.0999 -0.8085 -0.7976 -1.8335 -1.8595 0.5870 0.0463 0.1910 0.6977 2.0008
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
As you can see, the best solution it was able to find is, while considerably better than what fsolve could find, does not in fact even come close to a solution for your problem.
As has been sugggested, there is likely no solution at all to this system of polynomial equations. Of course, it is possible that my bounds were too tight. that seems to be unlikely, since none of the unknowns come even close to the bounds I posed.
  1 件のコメント
Alex Sha
Alex Sha 2025 年 1 月 4 日
there is actually a stable solution, absolutely:
a1: 14.3999986585031
a2: 14.4000005201237
a3: 9.39999825064551
a4: -13.3999986585031
a5: -24.8000010402474
a6: 3.09085138251274E-6
a7: -1.40808571949123E-14
a8: -2.35734583921126E-15
a9: -1.22923077450232E-6
a10: 4.07857597673348E-7
a11: 3.28647435957887E-17
fval:
0
-4.88498130835069E-15
0
1.25665533876502E-12
-2.04281036531029E-14
-1.11428057950672E-12
-4.64031866173406E-15
3.14631965397327E-13
1.51100829698357E-12
1.87200495910687E-13
-1.93982956360012E-15

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEquation Solving についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by