Solving Differential Equations Using Python Presentation
Solving Differential Equations Using Python Presentation
net/publication/360109804
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.
Solving Differential
Equations using Python
22 April 2022
Shardav Bhatt
Navrachana University
Content 2
1. SymPy: https://www.sympy.org/en/index.html
2. SciPy: https://scipy.org/
is a variable depending on
is derivative
Solving first order ODE 6
Mathematical
Type SymPy function Syntax
expression
𝑑𝑦
Derivative diff dydx = y.diff(x)
𝑑𝑥
𝑑𝑦
=𝑥 Differential equation Eq deq = Eq(dydx, x)
𝑑𝑥
• Initial value problems (IVPs) are ODE together with the initial condition.
• Consider an IVP,
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
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)
• Derivative(y,x) gives
• Derivative(y,x,x) gives
Solving second order ODE 17
• Code:
y = Function('y')(x)
solution = dsolve(deq)
Solving second order IVP 18
• We use these conditions in the general solution, which will given two
equations in two unknowns.
s = dsolve(deq)
eqn1 = s.rhs.subs(x,0) - 1
eqn2 = s.rhs.diff(x).subs(x,0) + 1
solution = s.subs(constants)
plot(solution.rhs)
Try yourself! 20
with conditions
, ,
• The syntax and steps of code for solving third order are similar to the
that of second order.
Higher order ODEs 22
y = Function('y')(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
solution = s.subs(constants)
plot(solution.rhs, (x,-1,1))
Try yourself! 24
• 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:
x = Symbol('x')
y1 = Function('y1')(x)
y2 = Function('y2')(x)
s = dsolve(system)
System of ODEs 27
s = dsolve(deq)
eq1 = s[0].rhs.subs(x,0) - 0
y1 = s[0].subs(constants)
y2 = s[1].subs(constants)
System of ODEs 28
• The numerical solution of ODE can be obtained using the SciPy package.
• We have to write
• 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
• Find the numerical solution of following IVP at points 1, 1.25, 1.5, 1.75, 2 & plot it.
First order PDE 35
Function Use
• Consider a PDE:
classify_pde(eq)
solution = pdsolve(eq)
checkpdesol(eq, solution)
What next? 39
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)