This notebook is a short tutorial of Laplace transform using SymPy.
The main functions to use are laplace_transform
and inverse_laplace_transform
.
from sympy import *
init_session()
IPython console for SymPy 1.0 (Python 2.7.13-64-bit) (ground types: python) These commands were executed: >>> from __future__ import division >>> from sympy import * >>> x, y, z, t = symbols('x y z t') >>> k, m, n = symbols('k m n', integer=True) >>> f, g, h = symbols('f g h', cls=Function) >>> init_printing() Documentation can be found at http://docs.sympy.org/1.0/
Let us compute the Laplace transform from variables t to s, then, we have the condition that t>0 (and real).
t = symbols("t", real=True, positive=True)
s = symbols("s")
To calculate the Laplace transform of the expression t4, we enter
laplace_transform(t**4, t, s)
This function returns (F, a, cond)
where F
is the Laplace transform of f
, R(s)>a is the half-plane of convergence, and cond
are auxiliary convergence conditions.
If we are not interested in the conditions for the convergence of this transform, we can use noconds=True
laplace_transform(t**4, t, s, noconds=True)
fun = 1/((s-2)*(s-1)**2)
fun
inverse_laplace_transform(fun, s, t)
Right now, Sympy does not support the tranformation of derivatives.
If we do
laplace_transform(f(t).diff(t), t, s, noconds=True)
we don't obtain, the expected
s*LaplaceTransform(f(t), t, s) - f(0)
or,
L{f(n)(t)}=snF(s)−n∑k=1sn−kf(k−1)(0),in general.
We can still, operate with the trasformation of a differential equation.
For example, let us consider the equation
df(t)dt=3f(t)+e−t,that has as Laplace transform
sF(s)−f(0)=3F(s)+1s+1.eq = Eq(s*LaplaceTransform(f(t), t, s) - f(0),
3*LaplaceTransform(f(t), t, s) + 1/(s +1))
eq
We then solve for F(s)
sol = solve(eq, LaplaceTransform(f(t), t, s))
sol
and compute the inverse Laplace transform
inverse_laplace_transform(sol[0], s, t)
and we verify this using dsolve
factor(dsolve(f(t).diff(t) - 3*f(t) - exp(-t)))
that is equal if 4C1=4f(0)+1.
It is common to use practial fraction decomposition when computing inverse
Laplace transforms. We can do this using apart
, as follows
frac = 1/(x**2*(x**2 + 1))
frac
apart(frac)
We can also compute the Laplace transform of Heaviside and Dirac's Delta "functions"
laplace_transform(Heaviside(t - 3), t, s, noconds=True)
laplace_transform(DiracDelta(t - 2), t, s, noconds=True)
The next cell change the format of the notebook.
from IPython.core.display import HTML
def css_styling():
styles = open('./styles/custom_barba.css', 'r').read()
return HTML(styles)
css_styling()