How to use the sum of all the iterations of a value calculated in a for-loop to calculate another value within the same loop?
12 views (last 30 days)
Show older comments
Hi, I am trying to calculate the species mass fractions (Wi) within a stream by dividing the species flow rate (Mi) by the overall mass flow rate (sum of all values of Mi). I have managed to calculate and save all the mass flow rates (mi), but I'm struggling with summing all those values to get the overall stream mass flow rate. Any help would be greatly appreciated.
for i = 1:50
Mi = M(i)*n(i); % species mass flow rate (g/h)
mi(i) = Mi;
Xi = n(i)/sum(n); % species mole fraction (no unit)
xi(i) = Xi;
Wi = Mi/sum(mi); % species mass fraction (no unit) - this is the denominator I'm struggling with
wi(i) = Wi;
end
4 Comments
Dyuman Joshi
on 20 Sep 2022
It's not a bit clear to me what you are trying to do, can you an example with a set of data?
Answers (1)
Dyuman Joshi
on 20 Sep 2022
Edited: Dyuman Joshi
on 20 Sep 2022
Is using a loop necessary?
If yes, then my comment above should work -
M = [2 3];
n = [8 9];
for i = 1:2
mi(i) = M(i)*n(i); % species mass flow rate (g/h)
end
mi
wi=mi/sum(mi)
If no, then you can do without loop as well
mi1=M.*n
wi1=mi1/sum(mi1)
Verification, with the code you provided
Mi_1 = M(1)*n(1);
Mi_2 = M(2)*n(2);
Mi = [Mi_1 Mi_2]
Mi_total = sum(Mi);
Wi_1 = Mi_1/Mi_total;
Wi_2 = Mi_2/Mi_total;
Wi = [Wi_1 Wi_2]
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!