微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何正确更新我的集成函数数组?

如何解决如何正确更新我的集成函数数组?

我一直在尝试使用 Runge-Kutta45 积分方法来更新空间中粒子的一组位置和速度,以获得某个时间步的新状态。

最初,我创建了一个包含所有这些元素的数组并将它们组合在一起 (y):

r_vec_1 = np.array([0,0])
v_vec_1 = np.array([-np.sqrt(2),-np.sqrt(2)])

r_vec_2 = np.array([-1,0])
v_vec_2 = np.array([np.sqrt(2) / 2,np.sqrt(2) / 2])

r_vec_3 = np.array([1,0])
v_vec_3 = np.array([np.sqrt(2) / 2,np.sqrt(2) / 2])

y_0 = np.concatenate((r_vec_1,v_vec_1,r_vec_2,v_vec_2,r_vec_3,v_vec_3))
y = y_0

现在,我使用这个数组作为我的初始条件并创建了一个函数,该函数为我提供了一个名为 F(y) 的新函数,它是我的函数 y 的导数,以一组一阶 ODE 表示:

def fun(t,y):
    np.array([y[2],y[3],x1_double_dot(y,mass_vector),y1_double_dot(y,y[6],y[7],x2_double_dot(y,y2_double_dot(y,y[10],y[11],x3_double_dot(y,y3_double_dot(y,mass_vector)])

获得新的函数文件后,我使用了scipy.integrate.RK45子程序中所需的初始时间和最终时间以及时间步长,结果如下:

#This will keep track of the y vector over time
history = [y_0]

#Time start,step,and finish point
t_0 = 0
t = 0
t_step = 0.01
t_final = 200
nsteps = int((t_final - t_0)/t_step)

#The loop for running the Runge-Kutta method over some time period.
for step in range(nsteps):
    y_new = sp.integrate.RK45(fun,t_0,y_0,t_final)
    history.append([y_new.y])
    y = y_new.y
    t += t_step

我想让RK45函数积分,然后把新得到的y函数放回RK45函数继续积分,直到时间结束。

目前,该方法为我的历史记录数组中的所有值输出初始 y 函数 (y_0),但我想要一段时间内所有 y 函数的更新列表。

任何建议将不胜感激,祝您有美好的一天!

解决方法

RK45 是一个步进器,它不与自身集成。在循环中,您只是重复调用此类的新实例的构造函数。由于您没有调用 .step() 方法,因此步进器在初始点保留在其数据中。

stepper = sp.integrate.RK45(fun,t_0,y_0,t_final)
for step in range(nsteps):
    stepper.step()
    history.append([stepper.t,*stepper.y])

您可能对步进器类实现自适应步长控制感兴趣,因此您还需要记录时间。

或者,如果您想使用 solve_ivp 选项模拟 t_eval 的基本功能,请这样做

t0,tf,dt = 0,10,0.1
stepper = RK45(lambda t,y: [y[1],-np.sin(y[0])],t0,[0.5,0],tf)
history = [stepper.y]
times = np.arange(t0,dt)
for t in times[1:]:
    if t > stepper.t:
        while t>stepper.t: stepper.step()
        sol = stepper.dense_output()
    history.append(sol(t))
plt.plot(times,history); plt.grid()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。