Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dsaint31x/347399c391ea0d162494316f7a875d36 to your computer and use it in GitHub Desktop.
Save dsaint31x/347399c391ea0d162494316f7a875d36 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Inver Laplace Transform by using SymPy\n",
"\n",
"## Repeasted Linear Factor 경우,\n",
"\n",
"sympy를 이용할 경우, 간단히 구할 수 있음.\n",
"\n",
"아래의 소스는 아래의 $X(s)$를 Partial fraction decomposition하는 것과 역변환하는 경우를 보여줌.\n",
"\n",
"$$\n",
"X(s) = \\frac{2s^2-25s-33}{s^3-3s^2-9s-5}\n",
"$$\n",
"\n",
"* SymPy에서 $\\theta(t)$는 Unit Step Function이며, 다른 이름으로는 Heaviside step function이라고도 불림.\n"
]
},
{
"cell_type": "code",
"execution_count": 230,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{5}{s + 1} + \\frac{1}{\\left(s + 1\\right)^{2}} - \\frac{3}{s - 5}$"
],
"text/plain": [
"5/(s + 1) + (s + 1)**(-2) - 3/(s - 5)"
]
},
"execution_count": 230,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sympy as sym\n",
"\n",
"s,t = sym.symbols('s t')\n",
"\n",
"\n",
"num = 2*(s**2) - 25*s -33\n",
"dim = s**3 -3*(s**2)-9*s-5\n",
"\n",
"X = num/dim\n",
"\n",
"sym.apart(X)"
]
},
{
"cell_type": "code",
"execution_count": 231,
"metadata": {},
"outputs": [],
"source": [
"# 꽤 시간이 걸림.\n",
"X = sym.nsimplify(X)\n",
"x = sym.inverse_laplace_transform(X,s,t)"
]
},
{
"cell_type": "code",
"execution_count": 232,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(t - 3*exp(6*t) + 5)*exp(-t)*Heaviside(t)\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle \\left(t - 3 e^{6 t} + 5\\right) e^{- t} \\theta\\left(t\\right)$"
],
"text/plain": [
"(t - 3*exp(6*t) + 5)*exp(-t)*Heaviside(t)"
]
},
"execution_count": 232,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(x)\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Complex Conjugate Pole의 경우,\n",
"\n",
"$$\n",
"X(s) = \\frac{4(s+1)^2}{s(s^2+2s+2)}\n",
"$$\n",
"\n",
"* SymPy에서 $\\theta(t)$는 Unit Step Function이며, 다른 이름으로는 Heaviside step function이라고도 불림."
]
},
{
"cell_type": "code",
"execution_count": 233,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1 - I)*(1 + I)*(exp(t) + sin(t) + cos(t))*exp(-t)*Heaviside(t)\n"
]
},
{
"data": {
"text/latex": [
"$\\displaystyle \\left(1 - i\\right) \\left(1 + i\\right) \\left(e^{t} + \\sin{\\left(t \\right)} + \\cos{\\left(t \\right)}\\right) e^{- t} \\theta\\left(t\\right)$"
],
"text/plain": [
"(1 - I)*(1 + I)*(exp(t) + sin(t) + cos(t))*exp(-t)*Heaviside(t)"
]
},
"execution_count": 233,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"num = 4*((s+1)**2)\n",
"dim = s*(s**2+2*s+2)\n",
"\n",
"X = num/dim\n",
"x = sym.inverse_laplace_transform(X,s,t)\n",
"print(x)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 234,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left(1 - i\\right) \\left(1 + i\\right) \\left(e^{t} + \\sqrt{2} \\sin{\\left(t + \\frac{\\pi}{4} \\right)}\\right) e^{- t} \\theta\\left(t\\right)$"
],
"text/plain": [
"(1 - I)*(1 + I)*(exp(t) + sqrt(2)*sin(t + pi/4))*exp(-t)*Heaviside(t)"
]
},
"execution_count": 234,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sym.trigsimp(sym.trigsimp(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"참고로, $\\sin(t+\\pi/4) = \\cos(t-\\pi/4)$임."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sym.sin(t+sym.pi/4).rewrite(sym.cos)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment