error: Index in position 2 exceeds array bounds (must not exceed 31).

1 vue (au cours des 30 derniers jours)
Seifeldin Mohamed
Seifeldin Mohamed le 2 Déc 2022
Commenté : Torsten le 2 Déc 2022
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
Index in position 2 exceeds array bounds. Index must not exceed 31.
%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')

Réponses (2)

VBBV
VBBV le 2 Déc 2022
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 commentaires
Seifeldin Mohamed
Seifeldin Mohamed le 2 Déc 2022
Modifié(e) : Seifeldin Mohamed le 2 Déc 2022
Thanks , but 0-0.3 should be in the y-axis and 0-0.4 in the x-axis. maybe the porblem in this line
what should I edit in the code ?
Torsten
Torsten le 2 Déc 2022
Modifié(e) : Torsten le 2 Déc 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

Connectez-vous pour commenter.


Torsten
Torsten le 2 Déc 2022
Modifié(e) : Torsten le 2 Déc 2022
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 commentaires
Torsten
Torsten le 2 Déc 2022
You know that the correctness of your code heavily depends on delta_x = delta_y ?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by