python - 当我开始范围不在 0 时,来自 scipy.integrate 的 odeint 问题
问题描述
我正在编写一个程序来求解 x''(t) + w^2(t)*x(t) = 0 形式的微分方程,所以我使用了 odeint。但是,只要它不是从 0 开始,它就会将第一点放在 0 应该是什么上
图片清楚地显示了问题
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
def omega(t):
return 1
def dU_dt(U, t):
return [U[1], -U[0] * ((omega(t)) ** 2)]
x0 = 0 # This is the initial condition x(0)
x1 = 1 # This is the initial condition x'(0)
U0 = [x0, x1]
pi = np.pi
start = -1
stop = 2 * pi
N = 10 ** 2
xs = np.linspace(start, stop, N)
Us, info = odeint(dU_dt, U0, xs, rtol=1e-10, full_output=True)
ys = Us[:, 0]
plt.xlabel('t')
plt.ylabel('x')
plt.axvline(x=0.0, color=(0, 0, 0))
plt.axhline(y=0.0, color=(0, 0, 0))
plt.plot(xs, ys)
for i in range(len(xs)):
print(xs[i], ys[i])
plt.show()
解决方案
“初始条件”并不意味着 t=0 时的值。给定的初始条件odeint
是第一个值处的t
值。在您的情况下,您有start = -1
,因此您U0
指定 t=-1 处的值。这就是您的第一个情节中显示的内容。
推荐阅读
- vba - 自动将已删除邮件中的电子邮件标记为已读 Outlook 2016 VBA
- azerothcore - 在哪里设置辛达苟萨的泰尔粉碎法术范围
- pyspark - AttributeError:“NoneType”对象在 model.surrogateDF 上没有属性“setCallSite”
- javascript - 在 Firefox 的页面中找到添加以下 html 的源
- java - 尝试按日期查询时如何使用 Spring JPA 进行自定义查询?
- c# - 从 CMD 控制台运行已发布的 Web (C# MVC)
- python - 使用 Plotly 绘制密度直方图
- windows - 作业成功运行时出现 Rundeck 错误
- date - 在 Markdown 文本中查找和替换日期格式
- python - 如何从头开始将map和reduce函数实现为python中的递归函数?