sum_1 is not assigned before it is needed (for loop does not execute immediately). Recommend assigning sum_1 = 0 outside the while loop.
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 个)
另请参阅
类别
在 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!