Attempted to access U(2,0); index must be a positive integer or logical.
Show older comments
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
Stephen23
on 6 Mar 2020
What is the point in carefully defining eight input arguments that you completely ignore?
Zuhaa Naz
on 6 Mar 2020
Adam
on 6 Mar 2020
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.
Zuhaa Naz
on 6 Mar 2020
Walter Roberson
on 6 Mar 2020
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.
Zuhaa Naz
on 6 Mar 2020
Walter Roberson
on 6 Mar 2020
Edited: Walter Roberson
on 6 Mar 2020
I am not familiar enough with the mathematics to say what the boundary conditions should be. But it probably involves the Bc* variables.
Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!