TExcelTAD
Home › Differential Equations › Solving ODEs with the Laplace Transform

Solving ODEs with the Laplace Transform

The method has three steps:

  1. Transform the equation, using L{y′}=sY−y(0)\mathcal{L}\{y'\} = sY - y(0) and L{y′′}=s2Y−sy(0)−y′(0)\mathcal{L}\{y''\} = s^2Y - sy(0) - y'(0).
  2. Solve the resulting algebraic equation for Y(s)Y(s).
  3. 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

y′′+3y′+2y=e−3ty'' + 3y' + 2y = e^{-3t}, y(0)=1y(0) = 1, y′(0)=0y'(0) = 0.

(s2Y−s)+3(sY−1)+2Y=1s+3(s2+3s+2) Y=s+3+1s+3Y=s+3(s+1)(s+2)+1(s+1)(s+2)(s+3).\begin{aligned} (s^2Y - s) + 3(sY - 1) + 2Y &= \frac{1}{s + 3} \\ (s^2 + 3s + 2)\,Y &= s + 3 + \frac{1}{s + 3} \\ Y &= \frac{s + 3}{(s + 1)(s + 2)} + \frac{1}{(s + 1)(s + 2)(s + 3)}. \end{aligned}

Partial fractions: s+3(s+1)(s+2)=2s+1−1s+2\frac{s + 3}{(s + 1)(s + 2)} = \frac{2}{s + 1} - \frac{1}{s + 2} and 1(s+1)(s+2)(s+3)=1/2s+1−1s+2+1/2s+3\frac{1}{(s + 1)(s + 2)(s + 3)} = \frac{1/2}{s + 1} - \frac{1}{s + 2} + \frac{1/2}{s + 3}. So

y(t)=52e−t−2e−2t+12e−3t.y(t) = \tfrac{5}{2}e^{-t} - 2e^{-2t} + \tfrac{1}{2}e^{-3t}.

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 t≥0t \ge 0. 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 x′=−2x+yx' = -2x + y, y′=x−2yy' = x - 2y, x(0)=1x(0) = 1, y(0)=0y(0) = 0:

sX−1=−2X+Y,sY=X−2Y.sX - 1 = -2X + Y, \qquad sY = X - 2Y.

From the second equation X=(s+2)YX = (s + 2)Y; substituting, ((s+2)2−1)Y=1((s + 2)^2 - 1)Y = 1, so Y=1(s+1)(s+3)Y = \frac{1}{(s + 1)(s + 3)} and y=12(e−t−e−3t)y = \frac{1}{2}(e^{-t} - e^{-3t}).

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 t=0t = 0 (shift time otherwise).
  • Variable coefficients: usually no help.

In software

  • Electrical engineers analyse circuits directly in the ss-domain: an inductor is an impedance sLsL, a capacitor 1sC\frac{1}{sC}, and Kirchhoff's laws become algebra. SPICE's AC analysis works this way with s=iωs = i\omega.
  • SymPy's dsolve does not use Laplace by default, but sympy.integrals.laplace provides the building blocks shown here.

Exercises

Exercise 1: First order

Solve y′+4y=0y' + 4y = 0, y(0)=3y(0) = 3 with the Laplace transform.

Answer

y=3e−4ty = 3e^{-4t}.

Solution

sY−3+4Y=0sY - 3 + 4Y = 0, so Y=3s+4Y = \frac{3}{s + 4}.

Exercise 2: Oscillator

Solve y′′+9y=0y'' + 9y = 0, y(0)=2y(0) = 2, y′(0)=3y'(0) = 3.

Answer

y=2cos⁡3t+sin⁡3ty = 2\cos 3t + \sin 3t.

Solution

s2Y−2s−3+9Y=0s^2Y - 2s - 3 + 9Y = 0, Y=2ss2+9+3s2+9Y = \frac{2s}{s^2 + 9} + \frac{3}{s^2 + 9}.

Exercise 3: Constant forcing (code)

Solve y′′+2y′+2y=2y'' + 2y' + 2y = 2, y(0)=y′(0)=0y(0) = y'(0) = 0.

Answer

y=1−e−t(cos⁡t+sin⁡t)y = 1 - e^{-t}(\cos t + \sin t).

Solution

Y=2s(s2+2s+2)=1s−s+2(s+1)2+1Y = \frac{2}{s(s^2 + 2s + 2)} = \frac{1}{s} - \frac{s + 2}{(s + 1)^2 + 1}, and s+2=(s+1)+1s + 2 = (s + 1) + 1.

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 y′−2y=0y' - 2y = 0, y(0)=5y(0) = 5 gives…

  1. sY−2Y=0sY - 2Y = 0
  2. sY−5−2Y=0sY - 5 - 2Y = 0
  3. Y=5Y = 5
  4. sY+5=2YsY + 5 = 2Y

In the Laplace method, the initial conditions are used…

  1. at the end to fit constants
  2. when transforming the derivatives
  3. never
  4. only for systems

The Laplace method is most useful for…

  1. nonlinear equations
  2. constant-coefficient linear equations with discontinuous or impulsive forcing
  3. equations with variable coefficients
  4. PDEs only