Skip to content

Instantly share code, notes, and snippets.

@yoniLavi
Created September 12, 2024 08:06
Show Gist options
  • Save yoniLavi/9cc5205a06b9175473269dc3338e60e7 to your computer and use it in GitHub Desktop.
Save yoniLavi/9cc5205a06b9175473269dc3338e60e7 to your computer and use it in GitHub Desktop.
A clean example of approaching an ODE via a Laplace transform in Sympy
from sympy import symbols, Eq, laplace_transform
from sympy.physics.mechanics import dynamicsymbols
t, s, G, F = symbols('t s G F')
g, f = dynamicsymbols('g f') # type: ignore
# Equation: d^2g/dt^2 + 4dg/dt + 3g = 0.2 df/dt + 1.5f
eq_lhs = g.diff(t, t) + 4 * g.diff(t) + 3 * g # type: ignore
eq_rhs = 0.2 * f.diff(t) + 1.5 * f # type: ignore
laplace_eq = Eq(laplace_transform(eq_lhs, t, s)[0], laplace_transform(eq_rhs, t, s)[0]).subs({
g.subs(t, 0): -1, # g(0) = -1
g.diff(t).subs(t, 0): 0, # g'(0) = 0
f.subs(t, 0): 0, # f(0) = 0
laplace_transform(g, t, s)[0]: G, # L(g(t)) = G(s)
laplace_transform(f, t, s)[0]: F, # L(f(t)) = F(s)
})
display(laplace_eq.simplify().expand().reversed) # show it exactly as in the materials
display(Eq(G, sp.solve(laplace_eq, G)[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment