Skip to content

Instantly share code, notes, and snippets.

@moorepants
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save moorepants/858503aa180df60a7829 to your computer and use it in GitHub Desktop.

Select an option

Save moorepants/858503aa180df60a7829 to your computer and use it in GitHub Desktop.
A simple physics example in SymPy.
import sympy as sm
g, tf, x0 = sm.symbols('g, tf, x0', real=True, positive=True)
t, vf = sm.symbols('t, vf', real=True)
x, v = sm.symbols('x, v', cls=sm.Function)
accel = sm.Eq(x(t).diff(t, 2), -g)
position = sm.dsolve(accel, x(t))
C1, C2, _, _ = list(sm.ordered(position.free_symbols))
velocity = sm.dsolve(accel.subs({x(t).diff(t): v(t)}), v(t))
# Make sure C1 and C2 are the same constants in both equations.
velocity = velocity.subs({C1: C2})
eq1 = position.subs({x(t): x0, t: 0})
eq2 = position.subs({x(t): 0, t: tf})
eq3 = velocity.subs({v(t): 0, t: 0})
sol = sm.solve((eq1, eq2, eq3), tf, C1, C2, dict=True)
final_velocity = velocity.subs({v(t): vf, t: tf}).subs(sol[0])
print(final_velocity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment