0

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.

MathMax
  • 571
  • 7
  • 22
  • 5
    So don't use zeros in the subscript... you define `c=0` and then try to index `U(i,c)`, what do you expect to happen here? Your question includes no information about expected behaviour... – Wolfie Mar 06 '20 at 16:35
  • You want to find out what is originating those unexpected zero or negative subscript indices, an excellent chance of learning the usage of [breakpoints](https://it.mathworks.com/help/matlab/matlab_prog/set-breakpoints.html). If you want to give fixed values to n,m,a,b etc, you should use a [script](https://it.mathworks.com/help/matlab/matlab_prog/create-scripts.html) and not a function. – MathMax Mar 10 '20 at 10:33

0 Answers0