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

Ғылым және технология

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)

Пікірлер: 8

  • @andrewsawyer5003
    @andrewsawyer5003 Жыл бұрын

    This is my favorite Minecraft lets play series, keep up the good content

  • @Just_Jabi
    @Just_Jabi6 ай бұрын

    Incredible video, nice and clear explanation. 10/10

  • @zzzzzz3690
    @zzzzzz369010 ай бұрын

    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-lt1jk4cm1k9 ай бұрын

    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
    @andrewl979710 ай бұрын

    I LOVE YOU SO MUCH THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU

  • @TewodrosAbebaw-zd1mt
    @TewodrosAbebaw-zd1mt28 күн бұрын

    more important

Келесі