【问题标题】:Issue with odeint from scipy.integrate when I start the range not at 0当我开始范围不在 0 时,来自 scipy.integrate 的 odeint 问题
【发布时间】:2020-05-02 15:47:36
【问题描述】:

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

sin(t) when starting from t = -1

sin(t) when starting from 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()

【问题讨论】:

    标签: python numpy scipy differential-equations odeint


    【解决方案1】:

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

    【讨论】:

    • 有没有办法始终使用 x(0) 和 x'(0),尽管从其他值开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多