gpt4 book ai didi

python - 使用 odeint 查找零值

转载 作者:行者123 更新时间:2023-11-28 23:06:13 25 4
gpt4 key购买 nike

如何使用 scipy.integrate.ode 找到方程的一阶导数等于 0 的点?

我设置了这个函数,它得到了答案,但我不确定准确性,它不是最有效的方法。

基本上,我使用此函数来查找具有初始速度的射弹停止移动的时间。对于 ODE 系统,是否有更好的方法来解决这个问题?

import numpy as np
from scipy import integrate

def find_nearest(array,value):
idx=(np.abs(array-value)).argmin()
return array[idx], idx

def deriv(x,t):
# This function sets up the following relations
# dx/dt = v , dv/dt = -(Cp/m)*(4+v^2)
return np.array([ x[1], -(0.005/0.1) * (4+ (x[1]**2)) ])

def findzero(start, stop, v0):
time = np.linspace(start, stop, 100000)
#xinit are initial vaules of equation
xinit = np.array([0.0,v0])
x = integrate.odeint(deriv,xinit,time)
# find nearest velocity value nearest to 0
value, num = find_nearest(x[:,1],0.0001)
print 'closest value ',
print value
print 'reaches zero at time ',
print time[num]
return time[num]
# from 0 to 20 seconds with initial velocity of 100 m/s
b = findzero(0.0,20.0,100.0)

最佳答案

一般来说,解决此类问题的一个好方法是重写您的方程式,使速度成为自变量,时间和距离成为因变量。然后,您只需对从 v=v0 到 v=0 的方程进行积分。

但是,在您给出的示例中,甚至根本没有必要求助于 scipy.integrate。这些方程可以很容易地用铅笔和纸求解(变量分离后跟一个标准积分)。结果是

t = (m/(2 Cp)) arctan(v0/2)

其中 v0 是初始速度,arctan 的结果必须以弧度为单位。

对于 100 m/s 的初始速度,答案是 15.5079899282 秒。

关于python - 使用 odeint 查找零值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5267261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com