Gauss Seidel Method problem

5 visualizzazioni (ultimi 30 giorni)
Maaz Madha
Maaz Madha il 29 Nov 2019
Risposto: David Hill il 29 Nov 2019
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?

Risposte (1)

David Hill
David Hill il 29 Nov 2019
sum_1 is not assigned before it is needed (for loop does not execute immediately). Recommend assigning sum_1 = 0 outside the while loop.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by