left and right sides have a different number of elements

4 Ansichten (letzte 30 Tage)
Jacob
Jacob am 21 Mär. 2023
Bearbeitet: Torsten am 22 Mär. 2023
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 Kommentar
Torsten
Torsten am 22 Mär. 2023
Bearbeitet: Torsten am 22 Mär. 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...

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 22 Mär. 2023
Bearbeitet: Matt J am 22 Mär. 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

Kategorien

Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by