Python 3.x 使用scipy';s solve_ivp求解非线性摆运动
我仍在试图理解solve_ivp如何对抗odeint,但就在我掌握窍门的时候,发生了一些事情 我想解非线性摆的运动。使用odeint,无论发生什么奇怪的事情,在solve_ivp Hoep上,一切都像一个符咒:Python 3.x 使用scipy';s solve_ivp求解非线性摆运动,python-3.x,scipy,ode,Python 3.x,Scipy,Ode,我仍在试图理解solve_ivp如何对抗odeint,但就在我掌握窍门的时候,发生了一些事情 我想解非线性摆的运动。使用odeint,无论发生什么奇怪的事情,在solve_ivp Hoep上,一切都像一个符咒: import numpy as np from matplotlib import pyplot as plt from scipy.integrate import solve_ivp, odeint g = 9.81 l = 0.1 def f(t, r): omega
import numpy as np
from matplotlib import pyplot as plt
from scipy.integrate import solve_ivp, odeint
g = 9.81
l = 0.1
def f(t, r):
omega = r[0]
theta = r[1]
return np.array([-g / l * np.sin(theta), omega])
time = np.linspace(0, 10, 1000)
init_r = [0, np.radians(179)]
results = solve_ivp(f, (0, 10), init_r, method="RK45", t_eval=time) #??????
cenas = odeint(f, init_r, time, tfirst=True)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(results.t, results.y[1])
ax1.plot(time, cenas[:, 1])
plt.show()
我遗漏了什么?这是一个数值问题。
solve_ivp
的默认相对和绝对公差分别为1e-3和1e-6。对于许多问题,这些值太低。odeint
的默认相对公差为1.49e-8
如果将参数rtol=1e-8
添加到solve_ivp
调用,则曲线图一致:
import numpy as np
from matplotlib import pyplot as plt
from scipy.integrate import solve_ivp, odeint
g = 9.81
l = 0.1
def f(t, r):
omega = r[0]
theta = r[1]
return np.array([-g / l * np.sin(theta), omega])
time = np.linspace(0, 10, 1000)
init_r = [0, np.radians(179)]
results = solve_ivp(f, (0, 10), init_r, method='RK45', t_eval=time, rtol=1e-8)
cenas = odeint(f, init_r, time, tfirst=True)
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(results.t, results.y[1])
ax1.plot(time, cenas[:, 1])
plt.show()
绘图:
谢谢,这让我发疯了。但是,您知道我在哪里可以了解更多关于solve_ivp的信息吗?我已经阅读了好几次文档,但它似乎有点让人不知所措。你能推荐点什么吗?
solve\u ivp
比odeint
更新很多,所以没有那么多代码可供参考(例如,在这里搜索只找到69个问题和答案)。如果在您体验了odeint
和solve_ivp
之后,您对如何改进文档有具体建议,您可以在上创建一个问题。