4.2 Derivatives, Integrals, Laplace Transforms, Differential Equations¶



Updated 6 JUN 2023
D. Falkenburg

Ex 4.2.1 Derivatives¶

We use the sympy method .diff(x) applied to the function f, and x is the variable we take the derivative with respect to. Again, f could be a function of several variables, but we are taking the derivative with respect to the one specified as the second argument. Here is an example: $\frac{d}{dx}\big(\frac{1+sinx}{1-cosx}\big)^2$

Line 4 NOTE: YOU MUST USE STANDARD PYTHON ARITMETIC OPERATORS IN THE FUNCTION, but special functions (trig, exp, etc. must be taken from the SymPy library e.g. smp.sin(x)

In [1]:
# Ex 4.2.1
import sympy as smp
smp.init_printing()
x=smp.symbols('x')
f=((1+smp.sin(x))/(1-smp.cos(x)))**2
f.diff(x)
Out[1]:
$\displaystyle \frac{2 \left(\sin{\left(x \right)} + 1\right) \cos{\left(x \right)}}{\left(1 - \cos{\left(x \right)}\right)^{2}} - \frac{2 \left(\sin{\left(x \right)} + 1\right)^{2} \sin{\left(x \right)}}{\left(1 - \cos{\left(x \right)}\right)^{3}}$

Ex 4.2.2 Partial Derivatives¶

Consider $ x^3+y^2-6xy$

Line 5 defines the function to be differentiated.

Line 6 by putting writing the list [f.diff(x) , f.diff(y)] we will get a list of the two partial derivatives.

In [2]:
# Ex 4.2.2
import sympy as smp
smp.init_printing()
x,y=smp.symbols('x y')
f=x**3+y**2-6*x*y
[f.diff(x),f.diff(y)]
Out[2]:
$\displaystyle \left[ 3 x^{2} - 6 y, \ - 6 x + 2 y\right]$

4.2.3 Integrals¶

Suppose we want to find $\int(x\sin(x))$ NOTE: Sympy does not add a constant to indefinite integrals! In the second example we form the definite integral $\int_{-\pi}^\pi(x\sin(x))dx$

In [3]:
# Ex 4.2.3
import sympy as smp
smp.init_printing()
x=smp.symbols('x')
smp.integrate(x*smp.sin(x),x)
Out[3]:
$\displaystyle - x \cos{\left(x \right)} + \sin{\left(x \right)}$

4.2.4 Alternate Form Integrating¶

This is the same problem as Ex 4.1.12 except we (line 3) define the function f then apply the integration method to the function (line 4).

In [4]:
# Ex 4.2.4
import sympy as smp
x=smp.symbols('x')
f=x*smp.sin(x)
smp.integrate(f,x)
Out[4]:
$\displaystyle - x \cos{\left(x \right)} + \sin{\left(x \right)}$

Ex 4.2.5 Definite Integrals¶

Consider $\int_1^t(x^{10} \exp(x)dx)$

Looks straight forward, but this requires multiple integration by parts to crank out the answer. Note, the upper limit of this definite integral is actually a symbol; we can substitute later.

In [5]:
# Ex 4.2.5
import sympy as smp
smp.init_printing()
x,t=smp.symbols('x t')
f=x**10*smp.exp(x)
smp.integrate(f, (x,1,t))
Out[5]:
$\displaystyle \left(t^{10} - 10 t^{9} + 90 t^{8} - 720 t^{7} + 5040 t^{6} - 30240 t^{5} + 151200 t^{4} - 604800 t^{3} + 1814400 t^{2} - 3628800 t + 3628800\right) e^{t} - 1334961 e$

Ex 4.2.6 Laplace Transform¶

Consider the function f(t)=5tu(t). This is a ramp of slope 5. The u(t) is the Heavyside function which is zero for all negative time and 1 for t>=0. The Laplace Transform is given by
$$L(f)=\int_0^\infty f(t) e^{-st}dt$$

Discussion of Results
If you have used the Lapace Transform before, you will recognize the solution $\frac{5}{s^2}$. The remainder of the output has to do with convergence conditions. For well behaved functions which engineers use, this causes no issue. Remember s is a complex variable. The issue re the condition can be understood by referring to http://scipp.ucsc.edu/~haber/ph116A/arg_11.pdf

In [6]:
# Ex 4.2.6
import sympy as smp
smp.init_printing()
s,t=smp.symbols('s t')
f=(5*t)*smp.exp(-s*t)
smp.integrate(f,(t,0,smp.oo))
Out[6]:
$\displaystyle \begin{cases} \frac{5}{s^{2}} & \text{for}\: \left|{\arg{\left(s \right)}}\right| < \frac{\pi}{2} \\\int\limits_{0}^{\infty} 5 t e^{- s t}\, dt & \text{otherwise} \end{cases}$

Ex 4.2.7 Finding Laplace Transform Using .laplace_transform(f, t, s)¶

Sympy provides a method .laplace_transform(f,t,s,noconds=True) which given f(t) computes F(s). By including noconds=True you will get only F(s). Leaving this out will produce a tuple which includes convergence conditions. For well-behaved functions that engineers use, the convergence conditions are not necessary.

In [7]:
# Ex 4.2.7
import sympy as smp
smp.init_printing()
s,t=smp.symbols('s t')
f= smp.exp(-2*t)*smp.sin(5*t)
smp.laplace_transform(f,t,s, noconds=True)
Out[7]:
$\displaystyle \frac{5}{\left(s + 2\right)^{2} + 25}$

Ex 4.2.8 Inverse Laplace Transform¶

Line 6 Given $F(s)=\frac{4}{s+a}+\frac{7}{s+b}$, we want to compute f(t). In defining a and b as symbols, it is important to declare these to be real (line 5), elsewise sympy will interpret them as complex and return a complex solution.

$\theta (t)$ is the so-called Heavyside function which in control theory we call the unit step function u(t).

In [8]:
# Ex 4.2.8
import sympy as smp
smp.init_printing()
s,t=smp.symbols('s t')
a,b=smp.symbols('a b', real=True)
F=(4/(s+a))+(7/(s+b))
smp.inverse_laplace_transform(F, s, t)
Out[8]:
$\displaystyle 7 e^{- b t} \theta\left(t\right) + 4 e^{- a t} \theta\left(t\right)$

Ex 4.2.9 Solving Differential Equations¶

We will solve the following differential equation analytically

$\frac{d^2 x}{dt^2}+3\frac{dx}{dt}+2 x=u(t)$; x=$\frac{dx}{dt}=0$ at t=0

When Solving the differential equation, we know that x(t) is a function of time. In line 4 we create a function object

a function of time t.

Line 5 Here we create an equation object named diffeq. Here, x.diff(t) is the first derivative of x with respect to time, while, x.diff(t,t) is the second derivative with respect to time.

Line 6 displays the differential equation.

Line 7 solves the differential equation diffeq for x. The last argument is a dictionary specifying the initial conditions. Read x.subs(t,0):0 as substitute a value of 0 into x(t) for t=0. The second key in the dictionary x.diff() takes on a value of 0 when t=0.

Line 8 we display the solution.

Reference

In [28]:
# Ex 4.2.9
import sympy as smp
smp.init_printing()
x=smp.Function("x")(t)
diffeq=smp.Eq(x.diff(t,t)+3*x.diff(t)+2*x,1)
display(diffeq)
sol=smp.dsolve(diffeq,x,ics={x.subs(t,0):0, x.diff().subs(t,0): 0})
display(sol)
$\displaystyle 2 x{\left(t \right)} + 3 \frac{d}{d t} x{\left(t \right)} + \frac{d^{2}}{d t^{2}} x{\left(t \right)} = 1$
$\displaystyle x{\left(t \right)} = \frac{1}{2} - e^{- t} + \frac{e^{- 2 t}}{2}$
In [ ]: