{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "a86613cf", "metadata": {}, "outputs": [], "source": [ "using SymPy" ] }, { "cell_type": "markdown", "id": "11fd813c", "metadata": {}, "source": [ "## 1. Formulas for the Laplace Transform" ] }, { "cell_type": "markdown", "id": "92a5a4d3", "metadata": {}, "source": [ "Our text book contains tables with the Laplace transforms for most of the common functions. SymPy offers the functions ``sympy.laplace_transform`` and ``sympy.inverse_laplace_transform`` to compute the Laplace transform and its inverse." ] }, { "cell_type": "code", "execution_count": 2, "id": "e2b4961b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(s, t, a, n)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@syms s, t, a::positive, n::integer" ] }, { "cell_type": "code", "execution_count": 3, "id": "44b91ac3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1/s, 0, True)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lap1 = sympy.laplace_transform(1, t, s)" ] }, { "cell_type": "code", "execution_count": 4, "id": "2f01245d", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\theta\\left(t\\right)$" ], "text/plain": [ "Heaviside(t)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invlap1 = sympy.inverse_laplace_transform(lap1[1], s, t)" ] }, { "cell_type": "markdown", "id": "8393a406", "metadata": {}, "source": [ "The $\\theta(t)$ function is the Heaviside function." ] }, { "cell_type": "code", "execution_count": 5, "id": "51d9e43f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(gamma(n + 1)/(s*s^n), 0, n > -1)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "laptn = sympy.laplace_transform(t^n, t, s)" ] }, { "cell_type": "code", "execution_count": 6, "id": "10bffe74", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$0$" ], "text/plain": [ "0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invlaptn = sympy.inverse_laplace_transform(laptn[1], s, t)" ] }, { "cell_type": "code", "execution_count": 7, "id": "4a70ca35", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1/(a + s), -a, True)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lapexp = sympy.laplace_transform(exp(-a*t), t, s)" ] }, { "cell_type": "code", "execution_count": 8, "id": "f623b2bf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(-exp(-a*t)*Ei(-a*t), 0, True)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invlapexp = sympy.laplace_transform(lapexp[1], s, t)" ] }, { "cell_type": "code", "execution_count": 9, "id": "db76fcf5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(s/(a^2 + s^2), 0, True)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lapcos = sympy.laplace_transform(cos(a*t), t, s)" ] }, { "cell_type": "code", "execution_count": 10, "id": "307a916e", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\cos{\\left(a t \\right)} \\theta\\left(t\\right)$" ], "text/plain": [ "cos(a*t)*Heaviside(t)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invlapcos = sympy.inverse_laplace_transform(lapcos[1], s, t)" ] }, { "cell_type": "code", "execution_count": 11, "id": "dfbae889", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(a/(a^2 + s^2), 0, True)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lapsin = sympy.laplace_transform(sin(a*t), t, s)" ] }, { "cell_type": "code", "execution_count": 12, "id": "636e5429", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\sin{\\left(a t \\right)} \\theta\\left(t\\right)$" ], "text/plain": [ "sin(a*t)*Heaviside(t)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invlapsin = sympy.inverse_laplace_transform(lapsin[1], s, t)" ] }, { "cell_type": "code", "execution_count": 13, "id": "9c3ebd42", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(s/(-a^2 + s^2), a, True)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lapcosh = sympy.laplace_transform(cosh(a*t), t, s)" ] }, { "cell_type": "code", "execution_count": 14, "id": "24d7e399", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\frac{e^{a t} \\theta\\left(t\\right)}{2} + \\frac{e^{- a t} \\theta\\left(t\\right)}{2}$" ], "text/plain": [ " a*t -a*t \n", "e *Heaviside(t) e *Heaviside(t)\n", "----------------- + ------------------\n", " 2 2 " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invlapcosh = sympy.inverse_laplace_transform(lapcosh[1], s, t)" ] }, { "cell_type": "code", "execution_count": 15, "id": "3168fb93", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(a/(-a^2 + s^2), a, True)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lapsinh = sympy.laplace_transform(sinh(a*t), t, s)" ] }, { "cell_type": "code", "execution_count": 16, "id": "a5fbe1d2", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$a \\left(\\frac{e^{a t} \\theta\\left(t\\right)}{2 a} - \\frac{e^{- a t} \\theta\\left(t\\right)}{2 a}\\right)$" ], "text/plain": [ " / a*t -a*t \\\n", " |e *Heaviside(t) e *Heaviside(t)|\n", "a*|----------------- - ------------------|\n", " \\ 2*a 2*a /" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invlapsinh = sympy.inverse_laplace_transform(lapsinh[1], s, t)" ] }, { "cell_type": "markdown", "id": "ac31934d", "metadata": {}, "source": [ "The Laplace transform of the Dirac delta function is 1." ] }, { "cell_type": "code", "execution_count": 17, "id": "048d1353", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\delta\\left(t\\right)$" ], "text/plain": [ "DiracDelta(t)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diracdelta = sympy.inverse_laplace_transform(1, s, t)" ] }, { "cell_type": "code", "execution_count": 18, "id": "4451f758", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, -oo, True)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sympy.laplace_transform(diracdelta, t, s)" ] }, { "cell_type": "markdown", "id": "b59f4bbd", "metadata": {}, "source": [ "## 2. Solving a System of Differential equations" ] }, { "cell_type": "markdown", "id": "3588398c", "metadata": {}, "source": [ "The Laplace transform of\n", "\n", "$$\n", " \\left\\{\n", " \\begin{array}{rcr}\n", " x' & = & -2 x - 9 y \\\\\n", " y' & = & x - 2 y\n", " \\end{array}\n", " \\right. \\quad x(0) = -1, y(0) = 1\n", "$$\n", "\n", "is\n", "\n", "$$\n", " \\left\\{\n", " \\begin{array}{rcr}\n", " s X + 1 & = & -2 X - 9 Y \\\\\n", " s Y - 1 & = & X - 2 Y\n", " \\end{array}\n", " \\right.\n", "$$\n", "\n", "or\n", "\n", "$$\n", " \\left\\{\n", " \\begin{array}{rcr}\n", " (s+2) X + 9 Y & = & -1 \\\\\n", " -X + (s+2) Y & = & 1\n", " \\end{array}\n", " \\right.\n", "$$\n", "\n", "where $X(s)$ and $Y(s)$ are the Laplace transforms\n", "of $x(t)$ and $y(t)$ respectively." ] }, { "cell_type": "code", "execution_count": 19, "id": "be624f75", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(X, Y)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@syms X, Y" ] }, { "cell_type": "code", "execution_count": 20, "id": "33c55e17", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\left[ \\begin{array}{r}X \\left(s + 2\\right) + 9 Y + 1\\\\- X + Y \\left(s + 2\\right) - 1\\end{array} \\right]$\n" ], "text/plain": [ "2-element Vector{Sym}:\n", " X*(s + 2) + 9*Y + 1\n", " -X + Y*(s + 2) - 1" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq1 = (s+2)*X + 9*Y + 1\n", "eq2 = -X + (s+2)*Y - 1\n", "eqs = [eq1, eq2]" ] }, { "cell_type": "code", "execution_count": 21, "id": "e4df4480", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Dict{Any, Any} with 2 entries:\n", " X => (-s - 11)/(s^2 + 4*s + 13)\n", " Y => (s + 1)/(s^2 + 4*s + 13)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sol = sympy.solve(eqs, [X, Y])" ] }, { "cell_type": "markdown", "id": "75f4b7cf", "metadata": {}, "source": [ "Now we transform the solution into the type domain." ] }, { "cell_type": "code", "execution_count": 22, "id": "f9b3ef11", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\left(- 3 e^{- 2 t} \\sin{\\left(3 t \\right)} - e^{- 2 t} \\cos{\\left(3 t \\right)}\\right) \\theta\\left(t\\right)$" ], "text/plain": [ "/ -2*t -2*t \\ \n", "\\- 3*e *sin(3*t) - e *cos(3*t)/*Heaviside(t)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xt = sympy.inverse_laplace_transform(sol[X], s, t)" ] }, { "cell_type": "code", "execution_count": 23, "id": "fe00458a", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\left(- \\frac{e^{- 2 t} \\sin{\\left(3 t \\right)}}{3} + e^{- 2 t} \\cos{\\left(3 t \\right)}\\right) \\theta\\left(t\\right)$" ], "text/plain": [ "/ -2*t \\ \n", "| e *sin(3*t) -2*t | \n", "|- -------------- + e *cos(3*t)|*Heaviside(t)\n", "\\ 3 / " ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "yt = sympy.inverse_laplace_transform(sol[Y], s, t)" ] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.9.1", "language": "julia", "name": "julia-1.9" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.9.1" } }, "nbformat": 4, "nbformat_minor": 5 }