Laplace Transforms in Python
Throughout my student days, I tried my best to avoid using the Laplace transform. What a pity, as it’s such a powerful tool! The reason why I disliked the Laplace transform, is that you can’t do a lot with it unless you have a reasonable table of inverse transforms. But here comes Python and here comes sympy , and the situation is totally different. With these tools at hand, it’s a joy to use the Laplace transform, for example, to solve differential equations.
The Laplace transform of a function 𝑓 is defined as
So you give it a function 𝑓(𝑡) and it spits out another function 𝐿(𝑓)=𝐹(𝑝), which we typically call by the same letter but uppercase. Note that since we integrate only from 0 to infinity, the Laplace transform will be the same no matter how 𝑓 behaves for negative 𝑡.
Let’s calculate the Laplace transforms of a few typical functions and start with the simplest one: the constant function. Since I’m lazy to write, I’ll use Python to do it:
There is an easier way in sympy to calculate the Laplace transform. Simply use the dedicated function:
Note that the output of that function not only gives you the Laplace transform but also information on whether the calculation was successful. The reason is that the integral is an improper one since we integrate out to infinity. And it could be that the integral does not converge. Not so in our case. If you only want the solution, use the
noconds=True
option:
The Laplace transform is linear, which means that
for constants a and b, which is easy to see if you simply insert the definition of 𝐿.
Another important property is the shifting property. If you multiply a function 𝑓(𝑡) with Laplace transform 𝐹(𝑝) by exp(−𝑎𝑡) and then take the Laplace transform, this will just shift the argument of 𝐹:
That’s most easily seen by calculating the Laplace transform of exp(−𝑎𝑡), which could be understood as multiplying the constant 1 with exp(−𝑎𝑡), so it should give 1/(𝑝+𝑎). Let’s check:
Indeed! Finally, before we move on to the inverse Laplace transform, let’s make a little table of Laplace transforms. We’ll borrow a function
display_table
from the article
“How to Display Objects Jupyter With Tabular Layout”
and write a little function for making plots:
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from ipywidgets import Output
mpl.rcParams['lines.linewidth'] = 12
def make_plot(f, xvar, xlims):
"""Make a plot with matplotlib and store it in an output widget."""
x_ = np.linspace(*xlims, 200)
# Lambdify the function f so what we can work with numpy on it.
# Also, handle the special case of a constant function,
# which is just a number and wouldn't lambdify correctly:
if isinstance(f, Integer):
f_ = [float(f)] * len(x_)
else:
f = lambdify(xvar, f)
f_ = f(x_)
# Create the plot in an output widget for later use.
out = Output()
with out:
fig, ax = plt.subplots(constrained_layout=True, figsize=(12, 8))
ax.plot(x_, f_)
ax.set_xticks([])
ax.set_yticks([])
plt.show(fig)
return out
Now we are ready to create the table:
a = Symbol('a', real=True, positive=True)
n = Symbol('n', integer=True)
from ipywidgets import HBox
fs = [Integer(1), exp(-a*t), sin(a*t), cos(a*t), t**n, sinh(a*t), cosh(a*t)]
repl = {a: 1, n: 2}
rows = []
for f in fs:
F = laplace_transform(f, t, p, noconds=True).simplify()
# Create little plots of the function and its transform:
pl_f = make_plot(f.subs(repl), t, (-2, 2))
pl_F = make_plot(F.subs(repl), p, (0.1, 2))
rows.append([pl_f, f, pl_F, simplify(F)])
display_table(rows)
So much for the Laplace transform itself. But it is of little use unless you have some way to transform it back to normal space. So we need the inverse Laplace transform. In general, this is quite tricky because you would have to solve a complicated integral in the complex plane. That’s also the reason, why the Laplace transform is rarely used in numerical work but almost only in analytical work. In practice, most people look up the inverse from tables, just like they look up integrals in integral tables. Fortunately, in
sympy
, there is an easier way: the function
inverse_laplace_transform
. Let's apply it to 1/𝑝, which should give back 1:
Hmm… that’s the Heaviside-Functon, or step-function. It has the value 0 for negative arguments and 1 for 𝑡≥0. So that’s ok. Remember that the Laplace transform loses all information on the transformed function for negative arguments. So the inverse transform simply sets it to zero. Or something a little more complicated, which would need a few lines of algebra using Laplace transform tables:
Nice. That saves us a lot of work when we want to use the Laplace transform, for example, to solve differential equations. But that’s the topic for the next post. See you next time, if you like. Thanks for reading!