error: Index in position 2 exceeds array bounds (must not exceed 31).
Show older comments
clear all
clc
%Geometry definition
L=0.3; %Length of the The rectangular bar in meter
W=0.4; %Width of the The rectangular bar in meter
t_max=10; %maximum time to get the solution for in sec
delta_t=0.2;
n_max=(t_max+delta_t)/delta_t;
%Material properties
alpha=11.234E-05; %theraml diffusivity
% computional details
i_max=31;
j_max=41;
delta_t=0.2;
delta_x=L/(i_max-1);
d=alpha*delta_t/(delta_x)^2;
delta_y=W/(j_max-1);
%Solution initializing
T=zeros(j_max,i_max);
T(1,2:i_max-1)= 40;
T(j_max,2:i_max-1)=10;
Tx=T;%taking the initial value
Ty=T;
%processing
for n=1:t_max
for i=2:i_max-1
for j=2:j_max-1
TT(i,j)=T(i,j)+d*(T(i-1,j)+T(i+1,j)+T(i,j-1)+T(i,j+1)-4*T(i,j));
end
end
i_max=31;
j_max=41;
%Boundary conditions
TT(j_max,2:i_max-1)=10; %upper
TT(1,2:i_max-1)= 40; %lower
T=TT;
n=n;
end
%plottting
solution_x=0:0.05:L;
solution_y=0:delta_y:W;
T_final = zeros(j_max,length(0:5:i_max));
T_final = T(:,1:5:i_max);
[X,Y] = meshgrid(solution_x,solution_y);
figure(1);
contourf(X,Y,T_final,'linecolor','non')
1 Comment
Seifeldin Mohamed
on 2 Dec 2022
Answers (2)
clear all
clc
%Geometry definition
L=0.3; %Length of the The rectangular bar in meter
W=0.4; %Width of the The rectangular bar in meter
t_max=10; %maximum time to get the solution for in sec
delta_t=0.2;
n_max=(t_max+delta_t)/delta_t;
%Material properties
alpha=11.234E-05; %theraml diffusivity
% computional details
i_max=31;
j_max=41;
delta_t=0.2;
delta_x=L/(i_max-1);
d=alpha*delta_t/(delta_x)^2;
delta_y=W/(j_max-1);
%Solution initializing
T=zeros(i_max,j_max);
T(1,2:j_max-1)= 40;
T(i_max,2:j_max-1)=10;
Tx=T;%taking the initial value
Ty=T;
i_max=31;
j_max=41;
%Boundary conditions
TT(i_max,2:j_max-1)=10; %upper
TT(1,2:j_max-1)= 40; %lower
%processing
for n=1:t_max
for i=2:i_max-1
for j=2:j_max-1
TT(i,j)=T(i,j)+d*(T(i-1,j)+T(i+1,j)+T(i,j-1)+T(i,j+1)-4*T(i,j));
T(i,j)=TT(i,j);
end
end
n=n;
end
%plottting
solution_x=linspace(0,L,i_max);
solution_y=linspace(0,W,j_max);
T_final = zeros(i_max,j_max);
T_final = T;
[X,Y] = meshgrid(solution_x,solution_y);
figure(1);
contourf(X,Y,T_final.')
colormap(jet)
4 Comments
Seifeldin Mohamed
on 2 Dec 2022
Askic V
on 2 Dec 2022
You created initial matrix T in the following way:
T=zeros(j_max,i_max);
which means total number of j_max rows and i_max columns, so 41x31
and yet inside the nested loop at: n = 1, i = 2, j = 31, you tried to use: ...+T(i,j+1)...
that is, you tried to acces the element with indices T(2, 32) which is greater than i_max (31).
Seifeldin Mohamed
on 2 Dec 2022
Edited: Seifeldin Mohamed
on 2 Dec 2022
Look at your coding. Clearly, L = 0.3 is the x-extension and W = 0.4 the y-extension.
L=0.3; %Length of the The rectangular bar in meter
W=0.4; %Width of the The rectangular bar in meter
% computional details
i_max=31;
j_max=41;
delta_x=L/(i_max-1);
delta_y=W/(j_max-1);
Maybe the settings here are not as wanted since these values are set left and right, not lower and upper:
TT(i_max,2:j_max-1)=10; %upper
TT(1,2:j_max-1)= 40; %lower
Note that I had to make changes to the boundary values and your loop ! Compare with your old code to see the errors you made.
clear all
clc
%Geometry definition
L=0.3; %Length of the The rectangular bar in meter
W=0.4; %Width of the The rectangular bar in meter
t_max=10; %maximum time to get the solution for in sec
delta_t=0.2;
n_max=(t_max+delta_t)/delta_t;
%Material properties
alpha=11.234E-05; %theraml diffusivity
% computional details
i_max=31;
j_max=41;
delta_t=0.2;
delta_x=L/(i_max-1);
d=alpha*delta_t/(delta_x)^2;
delta_y=W/(j_max-1);
%Solution initializing
T=zeros(i_max,j_max);
% Boundary conditions
T(2:i_max-1,1)= 40;
T(2:i_max-1,j_max)=10;
T(1,1) = 40/2;
T(i_max,1) = 40/2;
T(1,j_max) = 10/2;
T(i_max,j_max) = 10/2;
TT = T;
%processing
for n=1:t_max
for i=2:i_max-1
for j=2:j_max-1
TT(i,j)=T(i,j)+d*(T(i-1,j)+T(i+1,j)+T(i,j-1)+T(i,j+1)-4*T(i,j));
end
end
T = TT;
end
%plottting
solution_x=linspace(0,L,i_max);
solution_y=linspace(0,W,j_max);
T_final = zeros(i_max,j_max);
T_final = T;
[X,Y] = meshgrid(solution_x,solution_y);
figure(1);
contourf(X,Y,T_final.')
colorbar
colormap(jet)
2 Comments
Seifeldin Mohamed
on 2 Dec 2022
Torsten
on 2 Dec 2022
You know that the correctness of your code heavily depends on delta_x = delta_y ?
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!
