In recent versions of sympy (1.13 and later), \(~\)sympy.laplace_transform can transform derivatives of unknown functions, \(~\)apply both translation theorems, \(~\)the scaling rule \(f(at)\), \(~\)and the rule for \(t^n f(t)\). \(~\)Two companion functions finish the job:
laplace_correspondence(expr, {f: F})\(~\)replaces \(\mathcal{L}\{f(t)\}\) by \(F(s)\)
A few rules are still missing in sympy 1.14: \(~\)integrals \(\int_0^t\), \(~\)convolution integrals, \(~\)and products such as \(e^{ct}\, t^n f(t)\). \(~\)For these we use a small wrapper _laplace_transform_expansion.py, \(~\)whose function laplace_transform_() adds only those rules and otherwise calls sympy.laplace_transform
17.2 Symbols and Functions
a, b = sympy.symbols("a, b", positive=True)n, m = sympy.symbols("n, m", positive=True, integer=True)t, tau, tau1, tau2 = sympy.symbols("t, tau, tau1, tau2", positive=True)s = sympy.symbols("s")y, f, g = sympy.symbols("y, f, g", cls=sympy.Function)Y, F, G = sympy.symbols("Y, F, G", cls=sympy.Function)
A short helper: \(~\)transform and write \(\mathcal{L}\{y\}\), \(\mathcal{L}\{f\}\), \(\mathcal{L}\{g\}\) as \(Y(s)\), \(F(s)\), \(G(s)\)
A technique that can be used to solve certain ODE problems is to Laplace transform the ODE, which for many problems results in an algebraic equation that is easier to solve. The solution to the algebraic equation can then be transformed back to the original domain with an inverse Laplace transform, to obtain the solution to the original problem. For example, consider the following differential equation for a driven harmonic oscillator:
At this point, we need to specify the initial conditions for the ODE problem. Here we use \(y(0)=1\) and \(y'(0)=0\), and substitute them into the Laplace-transformed ODE with laplace_initial_conds:
Lode = laplace_initial_conds(Lode, t, {y: [1, 0]})Lode
The result is a list of solutions, which in this case contains only one element. Performing the inverse Laplace transformation on this expression gives the solution to the original problem in the time domain (older versions of sympy needed the partial fractions to be inverted one by one):
\(\displaystyle y{\left(t \right)} = \left(\frac{43 \sin{\left(3 t \right)}}{111} + \frac{49 \cos{\left(3 t \right)}}{37}\right) e^{- t} + \frac{2 \sin{\left(3 t \right)}}{37} - \frac{12 \cos{\left(3 t \right)}}{37}\)
Used in the main text:Section 4.2 (The Inverse Transform and Transforms of Derivatives)
17.13Example 2
In some instances, the Laplace transform can be used to solve linear differential equations with variable monomial coefficients
\[ty'' + y' +ty =0, \;\;y(0)=1, \;y'(0)=0\]
\(~\)
s, t = sympy.symbols('s, t', positive=True)y, Y = sympy.symbols('y, Y', cls=sympy.Function)ode = t *y(t).diff(t, 2) +y(t).diff(t) +t *y(t)ode
\(\displaystyle t y{\left(t \right)} + t \frac{d^{2}}{d t^{2}} y{\left(t \right)} + \frac{d}{d t} y{\left(t \right)}\)
\(~\)
Reduce the given differential equation to a linear first-order DE in the transformed function \(Y(s)=\mathcal{L}_t\left[y(t)\right]\). \(~\)Since \(\mathcal{L}\{t\,h(t)\} = -\frac{d}{ds}\mathcal{L}\{h(t)\}\), \(~\)the factor \(t\) becomes a derivative with respect to \(s\)