Outputting an equation with variables and text

3 ビュー (過去 30 日間)
Sasha Blazanovic
Sasha Blazanovic 2020 年 4 月 14 日
編集済み: Star Strider 2020 年 4 月 14 日
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?

回答 (2 件)

Peng Li
Peng Li 2020 年 4 月 14 日
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?

Star Strider
Star Strider 2020 年 4 月 14 日
編集済み: Star Strider 2020 年 4 月 14 日
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'
See the documentation for sprintf (or fprintf) to understand how it works.
.

カテゴリ

Help Center および File ExchangeChemistry についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by