首页 > 解决方案 > 当我开始范围不在 0 时,来自 scipy.integrate 的 odeint 问题

问题描述

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

sin(t) 从 t = -1 开始

sin(t) 从 t = 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()

标签: pythonnumpyscipydifferential-equationsodeint

解决方案


“初始条件”并不意味着 t=0 时的值。给定的初始条件odeint是第一个值处的t值。在您的情况下,您有start = -1,因此您U0指定 t=-1 处的值。这就是您的第一个情节中显示的内容。


推荐阅读