Dynamic array storage with for loop

D = 0.067;
Ugs = [0.047,0.061,0.288,0.344,0.404,0.544,0.709,0.945,1.418,1.891,2.363,2.836,4.727];
Uls = ones(1,13)*0.047;
Uls1 = ones(1,13)*0.071;
Uls2 = ones(1,13)*0.095;
Uls3 = ones(1,13)*0.142;
g = 9.81;
rho_gas=1.18;
rho_liq=900;
Ugs_aht= Ugs *((rho_gas).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
Uls_aht= Uls *((rho_liq).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
Uls1_aht= Uls1 *((rho_liq).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
Uls2_aht= Uls2 *((rho_liq).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
Uls3_aht= Uls3 *((rho_liq).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
print(Uls_aht, Uls1_aht,Uls2_aht,Uls3_aht)
C = ((Ugs_aht).^0.5) + ((Uls_aht).^0.5);
C1 = ((Ugs_aht).^0.5) + ((Uls1_aht).^0.5);
C2 = ((Ugs_aht).^0.5) + ((Uls2_aht).^0.5);
C3 = ((Ugs_aht).^0.5) + ((Uls3_aht).^0.5);
flow = ones(1,13);
flow = string('flow');
for i = 1:13
if C <= 0.2(i)
flow(i)= 'Bubble'
if C1 <= 0.89(i)
flow(i)= 'Slug'
if C2 <= 0.98(i)
flow(i)= 'Transition'
if C3 >= 1.00(i)
flow(i)= 'Churn'
end
end
end
end
% end
Ugs = Ugs.';
Usl = Usl.';
Usl1 = Usl1.';
Usl2 = Usl2.';
Usl3 = Usl3.';
C = C.';
C1 = C1.';
C2 = C2.';
C3 = C3.';
Uls_aht = Uls_aht.';
Uls1_aht = Uls1_aht.';
Uls2_aht = Uls2_aht.';
Uls3_aht = Uls3_aht.';
% flow = flow.';
T = table(Ugs,Uls_aht,Uls1_aht,Uls2_aht,Uls3_aht,C,C1,C2,C3)

 Risposta accettata

D = 0.067;% Diameter of tube in meters
Ugs = [0.047,0.061,0.288,0.344,0.404,0.544,0.709,0.945,1.418,1.891,2.363,2.836,4.727];
Uls = ones(1,13)*0.047;
Uls1 = ones(1,13)*0.071;
Uls2 = ones(1,13)*0.095;
Uls3 = ones(1,13)*0.142;
Uls4 = ones(1,13)*0.284;
Uls5 = ones(1,13)*0.378;
g = 9.81;% Acceleartion due to gravity (m/s^2)
rho_gas=1.18;% Gas density (Kg/m^3)
rho_liq=900;% Liquid density (Kg/m^3)
for i = 1:13
Ugs_aht(i)= Ugs(i) *((sqrt(rho_gas))/(sqrt(g * D *(rho_liq - rho_gas))));
end
Uls_aht= Uls *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
Uls_aht1= Uls1 *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
Uls_aht2 = Uls2 *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
Uls_aht3 = Uls3 *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
Uls_aht4 = Uls4 *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
Uls_aht5 = Uls5 *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
C = (sqrt(Ugs_aht)) + (sqrt(Uls_aht));
C1 = (sqrt(Ugs_aht)) + (sqrt(Uls_aht1));
C2 = (sqrt(Ugs_aht)) + (sqrt(Uls_aht2));
C3 = (sqrt(Ugs_aht)) + (sqrt(Uls_aht3));
C4 = (sqrt(Ugs_aht)) + (sqrt(Uls_aht4));
C5 = (sqrt(Ugs_aht)) + (sqrt(Uls_aht5));
Ugs =Ugs.';
Uls = Uls.';
Uls1 = Uls1.';
Uls2 = Uls2.';
Uls3 = Uls3.';
Uls4 = Uls4.';
Uls5 = Uls5.';
Ugs_aht = Ugs_aht.';
Uls_aht = Uls_aht.';
Uls_aht1 = Uls_aht1.';
Uls_aht2 = Uls_aht2.';
Uls_aht3 = Uls_aht3.';
Uls_aht4 = Uls_aht4.';
Uls_aht5 = Uls_aht5.';
C = C.';
C1 = C1.';
C2 = C2.';
C3 = C3.';
C4 = C4.';
C5 = C5.';
plot(C,Ugs_aht,'--r')
xlabel('C (sqrt(m/s))')
ylabel('Ugs_aht (m/s)')
title('Flooding Mechanism')
T= table(Ugs, Uls,Uls_aht, Ugs_aht, C,C1,C2,C3,C4,C5);
hold on
plot(C1,Ugs_aht,'--b')
hold on
plot(C2,Ugs_aht,'--g')
hold on
plot(C3,Ugs_aht,'--y')
hold on
plot(C4,Ugs_aht,'--c')
hold on
plot(C5,Ugs_aht,'--k')

Più risposte (2)

Ameer Hamza
Ameer Hamza il 18 Mag 2020
Modificato: Ameer Hamza il 18 Mag 2020
There are several syntax errors in your code. Compare it with the following code to see the issues
D = 0.067;
Ugs = [0.047,0.061,0.288,0.344,0.404,0.544,0.709,0.945,1.418,1.891,2.363,2.836,4.727];
Uls = ones(1,13)*0.047;
Uls1 = ones(1,13)*0.071;
Uls2 = ones(1,13)*0.095;
Uls3 = ones(1,13)*0.142;
g = 9.81;
rho_gas=1.18;
rho_liq=900;
Ugs_aht= Ugs *((rho_gas).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
Uls_aht= Uls *((rho_liq).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
Uls1_aht= Uls1 *((rho_liq).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
Uls2_aht= Uls2 *((rho_liq).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
Uls3_aht= Uls3 *((rho_liq).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
C = ((Ugs_aht).^0.5) + ((Uls_aht).^0.5);
C1 = ((Ugs_aht).^0.5) + ((Uls1_aht).^0.5);
C2 = ((Ugs_aht).^0.5) + ((Uls2_aht).^0.5);
C3 = ((Ugs_aht).^0.5) + ((Uls3_aht).^0.5);
flow = strings(1,13);
for i = 1:13
if C(i) <= 0.2
flow(i) = 'Bubble';
elseif C1(i) <= 0.89
flow(i) = 'Slug';
elseif C2(i) <= 0.98
flow(i) = 'Transition';
elseif C3(i) >= 1.00
flow(i) = 'Churn';
end
end
% end
Ugs = Ugs.';
Uls = Uls.';
Uls1 = Uls1.';
Uls2 = Uls2.';
Uls3 = Uls3.';
C = C.';
C1 = C1.';
C2 = C2.';
C3 = C3.';
Uls_aht = Uls_aht.';
Uls1_aht = Uls1_aht.';
Uls2_aht = Uls2_aht.';
Uls3_aht = Uls3_aht.';
% flow = flow.';
T = table(Ugs,Uls_aht,Uls1_aht,Uls2_aht,Uls3_aht,C,C1,C2,C3)
Result
T =
13×9 table
Ugs Uls_aht Uls1_aht Uls2_aht Uls3_aht C C1 C2 C3
_____ ________ ________ ________ ________ _______ _______ _______ _______
0.047 0.058011 0.087634 0.11726 0.17527 0.28669 0.34186 0.38826 0.46448
0.061 0.058011 0.087634 0.11726 0.17527 0.29307 0.34824 0.39464 0.47086
0.288 0.058011 0.087634 0.11726 0.17527 0.35431 0.40948 0.45588 0.5321
0.344 0.058011 0.087634 0.11726 0.17527 0.36485 0.42002 0.46642 0.54264
0.404 0.058011 0.087634 0.11726 0.17527 0.37523 0.4304 0.4768 0.55302
0.544 0.058011 0.087634 0.11726 0.17527 0.39678 0.45195 0.49835 0.57457
0.709 0.058011 0.087634 0.11726 0.17527 0.41886 0.47404 0.52044 0.59666
0.945 0.058011 0.087634 0.11726 0.17527 0.44636 0.50154 0.54794 0.62416
1.418 0.058011 0.087634 0.11726 0.17527 0.4926 0.54777 0.59417 0.67039
1.891 0.058011 0.087634 0.11726 0.17527 0.53157 0.58674 0.63314 0.70936
2.363 0.058011 0.087634 0.11726 0.17527 0.56583 0.621 0.6674 0.74362
2.836 0.058011 0.087634 0.11726 0.17527 0.59687 0.65205 0.69844 0.77467
4.727 0.058011 0.087634 0.11726 0.17527 0.70049 0.75566 0.80206 0.87828

4 Commenti

Temidayo BOBOYE
Temidayo BOBOYE il 19 Mag 2020
Modificato: Temidayo BOBOYE il 19 Mag 2020
Thanks for the inputs. However, the Ugs of 13 variables are to be used with each of the 4 variables of Uls( for each value of Uls 0.047,0.071,0.095 ,0.142 the corresponding 13 values of Ugs are to be used).The expected Ugs_aht should be 13 values each for the 4 values of Uls. I need to create a dynamic array that stores each value of Ugs_aht to determine the coresponding C, C1, C2 and C3.
I think a for loop might be need
for i=1:13
Ugs_aht= Ugs(i) *((rho_gas).^0.5)/(g * D *(rho_liq - rho_gas)).^0.5;
end
ndgrid() or meshgrid()
This loop should be able to store Ugs for each of the values of Uls and run the script and finally generate the table
D = 0.067;% Diameter of tube in meters
Ugs = [0.047,0.061,0.288,0.344,0.404,0.544,0.709,0.945,1.418,1.891,2.363,2.836,4.727];
Uls = ones(1,13)*0.047;
Uls1 = ones(1,13)*0.071;
Uls2 = ones(1,13)*0.095;
Uls3 = ones(1,13)*0.142;
Uls4 = ones(1,13)*0.284;
Uls5 = ones(1,13)*0.378;
g = 9.81;% Acceleartion due to gravity (m/s^2)
rho_gas=1.18;% Gas density (Kg/m^3)
rho_liq=900;% Liquid density (Kg/m^3)
for i = 1:13
Ugs_aht(i)= Ugs(i) *((sqrt(rho_gas))/(sqrt(g * D *(rho_liq - rho_gas))));
end
Uls_aht= Uls *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
Uls_aht1= Uls1 *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
Uls_aht2 = Uls2 *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
Uls_aht3 = Uls3 *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
Uls_aht4 = Uls4 *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
Uls_aht5 = Uls5 *((sqrt(rho_liq))/(sqrt(g * D *(rho_liq - rho_gas))));
C = (sqrt(Ugs_aht)) + (sqrt(Uls_aht));
C1 = (sqrt(Ugs_aht)) + (sqrt(Uls_aht1));
C2 = (sqrt(Ugs_aht)) + (sqrt(Uls_aht2));
C3 = (sqrt(Ugs_aht)) + (sqrt(Uls_aht3));
C4 = (sqrt(Ugs_aht)) + (sqrt(Uls_aht4));
C5 = (sqrt(Ugs_aht)) + (sqrt(Uls_aht5));
Ugs =Ugs.';
Uls = Uls.';
Uls1 = Uls1.';
Uls2 = Uls2.';
Uls3 = Uls3.';
Uls4 = Uls4.';
Uls5 = Uls5.';
Ugs_aht = Ugs_aht.';
Uls_aht = Uls_aht.';
Uls_aht1 = Uls_aht1.';
Uls_aht2 = Uls_aht2.';
Uls_aht3 = Uls_aht3.';
Uls_aht4 = Uls_aht4.';
Uls_aht5 = Uls_aht5.';
C = C.';
C1 = C1.';
C2 = C2.';
C3 = C3.';
C4 = C4.';
C5 = C5.';
plot(C,Ugs_aht,'--r')
xlabel('C (sqrt(m/s))')
ylabel('Ugs_aht (m/s)')
title('Flooding Mechanism')
T= table(Ugs, Uls,Uls_aht, Ugs_aht, C,C1,C2,C3,C4,C5);
hold on
plot(C1,Ugs_aht,'--b')
hold on
plot(C2,Ugs_aht,'--g')
hold on
plot(C3,Ugs_aht,'--y')
hold on
plot(C4,Ugs_aht,'--c')
hold on
plot(C5,Ugs_aht,'--k')

Accedi per commentare.

flow = ones(1,13);
flow = string(flow);
for i = 1:13
if C(i) <= 0.2
flow(i) = 'Bubble';
elseif C1(i) <= 0.89
flow(i) = 'Slug';
elseif C2(i) <= 0.98
flow(i) = 'Transition';
elseif C3(i) >= 1.00
flow(i) = 'Churn';
end
end

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by