How to: Solve an ODE in Python (Boundary Value Problem)

  Рет қаралды 7,669

HagesLab

HagesLab

Жыл бұрын

Learn out to numerically solve an ordinary differential equation (ODE) in Python using a built in solver for boundary value problems: "scipy.integrate.solve_bvp()".
Here I discuss a 2nd order ODE with 2 boundary conditions (i.e. boundary value problem - BVP). A 1D steady-state heat transfer problem is used as an example, with various boundary conditions (fixed temperature, radiation) and with the option for heat generation. In order to solve the BPV, the 2nd order ODE must be converted into a system of 1st order ODEs in the format required by "scipy.integrate.solve_bvp()".
The Python script and PDF of the notes can be found here: www.hageslab.com/Resources.ht...
Here we are using "Spyder" IDE with the numpy, scipy, and matplotlib libraries
Script (for the radiation example):
import numpy as np #For basic math functions
import scipy.integrate as intg #For advanced math functions
import matplotlib.pyplot as plt #For plotting
#Define Constants
sigma = 5.67e-8 #[W/m^2/K^4]
#Set-up Paramters
qgen = 0 #[W/m**3]
k = 40 #[W/m/K]
T1 = 273.15 #[K]
epsilon = 0.8
Tsurr = 500+273.15 #[K]
#Set-up Grid
L = 1 #[m]
nodes = 100
x = np.linspace(0,L,nodes)
#Define Equations & Boundary Conditions
def f(x,y):
return np.vstack((y[1],np.full_like(x,-qgen/k)))
def bc(ya,yb):
return np.array([ya[0]-T1,k*yb[1]+sigma*epsilon*(yb[0]**4-Tsurr**4)])
#Inital Guess
y0 = np.zeros((2,x.size))
#Solve
sol = intg.solve_bvp(f,bc,x,y0)
T = sol.y[0]
dTdX = sol.y[1]
#Surface Temps
TS1 = T[0]
TS2 = T[-1]
#Compute Flux
q = -k*dTdX
#Plot
plt.figure(1,dpi=120)
plt.yscale('linear')
plt.xscale('linear')
#plt.xlim(0,1)
plt.ylim(273.15,500+273.15)
plt.title("Temperature Profile")
plt.xlabel("Distance / m")
plt.ylabel("T / K")
plt.plot(x,T)
plt.figure(2,dpi=120)
plt.yscale('linear')
plt.xscale('linear')
#plt.xlim(0,0.1)
#plt.ylim(0,100)
plt.title("Heat Flux")
plt.xlabel("Distance / m")
plt.ylabel("Flux / kW m$^{-1}$ K$^{-1}$")
plt.plot(x,q/1000)

Пікірлер: 7
@andrewsawyer5003
@andrewsawyer5003 Жыл бұрын
This is my favorite Minecraft lets play series, keep up the good content
@Just_Jabi
@Just_Jabi 5 ай бұрын
Incredible video, nice and clear explanation. 10/10
@zzzzzz3690
@zzzzzz3690 9 ай бұрын
thank you for video. it was so easy to understand how to use this function. really perfect you know, I had a problem with code, and I didn't know what I need to do to solve. the problem was in boundary conditions. It really easy to solve my problem)
@scottfinefrock1096
@scottfinefrock1096 Жыл бұрын
Thanks for excellent video. Flux is in W/m2.
@user-lt1jk4cm1k
@user-lt1jk4cm1k 8 ай бұрын
how do you solve a 3 order ODE, is it the same as this since we have 3 dimensions for y? and the boundary conditions also double.
@bellgrooversmeetup5570
@bellgrooversmeetup5570 Жыл бұрын
is there a way to solve these type of problems when you also have boundary conditions for the first derivative? e.g. a function that starts at (T, T') = (T0, 0) and ends at (T, T') = (0, 0) . That would mean 4 boundary conditions for a 1D problem, is it possible?
@andrewl9797
@andrewl9797 9 ай бұрын
I LOVE YOU SO MUCH THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU
How to: Import, Plot, Fit, and Integrate Data in Python
24:11
Eigenstates of ANY 1D Potential in PYTHON
19:41
Mr. P Solver
Рет қаралды 35 М.
Я нашел кто меня пранкует!
00:51
Аришнев
Рет қаралды 1,3 МЛН
The joker's house has been invaded by a pseudo-human#joker #shorts
00:39
Untitled Joker
Рет қаралды 14 МЛН
We Got Expelled From Scholl After This...
00:10
Jojo Sim
Рет қаралды 75 МЛН
Solving Differential Equations using scipy.odeint in Python!
25:10
How to Solve Coupled Differential Equations ODEs in Python
10:12
Vincent Stevenson
Рет қаралды 50 М.
Navier-Stokes Equations - Numberphile
21:03
Numberphile
Рет қаралды 1,1 МЛН
Laplace's Equation with Arbitrary Boundary Conditions in PYTHON
25:45
Coding a Fourth-Order Runge-Kutta Integrator in Python and Matlab
36:52
Eigenfunction Eigenvalue Problem
10:36
BriTheMathGuy
Рет қаралды 116 М.
SOLVING the SCHRODINGER EQUATION | Quantum Physics by Parth G
13:04
Secret Wireless charger 😱 #shorts
0:28
Mr DegrEE
Рет қаралды 2,3 МЛН
Lid hologram 3d
0:32
LEDG
Рет қаралды 10 МЛН
Спутниковый телефон #обзор #товары
0:35
Product show
Рет қаралды 1,7 МЛН
Will the battery emit smoke if it rotates rapidly?
0:11
Meaningful Cartoons 183
Рет қаралды 37 МЛН