left and right sides have a different number of elements

4 次查看(过去 30 天)
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 个评论
Torsten
Torsten 2023-3-22
编辑:Torsten 2023-3-22
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...

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2023-3-22
编辑:Matt J 2023-3-22
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

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by