left and right sides have a different number of elements

조회 수: 4 (최근 30일)
Jacob
Jacob 2023년 3월 21일
편집: Torsten 2023년 3월 22일
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