Control Engineering II, Week 5 - Note 1
English translation
Symbolic calculation and Laplace transform with python
Symbolic Calculation
Install the sympy library
!pip install sympyRequirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (1.12) Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy) (1.3.0)
sympy library import
import sympy as sym
Call init_printing to display equations in LaTeX format
sym.init_printing(use_latex='mathjax')
Declaring symbolic variables and expressing polynomials
x, y, z = sym.symbols('x y z')
( 5*x**3 - x**2 + 3*x - 1 ) + ( -x**3 + 2*x**2 + 5 )3 2 4⋅x + x + 3⋅x + 4
Storing a polynomial in a variable
A = -3*x*y + x**2 + 2*y**2 + x
B = ( x**2 + x - 2 ) * ( 3*x - 1 )
A, B⎛ 2 2 ⎛ 2 ⎞⎞ ⎝x - 3⋅x⋅y + x + 2⋅y , (3⋅x - 1)⋅⎝x + x - 2⎠⎠
Collecting a polynomial in the symbol ’x’
sym.collect( A, x )2 2 x + x⋅(1 - 3⋅y) + 2⋅y
Expanding a polynomial
sym.expand(B)3 2 3⋅x + 2⋅x - 7⋅x + 2
Entering a factorable polynomial as C
C = 2*x**2 + 5*x*y + 3*y**2 + 3*x + 5*y - 2
C2 2 2⋅x + 5⋅x⋅y + 3⋅x + 3⋅y + 5⋅y - 2
Factoring polynomial C
sym.factor(C)(x + y + 2)⋅(2⋅x + 3⋅y - 1)
Entering a rational polynomial expression as D
D = ( x**2 - 2*x + 1 ) / ( x**2 - 3*x + 2 )
D2 x - 2⋅x + 1 ──────────── 2 x - 3⋅x + 2
Factoring the denominator and numerator, then canceling common factors
sym.cancel(D)x - 1 ───── x - 2
Entering a sum of rational polynomial expressions as E
E = 1/(x-1) + 2/(x+2)
E2 1 ───── + ───── x + 2 x - 1
Combining fractions and canceling common terms
sym.cancel(E)3⋅x ────────── 2 x + x - 2
The underscore character ’_’ >> denotes the previous output
sym.factor(_)3⋅x ─────────────── (x - 1)⋅(x + 2)
Entering a very complex polynomial expression as F
F = ( x - 1 + 1/(x-1) ) / ( x - 1 - 1/(x-1) )
F 1
x - 1 + ─────
x - 1
─────────────
1
x - 1 - ─────
x - 1
Simplifying a complex polynomial as far as possible
sym.simplify(F)2 x - 2⋅x + 2 ──────────── x⋅(x - 2)
Laplace Transform
Declare t and s as symbols for the Laplace transform, and define f
t, s = sym.symbols('t, s')
a = sym.symbols('a', real=True, positive=True)
f = sym.exp(-a*t)
fexp(-a*t)
Taking the Laplace transform
F = sym.laplace_transform(f, t, s)
F⎛ 1 ⎞ ⎜─────, -a, True⎟ ⎝a + s ⎠
Suppressing the conditions for existence of the Laplace transform
F = sym.laplace_transform(f, t, s, noconds=True)
F1 ───── a + s
Defining a function to simplify Laplace-transform calculations
def L(f):
return sym.laplace_transform(f, t, s, noconds=True)
def invL(F):
return sym.inverse_laplace_transform(F, s, t)L(f)1 ───── a + s
invL(F)-a⋅t ℯ ⋅θ(t)
denotes the unit step function
sym.Heaviside(t)θ(t)
sym.plot(sym.Heaviside(t), xlim=(-1, 3), ylim=(0, 1.5))<Figure size 640x480 with 1 Axes>
<sympy.plotting.plot.Plot at 0x7b32d9367550>
Substituting a specific number for a variable in a function
invL(F).subs({a: 2})-2⋅t ℯ ⋅θ(t)
Taking Laplace transforms of various functions
omega = sym.symbols('omega', real=True)
theta = sym.Function('theta')(t)
exp = sym.exp
sin = sym.sin
cos = sym.cos
functions = [1,
t,
exp(-a*t),
t*exp(-a*t),
t**2*exp(-a*t),
sin(omega*t),
cos(omega*t),
1 - exp(-a*t),
exp(-a*t)*sin(omega*t),
exp(-a*t)*cos(omega*t),
sym.diff(theta,t)
]
functions[1, t, exp(-a*t), t*exp(-a*t), t**2*exp(-a*t), sin(omega*t), cos(omega*t), 1 - exp(-a*t), exp(-a*t)*sin(omega*t), exp(-a*t)*cos(omega*t), Derivative(theta(t), t)]
Taking inverse Laplace transforms of various functions
L(sym.diff(theta,t)).subs({theta.subs({t:0}):0})s*LaplaceTransform(theta(t), t, s)
Fs = [L(f) for f in functions]
Fs[1/s, s**(-2), 1/(a + s), (a + s)**(-2), 2/(a + s)**3, omega/(omega**2 + s**2), s/(omega**2 + s**2), -1/(a + s) + 1/s, omega/(omega**2 + (a + s)**2), (a + s)/(omega**2 + (a + s)**2), s*LaplaceTransform(theta(t), t, s) - theta(0)]