python求解一阶常微分方程

一.用python求解一阶常微分方程

1.求解微分方程需要用到scipy库,pycharm中安装即可,同时需要导入numpy库和matplotlib两个库

2.使用scipy.integrate.odeint()来进行求解,一般指使用三个参数,默认其他参数

func : 导数函数f(y,t),即y在t处的导数,用函数形式表示

y0 :初始条件y0,用数组的形式表示

t : 求解函数值对应的时间点的序列。序列的第一个元素是与初始条件 y0 对应的初始时间 t0;时间序列必须是单调递增或单调递减的,允许重复值

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.举例

 对于该函数而言

#导入库函数
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()

猜你喜欢

转载自blog.csdn.net/weixin_68479946/article/details/128931459