Inverse Laplace Transforms in MATLAB, Python (SymPy) and Wolfram
Compute inverse Laplace transforms in software: MATLAB ilaplace, SymPy inverse_laplace_transform and Wolfram syntax, with the output quirks to expect.
Inverse Laplace Transforms in MATLAB, Python (SymPy) and Wolfram
A student has just solved for F(s) in a second-order differential equation and stares at the expression. The clock is ticking, and the correct ilaplace matlab command, the right SymPy call, or the proper Wolfram syntax returns f(t) in seconds. The exact syntax for each tool on the same F(s) is shown, the reason the outputs look different is explained, and what to do when no closed form exists is told.
MATLAB: Syms and Ilaplace
Declare Variables and Call Ilaplace
In MATLAB, use ilaplace from the Symbolic Math Toolbox. First, declare symbolic variables with syms. Then define F(s) and call ilaplace(F). The output is a symbolic expression in t.
For example, to invert F(s) = 1/(s^2 + 2s + 5):
syms s t
F = 1/(s^2 + 2*s + 5);
f = ilaplace(F);
disp(f)
It assumes s is real for simplification. It uses partial fraction expansion, table lookup, and residue computation internally. The result omits the Heaviside step function u(t) because MATLAB assumes t ≥ 0. For repeated poles, ilaplace handles the derivative terms automatically.
Python: Sympy Inverse Laplace Transform
Import SymPy and Call Inverse_Laplace_Transform
In Python, use SymPy's inverse_laplace_transform from the sympy.integrals module. Import sympy, define symbolic variables s and t, create F(s), and call the function. The syntax is inverse_laplace_transform(F, s, t).
For the same F(s) = 1/(s^2 + 2s + 5):
import sympy as sp
s, t = sp.symbols('s t')
F = 1/(s**2 + 2*s + 5)
f = sp.inverse_laplace_transform(F, s, t)
sp.pprint(f)
SymPy returns exp(-t)*sin(2*t)*Heaviside(t)/2. The Heaviside(t) factor explicitly multiplies the result. This is because SymPy's algorithm (based on Meijer G-transforms and table lookup) preserves the causality condition that f(t) = 0 for t < 0. The SymPy inverse_laplace_transform documentation covers all supported functions: rational, exponential, trigonometric, hyperbolic, Bessel, error function, Dirac delta, Heaviside step, and Meijer G.
Wolfram Language / Alpha: Inverse Laplace Transforms
Use InverseLaplaceTransform or Natural Language
Wolfram offers two routes: the Wolfram Language in Mathematica and the free Wolfram|Alpha website. In the Wolfram Language, use InverseLaplaceTransform[F, s, t]. For F(s) = 1/(s^2 + 2s + 5):
InverseLaplaceTransform[1/(s^2 + 2*s + 5), s, t]
Wolfram returns (E^(-t) Sin[2 t])/2. The output is clean, with no Heaviside function, because Wolfram assumes t > 0. The algorithm uses table lookup, partial fractions, residue computation, and the convolution theorem. In Wolfram|Alpha, type the same expression in natural language: "inverse Laplace transform of 1/(s^2+2s+5)". The Wolfram InverseLaplaceTransform documentation lists supported functions including Mittag-Leffler and Meijer G.
| Tool | Syntax | Output f(t) | Heaviside Included? |
|---|---|---|---|
| MATLAB (ilaplace) | ilaplace(F) | (1/2)*exp(-t)*sin(2*t) | No |
| SymPy | inverse_laplace_transform(F, s, t) | exp(-t)*sin(2*t)*Heaviside(t)/2 | Yes |
| Wolfram Language | InverseLaplaceTransform[F, s, t] | (E^(-t) Sin[2 t])/2 | No |
Why Outputs Contain Heaviside(t) or DiracDelta
SymPy includes Heaviside(t) because the inverse Laplace transform is defined only for t ≥ 0. The Heaviside step function ensures f(t) = 0 for negative t. MATLAB and Wolfram assume t is non-negative by default and omit the factor. Both conventions are correct; you need to know which your tool uses.
The DiracDelta(t) function (the Dirac delta) appears when you invert a constant, such as F(s) = 1. The inverse is δ(t), an impulse at t = 0. All three tools handle this: MATLAB's ilaplace returns DiracDelta(t), SymPy's inverse_laplace_transform returns DiracDelta(t), and Wolfram returns DiracDelta[t]. The Dirac delta is essential for impulse response analysis.
Numerical Inversion When No Closed Form Exists
When Analytic Inversion Fails
Not every F(s) has a closed-form inverse. Examples include F(s) = 1/(s*log(s)) and many transfer functions with time delays or non-rational terms. All three tools return the expression unevaluated when no analytic inverse exists.
Switch to Numerical Methods
When closed-form inversion fails, use numerical inversion. MATLAB provides numerical evaluation via the de command or custom algorithms. Python has the mpmath library's invertlaplace function. Wolfram|Alpha can compute numerical values pointwise using InverseLaplaceTransform with numerical arguments.
For practical engineering, numerical inversion is the fallback. The partial fraction decomposition method often succeeds for rational functions, but when it does not, the numerical path is reliable.
Common Questions
Why does SymPy output include Heaviside(t) but MATLAB does not?
SymPy returns the mathematically complete result f(t) = 0 for t < 0. MATLAB assumes t ≥ 0 and omits the factor. Both are valid; add Heaviside(t) manually in MATLAB if you need it.
What do I do when ilaplace returns an unevaluated expression?
Switch to numerical inversion. In MATLAB, use the de command or a custom numerical Laplace inversion function. In Python, use mpmath.invertlaplace. In Wolfram, evaluate pointwise.
How do I invert a function with a time delay e^{-as}?
Use the time-shift theorem: the inverse is the undelayed response shifted by a. All three tools handle e^{-as} directly. For example, ilaplace(exp(-2*s)/s) returns Heaviside(t-2).
Can I verify my partial fraction decomposition manually before using these tools?
Yes. Use the Heaviside cover-up method for distinct real poles. For complex poles, the tools handle them automatically. Cross-check the decomposition by recombining terms.