Why does Matlab have a different solution than mine? I tried other software, and they give me the same solution.
2 views (last 30 days)
Show older comments
Matteo Geraci
on 16 Jul 2020
Commented: Matteo Geraci
on 4 Aug 2020
%matrix A, and vector of constants b
A = [2 1 0 0 0; 1 1 -1 0 -1; -1 0 1 0 1; 0 -1 0 1 1; 0 1 1 1 1]
b = [100; 0; 50; 120; 0]
%augmented matrix
Ab = [A b]
%row reduction
[rowreducedAb, pivotvarsAb] = rref(Ab)
%free and basic variables
[numqns, numvars] = size(A)
[numrows, numpivotvars] = size(pivotvarsAb)
numfreevars = numvars - numpivotvars
%LU decomposition of A
[L,U] = lu(A)
y = L\b
%inverse of U
invU = inv(U)
%original solution
x = invU*y
%cramer's rule
A1 = A
A1(:,1) = b
x1 = det(A1)/det(A)
My Solution:
Matrix A Vector of unknows Vector of constants
2 1 0 0 0 x1 100
1 1 -1 0 -1 x2 0
-1 0 1 0 1 x3 50
0 -1 0 1 1 x4 120
0 1 1 1 1 x5 0
Fourth Step: Gaussian Eliminations;
B + C -> B x2 = 50
A - B -> A x1 = 25
D + E -> D
D - C -> D
D) x1 + x5 = 70 x5 = 45
C) -x1 + x3 + x5 = 50 x3 = 25 - 45 + 50 x3 = 30
E) x2 + x3 - x4 + x5 = 0 50 + 30 + 45 = x4 x4 = 125
0 Comments
Accepted Answer
Walter Roberson
on 16 Jul 2020
Back substitute on your proposed solution
>> A*[25;50;30;125;45]
ans =
100
0
50
120
250
The first four entries match b, but the last entry of b is 0 not 250. Your solution is incorrect.
>> A\b
ans =
25
50
-220
-125
295
>> A*[25;50;-220;-125;295]
ans =
100
0
50
120
0
MATLAB's solution works.
More Answers (0)
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!