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…