Last active
August 29, 2015 14:15
-
-
Save moorepants/858503aa180df60a7829 to your computer and use it in GitHub Desktop.
A simple physics example in SymPy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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