Why is my loop not working? "Index exceeds array bounds"

1 view (last 30 days)
Function:
function [ row, cp, k] = apd (T)
if T > 318 & T < 320
row = 1.11
cp = 1007
k = 0.02781
else if T > 316 & T< 318
row = 1.1135
cp= 1007
k = 0.027635
else if T > 314 & T < 316
row= 1.1205
cp= 1007
k= 0.02749
else if T > 312 & T <314
row= 1.1277
cp = 1006.5
k = 0.02734
else if T >310 & T < 312
row = 1.1349
cp= 1006
k= 0.02719
else if T >308 & T < 310
row = 1.142
cp = 1006
k = 0.02704
else if T > 306 & T < 308
row= 1.14975
cp= 1006
k=0.0269
else if T > 304 & T < 306
row = 1.1573
cp = 1006
k= 0.02682
else if T >302 & T < 304
row = 1.16495
cp = 1006
k = 0.02666
else if T >297 & T < 302
row = 1.17865
cp = 1006
k = 0.026345
end
end
end
end
end
end
end
end
end
end
end
This function supposedly generates 3 air properties for a range of temperatures.
now, for the upcoming code that monitors the change of temperature and the distance crossed as air enters a tube where heat transfer by convection occurs:
%%%%%% DEFINITIONS %%%%%%%
clear
Q_lpm=15;
Q(1) =(Q_lpm/1000)/60; %m3/s [INPUT]
diameter = 0.01; % m [INPUT]
t =0.01; % time step [INPUT]
viscosity = 1.95*10^-5; % N.s/m2
Pr = 0.702;
area = pi*(diameter)^2/4; %m2
xtraveled = 0
xtube = 1.5
xprev = 0
V(1) = Q(1)*t;
x(1) = V(1)/area;
T(1) = 30 + 273;
Twall = 25+273;
i = 0
%%%%% ITERATIONS %%%%%
while xtraveled < xtube
xprev =xtraveled
i= i+1
T = T(i)
[row,cp,k]=apd(T) %% generates new row, cp and k every iteration
mdot(i)= Q(i)*row; %% getting the mass flow rate at that specific row
velocity = Q(i) / area;
Re(i) = (row*velocity*diameter)/(viscosity) % if Re> 2300 - turbulent (calculated at that row)
Nu(i) = 0.023*(Re(i)^0.8)*(Pr^0.4) %% nusselts number at that row
h1(i) = Nu(i)*k /diameter %% Heat transfer coeefficient at each density
As(i) = pi*diameter*x(i) %% surface area at each distance crossed inside the tube
L(i) = (h1(i)*As(i)*(T(i)-Twall)*t)/(row*cp*V(i)) %% energy balance parameter
T(i+1) = - L(i) +T(i) %% the new temperature at the new control volume
Q(i+1) = mdot(i) /row %% the new flow with the new temperature (new density)
V (i+1) = Q(i+1) * t %% the new control volume of the new flow
x (i+1) = V(i+1) / area %% the new distance crossed inside the tube
xtraveled = xprev + x(i+1) %% total distance travelled by the flow is the previous travelled one plus the new x at each iteration
end
whenever i run the code, it stops at L(i) showing an error of "Index exceeds array bounds"
  1 Comment
Dyuman Joshi
Dyuman Joshi on 24 Sep 2022
Edited: Dyuman Joshi on 24 Sep 2022
You are overwriting the variable T in each iteration.
T = T(i) %over-writing
[row,cp,k]=apd(T)
change this to
t = T(i)
[row,cp,k]=apd(t)
%or directly
[row,cp,k]=apd(T(i))
and move "i=i+1;" line to below the apd function call line or at the bottom
Also, what would happen in the value of T is a boundary value.
For example - T = 302 / 304 / 306 ...
What will be the values of row, cp and k for these values?

Sign in to comment.

Answers (1)

VBBV
VBBV on 24 Sep 2022
Edited: VBBV on 24 Sep 2022
%%%%%% DEFINITIONS %%%%%%%
clear
Q_lpm=15;
Q(1) =(Q_lpm/1000)/60; %m3/s [INPUT]
diameter = 0.01; % m [INPUT]
t =0.01; % time step [INPUT]
viscosity = 1.95*10^-5; % N.s/m2
Pr = 0.702;
area = pi*(diameter)^2/4; %m2
xtraveled = 0
xtraveled = 0
xtube = 1.5
xtube = 1.5000
xprev = 0
xprev = 0
V(1) = Q(1)*t;
x(1) = V(1)/area;
T(1) = 30 + 273;
Twall = 25+273;
i = 0
i = 0
%%%%% ITERATIONS %%%%%
while (xtraveled < xtube)
xprev =xtraveled ;
i= i+1;
% T = T(i)
[row,cp,k]=apd(T(i)); %% generates new row, cp and k every iteration
mdot(i)= Q(i)*row; %% getting the mass flow rate at that specific row
velocity(i) = Q(i) / area;
Re(i) = (row*velocity(i)*diameter)/(viscosity); % if Re> 2300 - turbulent (calculated at that row)
Nu(i) = 0.023*(Re(i)^0.8)*(Pr^0.4) ;%% nusselts number at that row
h1(i) = Nu(i)*k /diameter; %% Heat transfer coeefficient at each density
As(i) = pi*diameter*x(i); %% surface area at each distance crossed inside the tube
L(i) = (h1(i)*As(i)*(T(i)-Twall)*t)/(row*cp*V(i)); %% energy balance parameter
T(i+1) = - L(i) +T(i); %% the new temperature at the new control volume
Q(i+1) = mdot(i) /row; %% the new flow with the new temperature (new density)
V (i+1) = Q(i+1) * t; %% the new control volume of the new flow
x (i+1) = V(i+1) / area; %% the new distance crossed inside the tube
xtraveled = xprev + x(i+1); %% total distance travelled by the flow is the previous travelled one plus the new x at each iteration
end
subplot(211); plot(T); subplot(212);plot(Re)
function [row, cp, k] = apd (T)
if T > 318 & T < 320
row = 1.11;
cp = 1007;
k = 0.02781;
else if T > 316 & T< 318
row = 1.1135;
cp= 1007;
k = 0.027635;
else if T > 314 & T < 316
row= 1.1205;
cp= 1007;
k= 0.02749;
else if T > 312 & T <314
row= 1.1277;
cp = 1006.5;
k = 0.02734;
else if T >310 & T < 312
row = 1.1349;
cp= 1006;
k= 0.02719;
else if T >308 & T < 310
row = 1.142;
cp = 1006;
k = 0.02704;
else if T > 306 & T < 308
row= 1.14975;
cp= 1006;
k=0.0269;
else if T > 304 & T < 306
row = 1.1573;
cp = 1006;
k= 0.02682;
else if T >302 & T < 304
row = 1.16495;
cp = 1006;
k = 0.02666;
else if T >297 & T < 302
row = 1.17865;
cp = 1006;
k = 0.026345;
else
return
end
end
end
end
end
end
end
end
end
end
end
  3 Comments
smith
smith on 24 Sep 2022
Edited: smith on 24 Sep 2022
Thank you very much, this has helped, but the whole point of the function isnt working, which is, at each iteration i want to change the values of row,cp and k for the corresponding temperature of the iteration.
VBBV
VBBV on 12 Nov 2022
The row, cp and k values change for corresponding termperature (iteration) using the below line
[row,cp,k]=apd(T(i)); % the function here returns a different row, cp and k value

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!