How to loop on several inputs and variables?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Sulaymon Eshkabilov
2021-6-5
编辑:Sulaymon Eshkabilov
2021-6-5
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 个评论
Sulaymon Eshkabilov
2021-6-5
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!