I am making a program that implements the Gauss-Seidel method for the Poisson equation. Here the error occurs due to zeros and negative values in subscripts. Will you please guide me on how to overcome this. The code is
function U=GaussSeidel(n,m,a,b,c,d,tol,N)
n=3; %input('enter the value of n= ');
m=3; %input('enter the value of m= ');
a=0; % input('enter the value of a= ');
b=3; %input('enter the value of b= ');
c=0; %input('enter the value of c= ');
d=3; %input('enter the value of d= ');
tol=0.001; %input('enter the value of tolerance= ');
N=2; %input('enter the total number of iterations = ');
A=-10; %input('input F(x,y)::::if your fuction is variable use inline('') ');
Bc1=0;%input('enter the Boundary condition at U(x,c) ');
Bc2=0;%input('enter the Boundary condition at U(x,d) ');
Bc3=0;%input('enter the Boundary condition at U(a,y) ');
Bc4=0;%input('enter the Boundary condition at U(b,y) ');
h=(b-a)/n;
k=(d-c)/m;
for i=1:n-1
for j=1:m-1
x(i)=a+i*h;
y(i)=c+j*k;
end
end
for i=1:n-1
for j=1:m-1
U(i,j)=0;
end
end
for i=2:n-1
U(i,c)=Bc1;
U(i,d)=Bc2;
for j=1:m
U(a,j)=Bc3;
U(b,j)=Bc4;
end
end
alpha=2*(1+h^2/k^2);
K=1;
while K<=N
errchk=0;
for j=2:m-3
for i=2:n-3
S=((-h^2)*A+U(i-1,j)+U(i+1,j)+(0.5*alpha-1)*(U(i,j-1)+U(i,j+1)))/alpha;
if errchk<abs(S-U(i,j))
errchk=abs(S-U(i,j));
U(i,j)=S;
if errchk<=tol
end
for j=1:m-1
for i=1:n-1
disp(U)
% disp(succesfully complete with solution)
end
end
end
end
K=K+1;
end
% disp(number of iterations exceed, unsuccessful completion)
end
end
Basically I'm working on an example of Poisson equation, so the values of a
,b
,c
,d
etc. are predefined.