ingenium
ingenium

Reputation: 27

How to rearrange for a variable with SymPy

I have a complex equation that I would like to rearrange for a variable vz(t) = .... enter image description here

What is the most elegant way in SymPy ? I have determined the upper equation as a solution of an ODE:

import sympy as sym
sym.init_printing()
t,m,k,g,v0,psi = sym.symbols("t,m,k,g,v_0,psi")

vz = sym.Function('v_z')


eqn = sym.Eq(-k*vz(t)**2 - m*vz(t).diff(t) - m*g,0)

sol = sym.dsolve(eqn, ics = {vz(0): v0*sym.sin(psi)})

Upvotes: 0

Views: 241

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14530

The solution you show can be simplified into solving involving atan and then rearranged. Some assumptions are needed on the symbols to make that rearrangement possible though. Actually if you set all symbols to be positive then you'll get a simpler result from dsolve in the first place:

In [51]: import sympy as sym
    ...: sym.init_printing()
    ...: t,m,k,g,v0,psi = sym.symbols("t,m,k,g,v_0,psi", positive=True)
    ...: 
    ...: vz = sym.Function('v_z', positive=True)
    ...: 
    ...: 
    ...: eqn = sym.Eq(-k*vz(t)**2 - m*vz(t).diff(t) - m*g,0)
    ...: 
    ...: sol = sym.dsolve(eqn, ics = {vz(0): v0*sym.sin(psi)})


In [52]: 

In [52]: sol
Out[52]: 
                  ⎛      ⎛           ⎛√k⋅v₀⋅sin(ψ)⎞⎞⎞
                  ⎜      ⎜       atan⎜────────────⎟⎟⎟
                  ⎜      ⎜  t        ⎝   √g⋅√m    ⎠⎟⎟
         √g⋅√m⋅tan⎜√g⋅√k⋅⎜- ── + ──────────────────⎟⎟
                  ⎝      ⎝  √m         √g⋅√k       ⎠⎠
v_z(t) = ────────────────────────────────────────────
                              √k 

Introducing a parameter α = sqrt(m*g/k) this becomes:

In [65]: sol.subs(sqrt(k), sqrt(m)*sqrt(g)/alpha).expand()
Out[65]: 
              ⎛    ⎛v₀⋅sin(ψ)⎞   g⋅t⎞
v_z(t) = α⋅tan⎜atan⎜─────────⎟ - ───⎟
              ⎝    ⎝    α    ⎠    α ⎠

Upvotes: 2

Related Questions