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

조회 수: 1 (최근 30일)
Seifeldin Mohamed
Seifeldin Mohamed 2022년 12월 2일
댓글: Torsten 2022년 12월 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(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')

답변 (2개)

VBBV
VBBV 2022년 12월 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
Seifeldin Mohamed
Seifeldin Mohamed 2022년 12월 2일
편집: Seifeldin Mohamed 2022년 12월 2일
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 2022년 12월 2일
편집: Torsten 2022년 12월 2일
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

댓글을 달려면 로그인하십시오.


Torsten
Torsten 2022년 12월 2일
편집: Torsten 2022년 12월 2일
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
Torsten
Torsten 2022년 12월 2일
You know that the correctness of your code heavily depends on delta_x = delta_y ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by