Attempted to access U(2,0); index must be a positive integer or logical.

here is my Guass-seidal program for poisson equation, how will i overcome this error.
function U=Guassseidal(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

7 Comments

What is the point in carefully defining eight input arguments that you completely ignore?
actually firstly i want my program to be like that user input these eight arguments by himself, but then i moved to a specfic example. so i ignored them
Use the debugger to find where your code is passing a 0 to U as an index and change it so you no longer do it. As the error message very coherently says, indices must be positive integers or logicals. 0 is neither.
i have changed the indices from i-1 to i, and then further changes... I have also started loop from i=2 instead of i=1, but unfortunately it moves outside the grid....
When you do this kind of work, in any case in which you subtract something from your index, then the index must start 1 higher than the maximum that you subtract, so that after the subtraction the lowest value would be 1.
Likewise in any case in which you add something to an index, then the index must stop by that much lower than the maximum, so that after the addition you do not exceed the maximum.
It is sometimes necessary to have additional code to deal with the boundary conditions.
Yes you are absolutely right. Will you please tell me which additional code is used to deal with boundary conditions
I am not familiar enough with the mathematics to say what the boundary conditions should be. But it probably involves the Bc* variables.

Sign in to comment.

Answers (0)

Asked:

on 6 Mar 2020

Edited:

on 6 Mar 2020

Community Treasure Hunt

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

Start Hunting!