Python 当我开始的范围不是0时,scipy.integrate中的odeint出现问题
我正在写一个程序来解一个形式为x'(t)+w^2(t)*x(t)=0的微分方程,所以我使用了odeint。然而,当它不是从0开始时,它会把第一点放在0应该是什么 图片清楚地显示了这些问题Python 当我开始的范围不是0时,scipy.integrate中的odeint出现问题,python,numpy,scipy,differential-equations,odeint,Python,Numpy,Scipy,Differential Equations,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
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处的值。这就是第一个绘图中显示的内容。是否有一种方法可以始终使用x(0)和x'(0),尽管从其他值开始。