繁体   English   中英

当我的范围不是 0 时,来自 scipy.integrate 的 odeint 出现问题

[英]Issue with odeint from scipy.integrate when I start the range not at 0

我正在编写一个程序来求解 x''(t) + w^2(t)*x(t) = 0 形式的微分方程,所以我使用了 odeint。 但是,只要它不是从 0 开始,它就会首先指出 0 应该是什么

sin(t) 从 t = -1 开始

从 t = 0 开始时的 sin(t)

图片清楚地显示了问题

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 处的值。 这就是您的第一个情节中显示的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM