Gauss Seidel Method problem
조회 수: 2 (최근 30일)
이전 댓글 표시
I have to write two codes one for Jacobi and one for Gauss Schiedel
For the Gauss Schiedel one I coded
clc
clear
a=[1.2528,-1.4031,-2.45;0,3.3659,-0.2;1.6094,-3.8357,-5];
b=[-0.56,1.18,-6.37]';
x=[0,0,0]';
err=9000;
tol=0.01;
n=length(x);
while err>tol
x_1=x;
for i=1:n
sum=0;
for j=1:i-1
sum_1=sum+a(i,j)*x(j);
end
for j=i:n
sum_2=sum+a(i,j)*x_1(j);
end
x(i)=1/a(i,i)*(b(i)-(sum_1)-sum_2);
end
end
and in the error it says Unrecognized function or variable 'sum_1' for x(i)=1/a(i,i)*(b(i)-(sum_1)-sum_2); despite the fact that it is clearly stated before that there is a sum_1. Why is matlab not recognising the variable?
댓글 수: 0
답변 (1개)
David Hill
2019년 11월 29일
sum_1 is not assigned before it is needed (for loop does not execute immediately). Recommend assigning sum_1 = 0 outside the while loop.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!