Python solves first-order ordinary differential equations

1. Use python to solve first-order ordinary differential equations

1. To solve differential equations, you need to use the scipy library, which can be installed in pycharm. At the same time, you need to import the numpy library and matplotlib.

2. Use scipy.integrate.odeint() to solve, which generally refers to using three parameters and defaulting to other parameters.

func: derivative function f(y,t), that is, the derivative of y at t, expressed in functional form

y0: initial condition y0, expressed in the form of an array

t: Solve the sequence of time points corresponding to the function value. The first element of the sequence is the initial time t0 corresponding to the initial condition y0; the time series must be monotonically increasing or monotonically decreasing, and repeated values ​​are allowed

scipy.integrate.odeint(func, y0, t, args=(), Dfun=None, col_deriv=0, full_output=0, ml=None, mu=None, rtol=None, atol=None, tcrit=None, h0=0.0, hmax=0.0, hmin=0.0, ixpr=0, mxstep=0, mxhnil=0, mxordn=12, mxords=5, printmessg=0, tfirst=False)
3.举例

 For this function

#导入库函数
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt 
#定义函数的导数
def dy_dt(y,t):
    return np.sin(t**2)
#给定初始值和时间范围
y0=[1]
t = np.arange(-10,10,0.01)
#使用odeint()方法:
y=odeint(dy_dt,y0,t)
#绘图
plt.plot(t, y)
plt.title("picture")
plt.show()

Guess you like

Origin blog.csdn.net/weixin_68479946/article/details/128931459