left and right sides have a different number of elements

5 views (last 30 days)
im trying to solve a gauss-seidel method problem and im getting the "left and right sides have a different number of elements" error in the xn(Iter) lines. how do i fix this?
A = [10 -2 -5 30; -2 15 -5 -5; -5 -5 14 25]
C = [2 ; 1 ; 3]
Error = 100;
Thr = 1;
Iter = 0;
x1 = C(1,1);
x2 = C(2,1);
x3 = C(3,1);
while Iter<5
Iter = Iter+1;
x1(Iter) = (A(1,4) - A(1,2)*x2 - A(1,3)*x3)/A(1,1);
x2(Iter) = (A(2,4) - A(2,1)*x1 - A(2,3)*x3)/A(2,2);
x3(Iter) = (A(3,4) - A(3,1)*x1 - A(3,2)*x2)/A(3,3);
x1 = x1(Iter);
x2 = x2(Iter);
x3 = x3(Iter);
end
  1 Comment
Torsten
Torsten on 22 Mar 2023
Edited: Torsten on 22 Mar 2023
You cannot use x1,x2 and x3 as a scalar and simultaneously as an array. Make up a decision for one of the two ways.
And what about x4 ? Your matrix A is 3x4 ! I've never heard of Gauss-Seidel for non-square systems...

Sign in to comment.

Answers (1)

Matt J
Matt J on 22 Mar 2023
Edited: Matt J on 22 Mar 2023
A = [10 -2 -5 30; -2 15 -5 -5; -5 -5 14 25];
C = [2 ; 1 ; 3];
Error = 100;
Thr = 1;
Iter = 0;
x1 = C(1,1);
x2 = C(2,1);
x3 = C(3,1);
while Iter<5
Iter = Iter+1;
X1(Iter) = (A(1,4) - A(1,2)*x2 - A(1,3)*x3)/A(1,1);
X2(Iter) = (A(2,4) - A(2,1)*x1 - A(2,3)*x3)/A(2,2);
X3(Iter) = (A(3,4) - A(3,1)*x1 - A(3,2)*x2)/A(3,3);
x1 = X1(Iter);
x2 = X2(Iter);
x3 = X3(Iter);
end
X1,X2,X3
X1 = 1×5
4.7000 4.6152 5.1480 5.2490 5.4178
X2 = 1×5
0.9333 1.2457 1.5479 1.6460 1.7589
X3 = 1×5
2.8571 3.7976 3.8789 4.1771 4.2482

Categories

Find more on Language Fundamentals 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!