solve: Cannot find explicit solution but it have solutions, matlab's bug?
Afficher commentaires plus anciens
this is my code:
syms x0 x1 x2 x3 x4;
cond1 = (1.00028*x0+1.00045*x1)>=1.00035*(x0+x1);
cond2 = ((1.00028*x0+1.00045*x1) + 1.00063*x2)>=1.00054*(x0+x1+x2);
cond3 = (((1.00028*x0+1.00045*x1) + 1.00063*x2)+1.00082*x3)>=1.00076*(x0+x1+x2+x3);
cond4 = ((((1.00028*x0+1.00045*x1) + 1.00063*x2)+1.00082*x3)+1.00101*x4)>=1.00091*(x0+x1+x2+x3+x4);
cond5 = x0+x1+x2+x3+x4>=0;
condconds = [cond1 cond2 cond3 cond4 cond5];
sol = solve(condconds, [x0 x1 x2 x3 x4])
Does my code have any error or matlab have bugs???
My solution :
x0 1
x1 1
x2 3.44
x3 19.94
x4 38.07
by the way, i just need integer x
6 commentaires
Sara Boznik
le 14 Août 2020
Your code looks fine. On my computer either does not work.
John D'Errico
le 14 Août 2020
Modifié(e) : John D'Errico
le 14 Août 2020
Actually, no. The code is not fine, at least not in terms of what MATLAB allows solve to handle. Solve is not designed to solve that class of probem. The problem has infinitely many solutions, and solve cannot solve problems with infinitely many solutions happily.
andrew wu
le 14 Août 2020
John D'Errico
le 14 Août 2020
Modifié(e) : John D'Errico
le 15 Août 2020
I showed exactly how to solve that problem in my answer! I guess I thought it would be clear that since your problem is linear and the system effectively homogeneous, then you can trivially scale that solution by any constant. I'll concede that may not have been obvious.
X = linprog(ones(5,1),-DA,zeros(5,1),[],[],ones(5,1))
Optimal solution found.
X =
1
1
3.88888888889285
21.5925925926071
41.2222222222452
Thus, if X is this vector, than ANY multiple of X is also a solution. This must be true, since your equations form a homogeneous system.
syms t
vpa(subs(condconds,[x0,x1,x2,x3,x4],t*X.'),10)
ans =
[ 2.0007*t <= 2.00073*t, 5.892068889*t <= 5.892068889*t, 27.50236741*t <= 27.50236741*t, 68.76622407*t <= 68.76622407*t, 0.0 <= 68.7037037*t]
The vector X*t is clearly a solution for any positive value of t.
andrew wu
le 15 Août 2020
andrew wu
le 15 Août 2020
Réponse acceptée
Plus de réponses (1)
Stephan
le 14 Août 2020
Congratulations, you have 1 solution of an infinite number of possible solutions - look to the borderline case of equal to:
syms x0 x1 x2 x3 x4;
assume([x0 x1 x2 x3 x4], {'real', 'positive'})
cond1 = (1.00028*x0+1.00045*x1)==1.00035*(x0+x1);
cond2 = ((1.00028*x0+1.00045*x1) + 1.00063*x2)==1.00054*(x0+x1+x2);
cond3 = (((1.00028*x0+1.00045*x1) + 1.00063*x2)+1.00082*x3)==1.00076*(x0+x1+x2+x3);
cond4 = ((((1.00028*x0+1.00045*x1) + 1.00063*x2)+1.00082*x3)+1.00101*x4)==1.00091*(x0+x1+x2+x3+x4);
cond5 = x0+x1+x2+x3+x4==0;
condconds = [cond1 cond2 cond3 cond4 cond5];
[A,~] = equationsToMatrix(condconds);
det_A = vpa(det(A))
leads to
det_A =
A =
[ -7/100000, 1/10000, 0, 0, 0]
[ -13/50000, -9/100000, 1266637395196663/14073748835532800000, 0, 0]
[ -3/6250, -31/100000, -1829587348620553/14073748835532800000, 3/50000, 0]
[ -8866461766385191/14073748835532800000, -1294784892868923/2814749767106560000, -78812993479/281474976710656, -1266637395197479/14073748835532800000, 450359962737/4503599627370496]
[ 1, 1, 1, 1, 1]
b =
0
0
0
0
0
det_A =
0.0000000000000033319999999983153682592275926204
which means, that the determinant of your system is zero in fact. Since solve is looking for a explicit solution it will not find any i guess.
3 commentaires
John D'Errico
le 14 Août 2020
Modifié(e) : John D'Errico
le 14 Août 2020
I'm sorry, but this is incorrect.
svd(double(A))
ans =
2.2361
0.00074952
0.00018996
0.00011237
9.3138e-05
A is not even remotely close to being a singular matrix. The condition number is not even that terribly large.
cond(double(A))
ans =
24008
Using the determinant to test for singularity is about as poor a solution as any I would ever recommend. Would you tell me that
B = eye(5)*0.001;
is a singular matrix? det seems to think it is, yet you should agree it is not.
det(B)
ans =
1e-15
NEVER use det to test a matrix for singularity, unless you are solving a homework assignment as directed. And even then, you should tell your teacher why it is such a bad idea to do so. Or let me tell them why.
andrew wu
le 18 Août 2020
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!