Gauss Seidel Method problem

2 次查看(过去 30 天)
Maaz Madha
Maaz Madha 2019-11-29
回答: David Hill 2019-11-29
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?

回答(1 个)

David Hill
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.

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by