See discussions, stats, and author profiles for this publication at: https://www.researchgate.
net/publication/360109804
Solving Differential Equations using Python
Presentation · April 2022
DOI: 10.13140/RG.2.2.26067.04641
CITATIONS READS
0 5,734
1 author:
Shardav Bhatt
Navrachana University Vadodara
29 PUBLICATIONS 26 CITATIONS
SEE PROFILE
All content following this page was uploaded by Shardav Bhatt on 22 April 2022.
The user has requested enhancement of the downloaded file.
PPG College of Arts and Science
Department of Mathematics
Five Day International FDP on Mathematical Programming – 2022
20-25 April 2022
Day 3
Solving Differential
Equations using Python
22 April 2022
Shardav Bhatt
Navrachana University
Content 2
1. Downloading and Installing 7. System of ODEs
Python 8. Numerical solution of ODE
2. Python packages required 9. First order PDE
3. First order ODEs & IVPs
4. Plotting solutions of IVPs
5. Second order ODEs & IVPs
6. Higher order ODEs
Downloading and Installing Python 3
1. From official source: https://www.python.org/downloads/
2. From Anaconda Distribution: https://www.anaconda.com/products/distribution
3. Google Colab (No installation or Download): https://colab.research.google.com/
Python packages required 4
1. SymPy: https://www.sympy.org/en/index.html
For analytical solution of differential equations
2. SciPy: https://scipy.org/
For numerical solution of differential equations
• Anaconda distribution and Google colab both contains pre-installed
SymPy and SciPy.
Solving first order ODE 5
• Let’s start with a simple ODE
• Here is an independent variable
is a variable depending on
is derivative
Solving first order ODE 6
Mathematical
Type SymPy function Syntax
expression
𝑥 independent variable Symbol x = Symbol('x')
𝑦 variable depending on 𝑥 Function y = Function('y')(x)
𝑑𝑦
Derivative diff dydx = y.diff(x)
𝑑𝑥
𝑑𝑦
=𝑥 Differential equation Eq deq = Eq(dydx, x)
𝑑𝑥
General Solution of ODE dsovle dsolve(deq)
Solving first order ODE 7
• Step – 1: Import necessary SymPy functions
from sympy import Symbol, Function, Eq
• Step – 2: Define an equation
x = Symbol('x')
y = Function('y')(x)
dydx = y.diff(x)
deq = Eq(dydx, x)
• Step – 3: Solve ODE
from sympy import dsolve
solution = dsolve(deq)
Solving first order ODE 8
• Let’s solve another one
• Here we need to import a function exp from SymPy to represent .
• In python, can be written as y**2.
** is an operator in python to determine power.
Solving first order ODE 9
• Step – 1: Import necessary SymPy functions
from sympy import Symbol, Function, Eq, exp
• Step – 2: Define an equation
x = Symbol('x')
y = Function('y')(x)
dydx = y.diff(x)
deq = Eq(dydx, (x+1)*exp(-x)*y**2 )
• Step – 3: Solve ODE
from sympy import dsolve
solution = dsolve(deq)
Try Yourself! 10
Solve the following ODE
First order IVPs 11
• Initial value problems (IVPs) are ODE together with the initial condition.
• Consider an IVP,
• After finding a general solution, we use condition to
determine the arbitrary constant.
• For that we substitute and in the RHS of the general
solution and solve it.
First order IVPs 12
• Step 1: Find the general solution s using dsolve as steps shown earlier
s = dsolve(deq)
• Step 2: Substitute the values and in the RHS of s using subs
eq = s.rhs.subs( , 𝟎 )- 𝟎
• Step 3: Import solve function and solve the expression using it
From sympy import solve
constant = solve([eq])
• Step 4: Substitute value of constant in the general equation using subs
solution = s.subs(constant)
Plotting the solution of IVP 13
• Suppose solution is the variable in which the particular solution of IVP
is stored.
• We can plot it using the function plot from sympy.
• Step 1: Import necessary modules
from sympy.plotting import plot
• Step 2: Plot the solution stored in the variable named solution
plot(solution.rhs)
Code to solve an IVP
14
from sympy import Symbol, Function, Eq, exp, dsolve, solve
x = Symbol('x')
y = Function('y')(x)
dydx = y.diff(x)
deq = Eq(dydx, -2*x*y )
s = dsolve(deq)
eq = s.rhs.subs(x,0)-1.8
constant = solve([eq])
solution = s.subs(constant)
from sympy.plotting import plot
plot(solution.rhs)
Try yourself! 15
• Consider an initial value problem
• Find the general solution, particular solution and plot it.
Solving second order ODE 16
• Consider a second order ODE
• It is convenient to use function Derivative of SymPy to write higher
order derivatives.
• Derivative(y,x) gives
• Derivative(y,x,x) gives
Solving second order ODE 17
• Code:
from sympy import Symbol, Function, Derivative, Eq, dsolve
x = Symbol('x')
y = Function('y')(x)
deq = Eq(Derivative(y,x,x) + Derivative(y,x) - 2*y, 0)
solution = dsolve(deq)
Solving second order IVP 18
• Consider a second order IVP
• For second order IVP, there are two initial conditions.
• We use these conditions in the general solution, which will given two
equations in two unknowns.
• Two arbitrary constants can be obtained by solving these two equations.
Solving second order IVP 19
• Code (after getting the general solution s)
s = dsolve(deq)
eqn1 = s.rhs.subs(x,0) - 1
eqn2 = s.rhs.diff(x).subs(x,0) + 1
constants = solve([eqn1, eqn2])
solution = s.subs(constants)
plot(solution.rhs)
Try yourself! 20
• Solve the following second order IVP:
Higher order ODEs 21
• Suppose we are solving a third order ODE
with conditions
, ,
• The syntax and steps of code for solving third order are similar to the
that of second order.
Higher order ODEs 22
• Code to find General solution:
from sympy import Symbol, Function, Derivative, Eq, dsolve, exp
x = Symbol('x')
y = Function('y')(x)
deq = Eq(Derivative(y,x,x,x) + 3*Derivative(y,x,x) + 2*Derivative(y,x) + y, 30*exp(-x))
s = dsolve(deq)
Higher order ODEs 23
• Code to find Particular solution:
s = dsolve(deq)
eq1 = s.rhs.subs(x,0) - 3
eq2 = s.rhs.diff(x).subs(x,0) + 3
eq3 = s.rhs.diff(x, x).subs(x,0) + 47
constants = solve([eq1, eq2, eq3])
solution = s.subs(constants)
from sympy.plotting import plot
plot(solution.rhs, (x,-1,1))
Try yourself! 24
• Solve the following fifth order ODEs
• Solve the following fifth order ODEs
System of ODEs 25
• Suppose we want to solve a system of ODE
• The syntax and steps of code for solving system of ODE are similar to
that of solving a single ODE.
System of ODEs 26
• Code to find General solution:
from sympy import Symbol, Function, Derivative, Eq, dsolve, solve
x = Symbol('x')
y1 = Function('y1')(x)
y2 = Function('y2')(x)
deq1 = Eq(Derivative(y1), -0.02*y1 + 0.02*y2)
deq2 = Eq (Derivative(y2), 0.02*y1 - 0.02*y2)
system = (deq1, deq2)
s = dsolve(system)
System of ODEs 27
• Code to find Particular solution:
s = dsolve(deq)
eq1 = s[0].rhs.subs(x,0) - 0
eq2 = s[1].rhs.subs(x,0) - 150
constants = solve([eq1, eq2])
y1 = s[0].subs(constants)
y2 = s[1].subs(constants)
System of ODEs 28
• Plotting a particular solution:
from sympy.plotting import plot
plot(y1.rhs, y2.rhs, (x, -10, 10))
Try yourself! 29
• Solve the following system of ODEs
Numerical solution of ODE 30
• The numerical solution of ODE can be obtained using the SciPy package.
• This can be done using the solve_ivp function of scipy.integrate.
• We can implement Runge-Kutta method of order 5, Runge-Kutta
method of order 3, Implicit Runge-Kutta method and Adams/BDF
to find numerical solution.
Numerical solution of ODE 31
• solve_ivp can solve IVP of the form
• We have to write
solution = solve_ivp(fun, t_span, y0, t_eval=None, method='RK45')
• Here fun is , t_span is an interval from to the desired point, y0 is 𝟎
• t_eval is the list of points at which solution is needed
• We can use other algorithms by providing different names of the method.
Numerical solution of ODE 32
• Suppose we want to solve an IVP
• Code:
from scipy.integrate import solve_ivp
def fun(t, y):
return t + y + t*y
sol = solve_ivp(fun, t_span=[0, 1], y0=[1],
t_eval = [0, 0.2, 0.4, 0.6, 0.8, 1], method = 'RK45')
Numerical solution of ODE 33
• sol.t gives all the points of provided by t_eval
• sol.y gives numerical solution at points given by sol.t
• We can plot them,
from matplotlib.pyplot import plot
plot(sol.t, sol.y[0], 'o-')
Try yourself! 34
• Find the numerical solution of following IVP at points 1, 1.25, 1.5, 1.75, 2 & plot it.
First order PDE 35
• We can solve following type of PDEs using SymPy:
1. First order linear homogeneous partial differential equation
2. First order linear partial differential equation with constant coefficients
3. First order linear partial differential equation with variable coefficients
• PDE related functions are available in sympy.solvers.pde
First order PDE 36
Function Use
classify_pde Returns a classifications for a PDE.
Solves any (supported) kind of partial differential
pdsolve
equation.
Checks if the given solution satisfies the partial
checkpdesol
differential equation
Solves a first order linear homogeneous partial
pde_1st_linear_constant_coeff_homogeneous
differential equation with constant coefficients.
Solves a first order linear partial differential equation with
pde_1st_linear_constant_coeff
constant coefficients.
Solves a first order linear partial differential equation with
pde_1st_linear_variable_coeff
variable coefficients.
First order PDE 37
• Consider a PDE:
• Here is a function of and . We can define PDE using following code:
from sympy import Symbol, u = f(x, y)
Function, Eq ux = u.diff(x)
x = Symbol('x')
uy = u.diff(y)
y = Symbol('y')
eq = Eq(2*ux + 3*uy, 1)
f = Function('f')
First order PDE 38
from sympy.solvers.pde import classify_pde, pdsolve, checkpdesol
classify_pde(eq)
solution = pdsolve(eq)
checkpdesol(eq, solution)
What next? 39
• We can solve many other mathematical problems using python like:
1. Finding limit, derivative, integration
2. Vector & Matrix operations
3. Laplace transforms, Fourier transform, Wavelet transforms
4. Statistical methods and probability distributions
5. Optimization and LPP
6. Numerical methods
View publication stats
Thank you
[email protected]
https://www.researchgate.net/profile/Shardav-Bhatt-2
(Refer the supported code file for the outputs and solutions)