Solving ODEs with the Laplace Transform
The method has three steps:
- Transform the equation, using and .
- Solve the resulting algebraic equation for .
- Invert with partial fractions (Inverse Transforms and Partial Fractions).
The initial conditions are built in, so there are no constants to fit at the end, and homogeneous and particular parts come out together.
Example: a forced second-order equation
, , .
Partial fractions: and . So
The same steps in SymPy:
python · try it yourself
import sympy as sp
t, s = sp.symbols("t s", positive=True)
Y = sp.symbols("Y")
y0, dy0 = 1, 0
transformed = sp.Eq((s**2 * Y - s * y0 - dy0) + 3 * (s * Y - y0) + 2 * Y, 1 / (s + 3))
Ysol = sp.solve(transformed, Y)[0]
print("Y(s) =", sp.apart(sp.factor(Ysol), s))
print("y(t) =", sp.inverse_laplace_transform(Ysol, s, t))Y(s) = 1/(2*(s + 3)) - 2/(s + 2) + 5/(2*(s + 1))
y(t) = 5*exp(-t)/2 - 2*exp(-2*t) + exp(-3*t)/2
The inverse transform only describes . Because t was declared positive, SymPy leaves out the Heaviside(t) factor it otherwise attaches to say so.
Example: a system
The transform works on systems too: each unknown gets its own transform. For , , , :
From the second equation ; substituting, , so and .
python · try it yourself
import sympy as sp
t, s = sp.symbols("t s", positive=True)
X, Y = sp.symbols("X Y")
sol = sp.solve([sp.Eq(s * X - 1, -2 * X + Y), sp.Eq(s * Y, X - 2 * Y)], [X, Y])
for name, F in sol.items():
print(name, "=", sp.factor(F), "->", sp.inverse_laplace_transform(F, s, t))X = (s + 2)/((s + 1)*(s + 3)) -> exp(-t)/2 + exp(-3*t)/2
Y = 1/((s + 1)*(s + 3)) -> exp(-t)/2 - exp(-3*t)/2
Check numerically
Always worth doing: compare the formula with solve_ivp.
python · try it yourself
import numpy as np
from scipy.integrate import solve_ivp
f = lambda t, Y: [Y[1], np.exp(-3 * t) - 3 * Y[1] - 2 * Y[0]]
sol = solve_ivp(f, (0, 3), [1.0, 0.0], t_eval=[0.5, 1.0, 3.0], rtol=1e-11, atol=1e-13)
exact = lambda t: 2.5 * np.exp(-t) - 2 * np.exp(-2 * t) + 0.5 * np.exp(-3 * t)
print(np.round(sol.y[0], 8))
print(np.round(exact(sol.t), 8))[0.89213285 0.67392157 0.11957187]
[0.89213285 0.67392157 0.11957187]
When to use the Laplace transform
- Constant coefficients with forcing that switches on and off or contains impulses (Step Functions and Shifting, The Dirac Delta): this is where it shines.
- Initial conditions at (shift time otherwise).
- Variable coefficients: usually no help.
In software
- Electrical engineers analyse circuits directly in the -domain: an inductor is an impedance , a capacitor , and Kirchhoff's laws become algebra. SPICE's AC analysis works this way with .
- SymPy's
dsolvedoes not use Laplace by default, butsympy.integrals.laplaceprovides the building blocks shown here.
Exercises
Exercise 1: First order
Solve , with the Laplace transform.
Answer
.
Solution
, so .
Exercise 2: Oscillator
Solve , , .
Answer
.
Solution
, .
Exercise 3: Constant forcing (code)
Solve , .
Answer
.
Solution
, and .
python · try it yourself
import sympy as sp
t, s = sp.symbols("t s", positive=True)
print(sp.inverse_laplace_transform(2 / (s * (s**2 + 2 * s + 2)), s, t))1 - exp(-t)*sin(t) - exp(-t)*cos(t)
Test yourself
Transforming , gives…
In the Laplace method, the initial conditions are used…
- at the end to fit constants
- when transforming the derivatives
- never
- only for systems
The Laplace method is most useful for…
- nonlinear equations
- constant-coefficient linear equations with discontinuous or impulsive forcing
- equations with variable coefficients
- PDEs only