You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
while loop index exceeds array elements problem
4 views (last 30 days)
Show older comments
I am trying to run multiple iterations of this while loop to determine the reynolds number for various mass flow rates.. The first loop works perfectly, but when it gets to the second loop it gives me the error "Index exceeds array elements (1)" which is odd because I set up both loops the same way.. I tried reloading matlab but got the same
11 Comments
DGM
on 23 Mar 2021
Can you paste the actual text. I'm not eager to re-type everything from a picture and then guess which errors are caused by my own typos.
mason beal
on 23 Mar 2021
i will but the code has variable brought in so your code wont run regardless cause they wont be in your workspace.
while abs(errorF30N) > 10^(-6)
M2 = 2*L2/(1-BN);
CNF30N= 0.99 - (0.2262*BN^(4.1))-(0.00175*BN^2-0.0033*BN^4.125)*(10^6/REF30N(i))^1.15;
MFRF30N = CNF30N*KN*A3*sqrt(2*ADF30N*F30NV);
REF30N(i+1) = 4*MFRF30N/(3.14159*AV*D1);
errorF30N=REF30N(i+1)-REF30N(i);
i = i+1;
end
while abs(errorF40N) > 10^(-6)
M2 = 2*L2/(1-BN);
CNF40N= 0.99 - (0.2262*BN^(4.1))-(0.00175*BN^2-0.0033*BN^4.125)*(10^6/REF40N(i))^1.15;
MFRF40N = CNF40N*KN*A3*sqrt(2*ADF40N*F40NV);
REF40N(i+1) = 4*MFRF30N/(3.14159*AV*D1);
errorF40N=REF40N(i+1)-REF40N(i);
i = i+1;
end
mason beal
on 23 Mar 2021
This is the complete monster
%Extract Temperature Values
F0NT=sum(F0N{:,2})/16;
F30NT=sum(F30N{:,2})/17;
F40NT=sum(F40N{:,2})/17;
F50NT=sum(F50N{:,2})/19;
F70NT=sum(F70N{:,2})/15;
F80NT=sum(F80N{:,2})/18;
F0OT=sum(F0O{:,2})/23;
F30OT=sum(F30O{:,2})/20;
F40OT=sum(F40O{:,2})/17;
F50OT=sum(F50O{:,2})/19;
F70OT=sum(F70O{:,2})/17;
F80OT=sum(F80O{:,2})/20;
%Extract Pressure difference values in Pa
F0NV=sum(F0N{:,12}./3.47.*248.84)/16;
F30NV=sum(F30N{:,12}./3.47.*248.84)/17;
F40NV=sum(F40N{:,12}./3.47.*248.84)/17;
F50NV=sum(F50N{:,12}./3.47.*248.84)/19;
F70NV=sum(F70N{:,12}./3.47.*248.84)/15;
F80NV=sum(F80N{:,12}./3.47.*248.84)/18;
F0OV=sum(F0O{:,12}./3.47.*248.84)/23;
F30OV=sum(F30O{:,12}./3.47.*248.84)/20;
F40OV=sum(F40O{:,12}./3.47.*248.84)/17;
F50OV=sum(F50O{:,12}./3.47.*248.84)/19;
F70OV=sum(F70O{:,12}./3.47.*248.84)/17;
F80OV=sum(F80O{:,12}./3.47.*248.84)/20;
%% calculations
%Determine input variables
D1=input('what is the Duct Diameter? mm');
D2=input('What is the Oriface Diameter? mm');
D3=input('What is the Nozzle Diameter? mm');
P=input('What is the Barametric Pressure? kPa');
%Convert Diameters
D1= D1/1000; %m
D2= D2/1000; %m
D3=D3/1000;
%Convert pressure
P= P*1000; %Pa
T=22.67;
%Determine intermediate variables
R = 287; %J/kg*K
A3 = (pi*(D3^2))/4;
A1 = (pi*(D1^2))/4; %duct area
A2 = (pi*(D2^2))/4; %orifice area
B=D2/D1;
BN = D3/D1; %diameter ratio
K = 1/((1-(B^4))^(1/2)); %velocity of approach factor
KN = 1/((1-(BN^4))^(1/2)); %velocity of approach factor
ADF0N = P/(R*(F0NT+273)); %air density
ADF30N = P/(R*(F30NT+273)); %air density
ADF40N = P/(R*(F40NT+273)); %air density
ADF50N = P/(R*(F50NT+273)); %air density
ADF70N = P/(R*(F70NT+273)); %air density
ADF80N = P/(R*(F80NT+273)); %air density
ADF0O = P/(R*(F0OT+273)); %air density
ADF30O = P/(R*(F30OT+273)); %air density
ADF40O = P/(R*(F40OT+273)); %air density
ADF50O = P/(R*(F50OT+273)); %air density
ADF70O = P/(R*(F70OT+273)); %air density
ADF80O = P/(R*(F80OT+273)); %air density
AV= (-0.2454*(T^2) +474.4064*T +1.7295e+05)*(10^-10); %absolute viscosity
KV = AV/AD; %kinematic viscosity
e=2.71828;
%% Discharge Coefficients
%% Reynolds number, MFR, VFR
%loop setup
i = 1; %initialize
%Initialize
errorF0N=1;
errorF30N=1;
errorF40N=1;
errorF50N= 1;
errorF70N=1;
errorF80N=1;
errorF0O=1;
errorF30O=1;
errorF40O=1;
errorF50O=1;
errorF70O=1;
errorF80O=1;
REF0N(i) = 10^8; %Reynolds number approximation
REF30N(i)= 10^8; %Reynolds number approximation
REF40N(i) = 10^8; %Reynolds number approximation
REF50N(i) = 10^8; %Reynolds number approximation
REF70N(i) = 10^8; %Reynolds number approximation
REF80N(i) = 10^8; %Reynolds number approximation
REF0O(i) = 10^8; %Reynolds number approximation
REF30O(i)= 10^8; %Reynolds number approximation
REF40O(i) = 10^8; %Reynolds number approximation
REF50O(i) = 10^8; %Reynolds number approximation
REF70O(i) = 10^8; %Reynolds number approximation
REF80O(i)= 10^8; %Reynolds number approximation
L2 = 0;
L1 = 0;
%Reynolds number determination loop
while abs(errorF30N) > 10^(-6)
M2 = 2*L2/(1-BN);
CNF30N= 0.99 - (0.2262*BN^(4.1))-(0.00175*BN^2-0.0033*BN^4.125)*(10^6/REF30N(i))^1.15;
MFRF30N = CNF30N*KN*A3*sqrt(2*ADF30N*F30NV);
REF30N(i+1) = 4*MFRF30N/(3.14159*AV*D1);
errorF30N=REF30N(i+1)-REF30N(i);
i = i+1;
end
while abs(errorF40N) > 10^(-6)
M2 = 2*L2/(1-BN);
CNF40N= 0.99 - (0.2262*BN^(4.1))-(0.00175*BN^2-0.0033*BN^4.125)*(10^6/REF40N(i))^1.15;
MFRF40N = CNF40N*KN*A3*sqrt(2*ADF40N*F40NV);
REF40N(i+1) = 4*MFRF30N/(3.14159*AV*D1);
errorF40N=REF40N(i+1)-REF40N(i);
i = i+1;
end
while abs(errorF50N) > 10^(-6)
M2 = 2*L2/(1-BN);
CNF50N= 0.99 - (0.2262*BN^(4.1))-(0.00175*BN^2-0.0033*BN^4.125)*(10^6/REF50N(i))^1.15;
MFRF50N = CNF50N*KN*A3*sqrt(2*ADF50N*F50NV);
REF50N(i+1) = 4*MFRF50N/(3.14159*AV*D1);
errorF50N=REF50N(i+1)-REF50N(i);
i = i+1;
end
while abs(errorF70N) > 10^(-6)
M2 = 2*L2/(1-BN);
CNF70N= 0.99 - (0.2262*BN^(4.1))-(0.00175*BN^2-0.0033*BN^4.125)*(10^6/REF70N(i))^1.15;
MFRF70N = CNF70N*KN*A3*sqrt(2*ADF70N*F70NV);
REF70N(i+1) = 4*MFRF70N/(3.14159*AV*D1);
errorF70N=REF70N(i+1)-REF70N(i);
i = i+1;
end
while abs(errorF80N) > 10^(-6)
M2 = 2*L2/(1-BN);
CNF80N= 0.99 - (0.2262*BN^(4.1))-(0.00175*BN^2-0.0033*BN^4.125)*(10^6/REF80N(i))^1.15;
MFRF80N = CNF80N*KN*A3*sqrt(2*ADF80N*F80NV);
REF80N(i+1) = 4*MFRF80N/(3.14159*AV*D1);
errorF80N=REF80N(i+1)-REF80N(i);
i = i+1;
end
while abs(errorF0O) > 10^(-6)
M2 = 2*L2/(1-B);
CF0O = 0.5961 + (0.0261*B^2)-(0.216*B^8)+(0.000521*(((10^6*B)/REF0O(i))^.7))+((0.0188+0.0063*A1)*B^3.5*(10^6/REF0O(i))^0.3)+((0.043+0.08*e^(10*L1)-0.123*e^(-7*L1))*(1-11*A1)*(B^4/(1-B^4)))- 0.31*(M2-0.8*M2^1.1)*B^1.3;
MFRF0O = CF0O*K*A2*sqrt(2*ADF0O*F0OV);
REF0O(i+1) = 4*MFRF0O/(3.14159*AV*D1);
errorF0O=REF0O(i+1)-REF0O(i);
i = i+1;
end
while abs(errorF30O) > 10^(-6)
M2 = 2*L2/(1-B);
CF30O = 0.5961 + (0.0261*B^2)-(0.216*B^8)+(0.000521*(((10^6*B)/REF30O(i))^.7))+((0.0188+0.0063*A1)*B^3.5*(10^6/REF30O(i))^0.3)+((0.043+0.08*e^(10*L1)-0.123*e^(-7*L1))*(1-11*A1)*(B^4/(1-B^4)))- 0.31*(M2-0.8*M2^1.1)*B^1.3;
MFRF30O = CF30O*K*A2*sqrt(2*ADF30O*F30OV);
REF30O(i+1) = 4*MFRF30O/(3.14159*AV*D1);
errorF30O=REF30O(i+1)-REF30O(i);
i = i+1;
end
while abs(errorF40O) > 10^(-6)
M2 = 2*L2/(1-B);
CF40O = 0.5961 + (0.0261*B^2)-(0.216*B^8)+(0.000521*(((10^6*B)/REF40O(i))^.7))+((0.0188+0.0063*A1)*B^3.5*(10^6/REF40O(i))^0.3)+((0.043+0.08*e^(10*L1)-0.123*e^(-7*L1))*(1-11*A1)*(B^4/(1-B^4)))- 0.31*(M2-0.8*M2^1.1)*B^1.3;
MFRF40O = CF40O*K*A2*sqrt(2*ADF40O*F40OV);
REF40O(i+1) = 4*MFRF40O/(3.14159*AV*D1);
errorF40O=REF40O(i+1)-REF40O(i);
i = i+1;
end
while abs(errorF50O) > 10^(-6)
M2 = 2*L2/(1-B);
CF50O = 0.5961 + (0.0261*B^2)-(0.216*B^8)+(0.000521*(((10^6*B)/REF50O(i))^.7))+((0.0188+0.0063*A1)*B^3.5*(10^6/REF50O(i))^0.3)+((0.043+0.08*e^(10*L1)-0.123*e^(-7*L1))*(1-11*A1)*(B^4/(1-B^4)))- 0.31*(M2-0.8*M2^1.1)*B^1.3;
MFRF50O = CF50O*K*A2*sqrt(2*ADF50O*F50OV);
REF50O(i+1) = 4*MFRF50O/(3.14159*AV*D1);
errorF50O=REF50O(i+1)-REF50O(i);
i = i+1;
end
while abs(errorF70O) > 10^(-6)
M2 = 2*L2/(1-B);
CF70O = 0.5961 + (0.0261*B^2)-(0.216*B^8)+(0.000521*(((10^6*B)/REF70O(i))^.7))+((0.0188+0.0063*A1)*B^3.5*(10^6/REF70O(i))^0.3)+((0.043+0.08*e^(10*L1)-0.123*e^(-7*L1))*(1-11*A1)*(B^4/(1-B^4)))- 0.31*(M2-0.8*M2^1.1)*B^1.3;
MFRF70O = CF70O*K*A2*sqrt(2*ADF70O*F70OV);
REF70O(i+1) = 4*MFRF70O/(3.14159*AV*D1);
errorF70O=REF70O(i+1)-REF70O(i);
i = i+1;
end
while abs(errorF80O) > 10^(-6)
M2 = 2*L2/(1-B);
CF80O = 0.5961 + (0.0261*B^2)-(0.216*B^8)+(0.000521*(((10^6*B)/REF80O(i))^.7))+((0.0188+0.0063*A1)*B^3.5*(10^6/REF80O(i))^0.3)+((0.043+0.08*e^(10*L1)-0.123*e^(-7*L1))*(1-11*A1)*(B^4/(1-B^4)))- 0.31*(M2-0.8*M2^1.1)*B^1.3;
MFRF80O = CF80O*K*A2*sqrt(2*ADF80O*F80OV);
REF80O(i+1) = 4*MFRF80O/(3.14159*AV*D1);
errorF80O=REF80O(i+1)-REF80O(i);
i = i+1;
end
while abs(errorF0N) > 10^(-6)
M2 = 2*L2/(1-B);
CNF0N= 0.99 - (0.2262*BN^(4.1))-(0.00175*BN^2-0.0033*BN^4.125)*(10^6/REF0N(i))^1.15;
MFRF0N = CNF0N*KN*A3*sqrt(2*ADF0N*F0NV);
REF0N(i+1) = 4*MFRF0N/(3.14159*AV*D1);
errorF0N= REF0N(i+1)-REF0N(i);
i = i+1;
end
L_REF0N= length(REF0N);
L_REF30N= length(REF30N);
L_REF40N= length(REF40N);
L_REF50N= length(REF50N);
L_REF70N= length(REF70N);
L_REF80N= length(REF80N);
L_REF0O= length(REF0O);
L_REF30O= length(REF30O);
L_REF40O= length(REF40O);
L_REF50O= length(REF50O);
L_REF70O= length(REF70O);
L_REF80O= length(REF80O);
F_REF0N= REF0N(L_REF0N);
F_REF30N= REF30N(L_REF30N);
F_REF40N= REF40N(L_REF40N);
F_REF50N= REF50N(L_REF50N);
F_REF70N= REF70N(L_REF70N);
F_REF80N= REF80N(L_REF80N);
F_REF0O= REF0O(L_REF0O);
F_REF30O= REF30O(L_REF30O);
F_REF40O= REF40O(L_REF40O);
F_REF50O= REF50O(L_REF50O);
F_REF70O= REF70O(L_REF70O);
F_REF80O= REF80O(L_REF80O);
%calcs
unc = ((5/3)*B-0.5)/100; %relative uncertainty
VFRF0N = MFRF0N/ADF0N; %volumetric flow rate in metric
VFRF30N = MFRF30N/ADF30N; %volumetric flow rate in metric
VFRF40N = MFRF40N/ADF40N; %volumetric flow rate in metric
VFRF50N = MFRF50N/ADF50N; %volumetric flow rate in metric
VFRF70N = MFRF70N/ADF70N; %volumetric flow rate in metric
VFRF80N = MFRF80N/ADF80N; %volumetric flow rate in metric
VFRF0O = MFRF0O/ADF0O; %volumetric flow rate in metric
VFRF30O = MFRF30O/ADF30O; %volumetric flow rate in metric
VFRF40O = MFRF40O/ADF40O; %volumetric flow rate in metric
VFRF50O = MFRF50O/ADF50O; %volumetric flow rate in metric
VFRF70O = MFRF70O/ADF70O; %volumetric flow rate in metric
VFRF80O = MFRF80O/ADF80O; %volumetric flow rate in metric
vel = VFR/A1; %average duct velocity
%% plot setup
NozP1 = [F0NV,F30NV,F40NV,F50NV,F70NV,F80NV];
OriP1 = [F0OV F30OV F40OV F50OV F70OV F80OV];
NozP=NozP1(1,:);
OriP=OriP1(1,:);
NozVFR1 = [VFRF0N VFRF30N VFRF40N VFRF50N VFRF70N VFRF80N];
OriVFR1 = [VFRF0O VFRF30O VFRF40O VFRF50O VFRF70O VFRF80O];
NozVFR=NozVFR1(1,:);
OriVFR=OriVFR1(1,:);
%% Plots
plot(NozVFR(1,:),NozP(1,:),'r*')
hold on
plot(OriVFR(1,:),OriP(1,:),'b+')
xlabel('Flowrate [m^3/s]')
ylabel('Change in Pressure [Pa]')
legend('Nozzle','Oriface')
title('Pressure V Flowrate')
DGM
on 23 Mar 2021
Is this supposed to be using MFRF40N instead of MFRF30N?
REF40N(i+1) = 4*MFRF30N/(3.14159*AV*D1);
DGM
on 23 Mar 2021
So it is supposed to be MFRF40N? Did that fix anything or change the behavior?
FWIW, I was only looking at the pattern of the code.
REF30N(i+1) = 4*MFRF30N/(3.14159*AV*D1);
...
REF40N(i+1) = 4*MFRF30N/(3.14159*AV*D1);
...
REF50N(i+1) = 4*MFRF50N/(3.14159*AV*D1);
...
REF70N(i+1) = 4*MFRF70N/(3.14159*AV*D1);
...
REF80N(i+1) = 4*MFRF80N/(3.14159*AV*D1);
If we're still stuck, you're going to have to find a way to see which variable/line number it's throwing the error at.
mason beal
on 23 Mar 2021
The main thing that confuses me is that it can run the line for MFRF30N with no problem, which has identical code to the next line. Yet it is still getting tripped up
mason beal
on 23 Mar 2021
DGM i see what you were saying now about the repeated MFRF30N I apologize i did need to change that thank you. after changing it I am still getting the same error.
mason beal
on 23 Mar 2021
I was able to figure it out.. The original while loop changed the i value to 8 so when the next tried to run it was stuck at 8. So I switched my variable to j then k etc. for the other loops. Thank you to everyone that helped I really appreciate it :).
Answers (1)
Veronica Taurino
on 23 Mar 2021
Edited: Veronica Taurino
on 23 Mar 2021
Did you check your variable "i" has been set up to your desired value in the second while? Because in the first while you are updating it, so that the final value of ''i'' from the first "while" will be the input "i" for the second while.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Tags
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)