Index in position 1 exceeds array bounds. Index must not exceed 161. Error in Test (line 100) T(i,j)=(Tc​(i-1,j)+Tc​(i+1,j)+Tc​(i,j+1)+Tc​(i,j-1))/4​.;

4 views (last 30 days)
I think its the rows
round(a/dx+1);
Y = round(b/dy+1);
T = ones(X,Y)*30;
max_iteration = 10000;
error_max = 1e-6;
n = 1;
error = ones(1,max_iteration);
Tc=T;
while n < max_iteration && error(n) > error_max
n = n+1;
Tc = T;
for i=1:1:X
for j=1:1:Y
%boundary node 1, B1
if i>1 && i<X && j==Y
T(i,j)=+((Tc(i-1,j)+2*(Tc(i,j-1))+Tc(i+1,j)+(2*ha*dx*Ta)/kb)/(4+2*ha*dx)/kb)/2;
%boundary node 2, B2
elseif i==1 && j>(0.05/dx)+1 && j<Y
T(i,j)=+((kb*dy^2*Tc(i+1,j))+(kb*dx^2/2)*(Tc(i,j-1)+Tc(i,j+1)))/(kb*(dx^2+dy^2));
%boundary node 3, B3
elseif i==1 && j==(0.1/dx)+1
T(i,j)=Tc(i+1,j)/2+((kc*Tc(i,j-1))+kb*Tc(i,j+1))/2*(kc+kb);
%boundary node 4, B4
elseif i==1 && j<(0.1/dx)+1 &&j>(0.08/dx)+1
T(i,j)=Tc(i+1,j)/2+(Tc(i,j+1)+Tc(i,j-1))/4;
%boundary node 5, B5
elseif i>1 && j==(0.08/dx)+1 && i<(0.02/dx)+1
T(i,j)=((2*hw*dy*Tw/kc)+Tc(i+1,j)+Tc(i-1,j)+2*Tc(i,j+1))/((2+hw*dy/kc)+4);
%boundary node 6, B6
elseif i==(0.02/dx)+1 && j<(0.08/dx)+1 && j>(0.06/dx)+1
T(i,j)=(2*hw*dx*Tw/kc+Tc(i,j+1)+(2*Tc(i+1,j)+Tc(i,j-1)))/(4+2*hw*dx/kc);
%boundary node 7, B7
elseif i>1 && j==(0.06/dx)+1 && i<(0.02/dy)+1
T(i,j)=(2*hw*dy*Tw/kc+2*Tc(i,j-1)+(Tc(i+1,j))+Tc(i-1,j))/(4+2*hw*dy/kc);
%boundary node 8, B8
elseif i==1 && j<(0.06/dx)+1 && j>1
T(i,j)=(Tc(i+1,j)/2+(Tc(i,j+1)+Tc(i,j-1))/4);
%boundary node 9, B9
elseif i>1 && j==1 && i<X
T(i,j)=(2*Tc(i,j+1)+(Tc(i+1,j)+Tc(i-1,j)))/4;
%boundary node 10, B10
elseif i==X && j<(0.10/dx)+1 && j>1
T(i,j)=(Tc(i,j+1)+(Tc(i,j-1)+2*Tc(i-1,j)))/4;
%boundary node 11, B11
elseif i==X && j>(0.10/dx)+1 && j<Y
T(i,j)=(Tc(i,j+1)+(Tc(i,j-1)+2*Tc(i-1,j)))/2;
%boundary node 12, B12
elseif i==X && j==(0.10/dx)+1
T(i,j)=Tc(i-1,j)/2+(kc*Tc(i,j-1)+kb*Tc(i,j+1))/2*(kc+kb);
%boundary node 13, B13
elseif i>1 && j==(0.10/dx)+1 && i<X
T(i,j)=(((kc+kb)/2)*(Tc(i+1,j)+Tc(i-1,j))+kb*Tc(i,j+1) +kc*(Tc(i,j-1)))/2*(kc+kb);
%exterior corner, C1
elseif i==1 && j==Y
T(i,j)=(dx*ha*Ta/kb+(Tc(i+1,j)+Tc(i,j-1)))/(2+ha*dx/kb);
%exterior corner, C2
elseif i==1 && j==(0.08/dx)+1
T(i,j)=(dx*hw*Tw/k+(Tc(i+1,j)+Tc(i,j+1)))/(2+ha*dx/kc);
%exterior corner, C3
elseif i==(0.02/dx)+1 && j==(0.08/dx)+1
T(i,j)=(dx*hw*Tw/kc+(Tc(i+1,j)+Tc(i,j+1))+0.5*(Tc(i-1,j)+Tc(i,j-1)))/(3+hw*dx/kc);
%interior corner, C4
elseif i==(0.02/dx)+1 && j==(0.06/dx)+1
T(i,j)=(dx*hw*Tw/kc+(Tc(i+1,j)+Tc(i,j-1))+0.5*(Tc(i-1,j)+Tc(i,j+1)))/(3+hw*dx/kc);
%interior corner, C5
elseif i==1 && j==(0.06/dy)+1
T(i,j)=(dx*hw*Tw/kc+(Tc(i+1,j)+Tc(i,j-1)))/(2+hw*dx/kc);
%interior corner, C6
elseif i==1 && j==1
T(i,j)= (Tc(i+1,j)+Tc(i,j+1))/2;
%interior corner, C7
elseif i==X && j==1
T(i,j)=(Tc(i-1,j)+Tc(i,j+1))/2;
%exterior corner, C8
elseif i==X && j==X
T(i,j)=(dx*ha*Ta/kb+(Tc(i-1,j)+Tc(i,j-1)))/(2+ha*dx/kb);
%internal node w, Iw
elseif i>1 && i<(0.02/dx)+1 && j>(0.06/dx)+1 && j<(0.08/dy)+1
T(i,j)=Tw;
%internal node 1 and 2, I1/I2
else
T(i,j)=(Tc(i-1,j)+Tc(i+1,j)+Tc(i,j+1)+Tc(i,j-1))/4.;
end
end
end
differentmatrix = abs(T-Tc);
error(n) = max(differentmatrix(:));
end
n = n-1;
figure(1)
error = error(1:n);
plot(error,'b-','LineWidth',1);
ylimtic;
clc;
a = 0.16; %length of x
b = 0.13; %length of y
Ta = 20; % ambient temperature
Tw = 40; % temperature of water
hw = 100;% convective heat transfer coefficient for water and tube surface
ha = 5 ;% convective coefficient for floor surface and ambient
kb = 2 ;% convective coefficient for carpet B
kc = 0.5; %thermal conductivity for concrete
dx = 0.001;
dy = dx;
X = ([0,0.1]);
grid on;
drawnow;
title('Graph of error vs number of iteration-Jacobi method (5mm gridresolution)','FontSize',12);
xlabel('number of iteration','FontSize',12);
ylabel('error','FontSize',12);
xlim([0 n]);
Tc = T;
Tc = transpose(Tc);
figure(2)
pcolor(Tc);
colorbar;
title('Temperature distribution profile-Jacobi method (5mm gridresolution)','FontSize',12);
xlabel('width(mm)','FontSize',12);
ylabel('length(mm)','FontSize',12);
figure(3)
contourf(Tc,6);
colorbar;
title('Temperature distribution profile-Jacobi method (5mm gridresolution)','FontSize',12);
xlabel('width(mm)','FontSize',12);
ylabel('length(mm)','FontSize',12);
toc;

Answers (1)

Nipun
Nipun on 11 Oct 2023
Hi Danish,
I understand that you are encountering an index out of bounds error when iterating over an array of fixed size. Since the value of the variables are not provided in the attached code, I assume that they exceed the size of the 2 dimensional array (either the iterating variable is greater than the number of rows or the number of columns).
Note that in the attached code, the iterators i, j range from 1 to X and 1 to Y respectively (X,Y included). Since you are creating the matrix using X and Y scalars, X and Y denote the number of rows and columns respectively. In the while loop, you assign Tc=T. Therefore, they have the same size.
In the referenced line 100,
T(i,j)=(Tc (i-1,j)+Tc (i+1,j)+Tc (i,j+1)+Tc (i,j-1))/4
Since j iterates in the inner for loop, for each X, it completes it's iteration from 1 to Y. Observe that Y is the number of columns for both T and Tc. When i==X and j==Y, none of the if, else if conditions are true. Therefore else statement will be executed (line 100). At this iteration, you cannot index i+1 row and j+1 column. That is Tc (i+1,j)+Tc (i,j+1) cannot be evaluated. This line throws the mentioned error.
Hope this helps.
Regards,
Nipun

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!