Having Trouble with index out of bounds transient conduction

* *%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)

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

These are my .m files. I am trying to say that the nodes from 2-40 are all initially 19 Celsius and that the first node = 27 Celsius. Then as time progresses through the simulation the nodes from x> to x==n_x should increase in temperature. The whole simulation should run for 180 seconds. The error that I keep receiving is ----
Attempted to access T(1,41,1); index out of bounds because
size(T)=[2,40,2].
Error in Transient2D (line 27)
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 do not understand why it states that the index is out of bounds.
This line should should make sure the first node is always 27
if x==1 % Boundary Condition First Node = 27 Celius
T(r,x,k+1) = 27;
Then this line should constantly loop until the end of simulation so that the new Temperature values are calculated from node 2 till end of the rod.
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
Hopefully this explains it a little bit better.

Sign in to comment.

I have seemed to fixed the issues I was having however I am now having different errors, wondered if you could help.
%Aluminium Material Properties%
clear all
close all
clc
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
h = 5;
%Set Inital Matrices Temp
T(1:n_r,1:n_x+2) = T_i;
Fo = alpha*del_t/(del_x.^2) ; % Fourier Number
Bi = h*del_x/k ; % Biot Number
if Fo > 0.25
fprintf('Fo Number to big, code unstable \n \n')
elseif Fo*(1-Bi) > 0.25
fprintf('Bi Number to big, code unstable')
end
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 % 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))/del_x.^2)+((T(r+1,x,k)-2*T(r,x,k)+T(r-1,x,k))/del_x.^2) ;
end
end
end
for r = 1:n_x
T(r,1,k+1) = T(r,3,k);
T(r,n_x+2,k+1) = T(r,n_x,k);
end
t(k) = k+del_t;
k = k+1;
end
Temp(1:n_r,1:n_x) = T(1:n_r,1:n_x,k-1);
The error that is occurring now is -----
??? Attempted to access T(0,2,1); index must be a positive integer or logical.
Error in ==> Transient2D at 39
T(r,x,k+1) =
alpha*((T(r,x+1,k)-2*T(r,x,k)+T(r,x-1,k))/del_x.^2)+((T(r+1,x,k)-2*T(r,x,k)+T(r-1,x,k))/del_x.^2)
;
Hopefully someone can help.
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.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 30 Mar 2014

Commented:

on 31 Mar 2014

Community Treasure Hunt

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

Start Hunting!