필터 지우기
필터 지우기

Correct and incorrect answer from linsolve

조회 수: 7 (최근 30일)
Oddur Bjarnason
Oddur Bjarnason 2016년 10월 14일
편집: Walter Roberson 2016년 10월 17일
The following gives a correct answer:
A=[-2/5,1/5;2/5,-1/5;1,1]
B=[0;0;1]
linsolve(A,B)
ans =0.3333 0.6667
The following, however, gives an incorrect answer:
C=[-0.2,0.3;0.25,-0.3;1,1]
D=[0;0;1]
C\D
ans = 0.5699 0.4297
The correct answer, found by substitution, is 0.6000 0.4000
How can I get a correct answer by linsolve?

답변 (1개)

Walter Roberson
Walter Roberson 2016년 10월 14일
x = sym('x',[2 1]);
C*x
ans =
(3*x2)/10 - x1/5
x1/4 - (3*x2)/10
x1 + x2
You have a system of 3 equations in 2 unknowns. It is overdetermined, and might not have any exact solution. The \ operation will do a least-squared fit to find an answer that is least bad in some sense.
Solving (3*x2)/10 - x1/5 = 0 for x1 gives x1 = (3*x2)/2. Substituting that back into C*x gives
0
(3*x2)/40
(5*x2)/2
solving (3*x2)/40 = 0 for x2 gives x2 = 0. Substituting that back gives (5*0)/2 = 1 which is 0 = 1 which has no solution.
>> C*[0.6;0.4]
ans =
0
0.03
1
So 0.6 0.4 is not a solution after-all.
>> sum((D-C*(C\D)).^2)
ans =
0.000407074042245239
>> sum((D-C*[0.6;0.4]).^2)
ans =
0.0009
so the solution found by C\D gives less of an error than [0.6 0.4] does.
  댓글 수: 6
Steven Lord
Steven Lord 2016년 10월 17일
Now the cause of the problem is clear. You wrote:
This yields the following system of linear equations:
0.8*SS1+0.3*SS2=SS1 0.25*SS1+0.7*SS2=SS2 SS1+SS2=1
You have a typo in your second equation. You wrote 0.25 instead of 0.2. So linsolve is solving the problem you told it to solve, not the problem you wanted it to solve. When I write the correct coefficient matrix, both linsolve and backslash (\) return the answer you expected.
A = [0.8-1 0.3; 0.2 0.7-1; 1 1];
b = [0; 0; 1];
x1 = linsolve(A, b)
x2 = A\b
Since you're interested in Markov chains you may find the PageRank and Markov Chains section of the chapter on Linear Equations in Cleve's Numerical Computing in MATLAB interesting and informative.
Oddur Bjarnason
Oddur Bjarnason 2016년 10월 17일
Thank you Steven, When I was filling in the coefficient Matrix I read 0.2S1 to be 0.25. So you are right - it was a typo.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by