How to loop on several inputs and variables?

1 visualización (últimos 30 días)
ONG ZHI SAN
ONG ZHI SAN el 5 de Jun. de 2021
Comentada: Sulaymon Eshkabilov el 5 de Jun. de 2021
I have written a program to calculate the molar flowrate, mass flowrate and mass fraction of the components at certain amount of ammonia produced per day. But I have to calculate for another 10 different amount of ammonia produced a day. I wish to use for loop to solve it but I have no idea on how to do it. Any suggestion on how to do this are much appreciated.
Below is my code.
clc
clear all
% Start
% Let M=mass flow rate, m=mass fraction, Y=molar flow rate
% Input variable
M= input('Enter the mass flow rate of ammonia produced in ton/day: ');
Eff=0.17
MW_NH3= 17
MW_N2= 28
MW_H2= 2
N2_to_NH3= 0.5
H2_to_N2= 3
% Molar flow rate calculation
% Calculation of the molar flow rate of N2 and H2 reacted,
M0 = M*1000; % to convert ton/day to kg/day
Y0=M0/MW_NH3; % to convert the mass flow rate of NH3 to molar flow rate
N2_reacted = Y0*(N2_to_NH3);
H2_reacted = (H2_to_N2)*N2_reacted;
% Calculation of the molar flow rate of N2 and H2 at reactor inlet
Y3= N2_reacted/Eff; % to calculate actual inlet of the N2 to the reactor
Y4= 3*Y3; % to calculate actual inlet of H2 to the reactor
% Calculation of molar flow rate of N2 and H2 at reactor outlet
Y1 = Y3 - N2_reacted; % N1 = unreacted N2
Y2 = Y4 - H2_reacted; % N2 = unreacted H2
% Mass flow rate calculation
% To calculate of mass flow rate of N2 and H2 at reactor outlet
M1 = Y1*(MW_N2); % mass flow rate of N2
M2 = Y2*(MW_H2); % mass flow rate of H2
% To calculate of mass flow rate of N2 and H2 at reactor inlet
M3 = Y3*(MW_N2); % mass flow rate of N2
M4 = Y4*(MW_H2); % mass flow rate of H2
% Mass fraction calculation
% To calculate the mass fraction at reactor outlet
m0 = M0/(M0+M1+M2); % mass fraction of NH3
m1 = M1/(M0+M1+M2); % mass fraction of N2
m2 = 1- m0 - m1; % mass fraction of H2
% To calculate the mass fraction at reactor inlet
m3 = M3/(M3+M4); % mass fraction of N2
m4 = 1- m3; % mass fraction of H2

Respuesta aceptada

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 5 de Jun. de 2021
Editada: Sulaymon Eshkabilov el 5 de Jun. de 2021
Your calcs are quite simple and your code has to work ok with any number of input data points. To ease the input process, you may consider to input the whole data as a column or row of data directly or import them into MATLAB.
Note that the loop is not required here and is an efficient.
If you want to make sure that user enter's exact number of input data that can be coded using "while" loop, e.g: N = 10 (number of entry points)
...
N = 10;
NM = 0;
while NM ~=N
M= input('Enter the mass flow rate of ammonia produced in ton/day: ');
NM=numel(M);
end
...
  2 comentarios
ONG ZHI SAN
ONG ZHI SAN el 5 de Jun. de 2021
It works! Thank you!! Is there any suggestion on how to export all the data calculated to excel file? It shows error as the input array is empty when i use 'xlswrite'. Thank you in advance.
Molar_Flowrate=[Y0 Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8]';
Mass_Flowrate=[M0 M1 M2 M3 M4 M5 M6 M7 M8]';
Mass_Fraction=[m0 m1 m2 m3 m4 m5 m6 m7 m8]';
A = ([Molar_Flowrate Mass_Flowrate Mass_Fraction])
col_header= {'Molar Flowrate, Y', 'Mass Flowrate,M', 'Mass Fraction,m'};
Components = [0 1 2 3 4 5 6 7 8]';
row_header={'Components'};
Row_header= [Components];
xlswrite('ALW_W102.xls', A)
NM=numel(M);
end
Sulaymon Eshkabilov
Sulaymon Eshkabilov el 5 de Jun. de 2021
If your augmented data matrix is correct, then it should write it into MS Excel. You may also explore: csvwrite('ALW_W102.csv', A). The imported data file can be opened and read in MS Excel.
In addition, after converting your data into a table array format, you can use tablewrite() that is also very similar.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by