Outputting an equation with variables and text
3 views (last 30 days)
Show older comments
I have the following variables:
a=0.9; %CH4 reactant
b=0.2; %H2O reactant
c=0.1; %CO2 reactant
d=1.8; %O2 reactant
e=2; %H20 product
f=1; %O2 product
These are coefficients to a chemical equation. I am using if else if statements to manipulate the coefficients for the reaction balance. I need to output the chemical reaction balance for the user, for example:
The balance for the chemical reaction is: 0.9CH4 + 0.2H2O + 0.1CO2 + 1.8O2 -> 2H2O + 1O2
How may I type code in matlab to incorporate both text and values of variables?
0 Comments
Answers (2)
Peng Li
on 14 Apr 2020
a=0.9; %CH4 reactant
b=0.2; %H2O reactant
c=0.1; %CO2 reactant
d=1.8; %O2 reactant
e=2; %H20 product
f=1; %O2 product
fprintf("the balance for the chemical reaction is: %.1fCH4 + %.1fH2O " ...
+ "+ %.1fCO2 + %.1fO2 -> %dH2O + %dO2\r", ...
a, b, c, d, e, f);
the balance for the chemical reaction is: 0.9CH4 + 0.2H2O + 0.1CO2 + 1.8O2 -> 2H2O + 1O2
Is this what you want?
0 Comments
Star Strider
on 14 Apr 2020
Edited: Star Strider
on 14 Apr 2020
Try this:
ChemEqn = sprintf('%gCH4 + %gH20 + %gCO2 + %gO2 -> %gH2O + %gO2',a,b,c,d,e,f)
producing:
ChemEqn =
'0.9CH4 + 0.2H20 + 0.1CO2 + 1.8O2 -> 2H2O + 1O2'
.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!