Updated 6 JUN 2023
D. Falkenburg
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)
# 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)
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.
# 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)]
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$
# Ex 4.2.3
import sympy as smp
smp.init_printing()
x=smp.symbols('x')
smp.integrate(x*smp.sin(x),x)
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).
# Ex 4.2.4
import sympy as smp
x=smp.symbols('x')
f=x*smp.sin(x)
smp.integrate(f,x)
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.
# 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))
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
# 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))
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.
# 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)
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).
# 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)
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.
# 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)