sample_volume=20
final_concentration=0.55
ethanol_concentration=0.96
final_volume = ethanol_vol+sample_volume
final_concentration*final_volume=ethanol_vol*ethanol_concentration
Hi, I am a sympy newbie, and I would like to use sympy to solve equation systems. I don't expect these systems to be huge or complicated.
/.../
- has this been done before? I would like to use tested code if possible.
- is it possible to incorporate units in the calculations? This issue suggests that it is not.
- are there any immediately apparent gotchas the code above? I would like to have a system for solving equations not much more complex than the example.
In [2]: sample_volume=20...: final_concentration=0.55...: ethanol_concentration=0.96...:In [4]: fv,ev = symbols('fv,ev')In [5]: expr1 = fv - ev+sample_volumeIn [6]: expr2 = final_concentration*fv - ev*ethanol_concentrationIn [7]: solve([expr1,expr2], [fv,ev])Out[7]: {ev: -26.8292682926829, fv: -46.8292682926829}# I will recommend you to use solveset's linear system solver (linsolve)In [8]: linsolve([expr1,expr2], [fv,ev])Out[8]: {(-46.8292682926829, -26.8292682926829)}
ethanol_vol = final_concentration*sample_volume/(ethanol_concentration-final_concentration)
In [15]: fv,ev = symbols('fv,ev')
In [17]: expr1 = -fv + ev+sample_volume
In [18]: expr2 = final_concentration*fv - ev*ethanol_concentration
In [19]: solve([expr1,expr2], [fv,ev])
Out[19]: {ev: 26.8292682926829, fv: 46.8292682926829}
In [20]: linsolve([expr1,expr2], [fv,ev])
Out[20]: {(46.8292682926829, 26.8292682926829)}