Having Trouble with index out of bounds transient conduction
Show older comments
* *%Aluminium Material Properties%
rho = 2700 ; %kg/m^3 Density
k = 170 ; %
cp = 910 ; %
alpha = k/(rho*cp); %
L_d = 0.0047; %m Diameter
L_x = 0.047; %m Length in x direction
L_r = L_d/2; %m Radius
n_r = 2; % Nodes in r direction
n_x = 40; % Nodes in x direction
del_x = L_x/(n_x-1); %m Spacing of Nodes
T_h = 27; %C Temperature of Hot Side
T_i = 19; %C Initial Temperature
t_2 = 180; %s Simulation Time
t(1) = 0; %s Initial Time
Fo = 0.25; % Fourier Number Stability 0.25 For 3D
del_t = (Fo*del_x^2)/alpha; % Calculate time step using Fourier Number for Stability
%Set Inital Matrices Temp
T(1:n_r,1:n_x) = T_i;
k = 1;
while t<t_2 % Time Loop
for r=1:n_r % Space Loop r direction
for x=1:n_x % Space Loop x direction
if x==1 % Boundary Condition First Node = 27 Celius
T(r,x,k+1) = 27;
elseif x>1 && x==n_x % Middle to End of Material
T(r,x,k+1) = alpha*((T(r,x+1,k)-2*T(r,x,k)+T(r,x-1,k))/n_x^2)+((T(r+1,x,k)-2*T(r,x,k)+T(r-1,x,k))/n_r^2) ;
end
end
end
t(k+1) = t(k)+del_t;
k = k+1;
end
Temp(1:n_r,1:n_x) = T(1:n_r,1:n_x,k-1);* *
These are the errors that I am getting
Attempt to grow array along ambiguous dimension.
Error in ==> Transient2D at 18 T(1:n_r,1:n_x) = T_i;
Attempted to access T(1,41,1); index out of bounds because size(T)=[2,40,2].
Error in Transient2D (line 33) T(r,x,k+1) = alpha*((T(r,x+1,k)-2*T(r,x,k)+T(r,x-1,k))/n_x^2)+((T(r+1,x,k)-2*T(r,x,k)+T(r-1,x,k))/n_r^2) ;
I am wondering why it is stating It is out of bounds as the size should be correct.
Please Help!!!
Answers (3)
Image Analyst
on 30 Mar 2014
This ran fine for me:
rho = 2700 ; %kg/m^3 Density
k = 170 ; %
cp = 910 ; %
alpha = k/(rho*cp); %
L_d = 0.0047; %m Diameter
L_x = 0.047; %m Length in x direction
L_r = L_d/2; %m Radius
n_r = 2; % Nodes in r direction
n_x = 40; % Nodes in x direction
del_x = L_x/(n_x-1); %m Spacing of Nodes
T_h = 27; %C Temperature of Hot Side
T_i = 19; %C Initial Temperature
t_2 = 180; %s Simulation Time
t(1) = 0; %s Initial Time
Fo = 0.25; % Fourier Number Stability 0.25 For 3D
del_t = (Fo*del_x^2)/alpha; % Calculate time step using Fourier Number for Stability
%Set Inital Matrices Temp
T(1:n_r,1:n_x) = T_i;
even the last line where you said the error was. Try running clearvars on the command line and try again.
But then I'm confused. If you got an error at that line, then how could you continue on and get two more errors at two other lines??? What value is x, x-1, and x+1? on those lines? Look at this code:
for x=1:n_x % Space Loop x direction
if x==1 % Boundary Condition First Node = 27 Celius
T(r,x,k+1) = 27;
elseif x>1 && x==n_x % Middle to End of Material
T(r,x,k+1) = alpha*((T(r,x+1,k)-2*T(r,x,k)+T(r,x-1,k))/n_x^2)+((T(r+1,x,k)-2*T(r,x,k)+T(r-1,x,k))/n_r^2) ;
end
You handle the x=1 boundary condition, but you are not handling the x=n_x boundary condition. Maybe you want
for x=1:n_x % Space Loop x direction
if x==1 || x == n_x % Boundary Condition First Node = 27 Celius
So you're checking both sides of the array.
1 Comment
Luke Cartwright
on 30 Mar 2014
Luke Cartwright
on 31 Mar 2014
Image Analyst
on 31 Mar 2014
0 votes
You can't have the 0th row of a matrix. The indexes are natural numbers (integers) starting at 1. 1,2,3,4,5,6, etc. Not 0. Figure out why r is 0 and fix it.
1 Comment
Luke Cartwright
on 31 Mar 2014
Categories
Find more on Programming 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!