Index in position 1 is invalid- attempting to perform Laplace's equation

1 view (last 30 days)
I've been trying to use Laplace's equation to model the temperature gradient in the cross section of two tubes, here's the code:
[X,Y,T] = meshgrid(0:0.1:10, 0:0.1:10, zeros);
for k = 1:1000
for i = 1:100
for j = 1:100
if ((0.1*i)-6)^2 + ((0.1*j)-5)^2 <= 6.25
T(i,j) = 100;
end
if ((0.1*i)-5)^2 + ((0.1*j)-5)^2 <= 25
T(i,j) = 0;
end
T(i,j) = 0.25*(T((0.1*i)-1,(0.1*j))+T((0.1*i)+1,(0.1*j))+T((0.1*i),(0.1*j)-1)+T((0.1*i),(0.1*j)+1));
end
end
end
surf(X,Y,T)
Everything works fine before I insert line 11, and when I do, I get the error message
"Index in position 1 is invalid. Array indicies must be positive integers or logical values".
For the record, I've tried doing it without multiplying all the variables in the equation by 0.1, that was just one of my many attempts to get this to work.
What am I doing wrong, and what should I do to fix it?
  6 Comments
Tethys Robertson
Tethys Robertson on 19 Nov 2020
Here's a view from the top (note that I also removed the parts multiplying each variable by 0.1)
Tethys Robertson
Tethys Robertson on 19 Nov 2020
Edited: Tethys Robertson on 19 Nov 2020
Actually, I just noticed that there was a missing "+1" next to the "i" in the second term of the equation, but now I'm getting an error that says "Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters", highlighting the semicolon. This is the code I'm using:
T(i,j) = 0.25*(T((i-1),j)+T((i+1),j)+T(i,(j-1))+T(i,(j+1));
EDIT: never mind, I found the missing bracket and fixed that, but now I'm getting whatever this is:

Sign in to comment.

Answers (1)

Alan Stevens
Alan Stevens on 19 Nov 2020
In your loops, 0.1i and 0.1j will result in non integer values for most values of i and j. Indices in T(i,j) etc must be integers (>=1), so T(0.1*i-1,0.1j) could have indices that are non-integer (and even negative), so are inadmissable.

Community Treasure Hunt

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

Start Hunting!