阿里云网站建设——部署与发布,wordpress图床网站,制作相册的模板,seo链接优化建议文章目录odeint简介示例odeint简介
scipy文档中将odeint函数和ode, comples_ode这两个类称为旧API#xff0c;是scipy早期使用的微分方程求解器#xff0c;但由于是Fortran实现的#xff0c;尽管使用起来并不方便#xff0c;但速度没得说#xff0c;所以有的时候还挺推荐…
文章目录odeint简介示例odeint简介
scipy文档中将odeint函数和ode, comples_ode这两个类称为旧API是scipy早期使用的微分方程求解器但由于是Fortran实现的尽管使用起来并不方便但速度没得说所以有的时候还挺推荐使用的。
其中odeint的参数如下
scipy.integrate.odeint(func, y0, t, args(), DfunNone, col_deriv0, full_output0, mlNone, muNone, rtolNone, atolNone, tcritNone, h00.0, hmax0.0, hmin0.0, ixpr0, mxstep0, mxhnil0, mxordn12, mxords5, printmessg0, tfirstFalse)其中func为待求解函数y0为初值t为自变量列表其他参数都有默认选项可以不填而且这些参数非常多其中常用的有
args func中除了t之外的其他变量Dfun func的梯度函数当此参数不为None时若将col_deriv设为True则可提升效率。full_output 如果为True则额外返回一个参数字典mlNone, muNone, rtolNone, atolNone, tcritNone, h00.0, hmax0.0, hmin0.0, ixpr0, mxstep0, mxhnil0, mxordn12, mxords5,printmessg 为True时打印信息。tfirst 当为False时func的格式为func(y,t...)否则格式为func(t, y...)
示例
对于常微分方程
θ′′(t)bθ′(t)csinθ(t)0b0.25;c5θ(0)π−0.1;θ′(0)0\theta(t)b\theta(t)c\sin\theta(t)0\\ b0.25;\quad c5\\ \theta(0)\pi-0.1;\quad \theta(0)0 θ′′(t)bθ′(t)csinθ(t)0b0.25;c5θ(0)π−0.1;θ′(0)0
将其中的二阶导数项用一个新变量替代ω(t)θ′(t)\omega(t)\theta(t)ω(t)θ′(t)则常微分方程可拆分成微分方程组
θ′(t)ω(t)ω′(t)−bω(t)−csinθ(t)\begin{aligned} \theta(t)\omega(t)\\ \omega(t)-b\omega(t)-c\sin\theta(t) \end{aligned} θ′(t)ω′(t)ω(t)−bω(t)−csinθ(t)
令y[θ,ω]y[\theta, \omega]y[θ,ω]则y′[θ′,ω′]y[\theta, \omega]y′[θ′,ω′]据此可设计函数func
import numpy as np
def pend(y, t, b, c):th, om ydydt [om, -b*om - c*np.sin(th)]return dydt然后调用并求解
from scipy.integrate import odeint
y0 [np.pi-0.1, 0]
t np.linspace(0, 10, 101)
sol odeint(pend, y0, t, args(0.25, 5))然后绘制一下结果
import matplotlib.pyplot as plt
plt.plot(t, sol[:,0], labeltheta)
plt.plot(t, sol[:,1], labelomega)
plt.legend()
plt.show()这个形状还是比较离奇的。