Python中的Verlet算法实现 [英] Verlet algorithm implementation in Python

查看:531
本文介绍了Python中的Verlet算法实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用Python实现Verlet算法时遇到问题.我尝试了这段代码:

I have problem with implementation of verlet algorithm in Python. I tried this code:

x[0] = 1
v[0] = 0
t[0] = 0
a[0] = 1
for i in range (0, 1000):
    x[i+1] = x[i] - v[i] * dt + (a[i] * (dt**2) * 0.5)
    v[i] = (x[i+1] - x[i-1]) * 0.5 * dt
    t[i+1] = t[i] + dt

但是它不能正常工作.怎么了? 我正在寻找Verlet算法的通用代码.

But it is not working properly. What is wrong? I am looking for general code for Verlet algorithm.

推荐答案

您的问题不是很清楚,但是您的代码中有一些错误来源.

Your question isn't very clear, but there are a few sources of error in your code.

例如,对于i> 0

x[i+1] = x[i]-v[i]*dt+(a[i]*(dt**2)*0.5)

尝试使用v[i]的值,但是该元素在v列表中尚不存在.

tries to use the value of v[i], but that element doesn't exist yet in the v list.

举一个具体的例子,当i = 1时,您需要v[1],但是在那个阶段的v列表中唯一的东西是v[0].直到下一行才计算v[1].

To give a concrete example, when i = 1, you need v[1], but the only thing in the v list at that stage is v[0]; v[1] isn't computed until the following line.

该错误应导致Python解释器显示错误消息:

That error should cause the Python interpreter to display an error message:

IndexError: list index out of range

当寻求有关堆栈溢出的帮助时将错误消息与您的问题一起发布,最好从以下行开始:

When asking for help on Stack Overflow please post the error messages with your question, preferably starting with the line:

Traceback (most recent call last):

这使人们阅读您的问题来调试代码变得更容易了.

That makes it a lot easier for people reading your question to debug your code.

FWIW,在for循环的第一次迭代中,当i == 0时,x[i+1]x[i-1]都引用相同的元素,因为x列表中有两个元素那个阶段,所以x[-1]x[1].

FWIW, on the first iteration of your for loop, when i == 0, x[i+1] and x[i-1] both refer to the same element, since there are two elements in the x list at that stage, so x[-1] is x[1].

此外,将t值存储在列表中也很奇怪.您不需要这样做.只需将t存储为简单的float值,并在循环中每次将其递增dt即可;请注意,t本身未在任何计算中使用,但您可能希望将其打印出来.

Also, it's strange that you are storing your t values in a list. You don't need to do that. Just store t as a simple float value and increment it by dt each time through the loop; note that t itself isn't used in any of the calculations, but you may want to print it.

对于速度Verlet .例如,在我之前引用的代码行中,您减去了v[i]*dt,但应该添加它.

Your formulas don't appear to match those given on the Wikipedia page, either for the Basic Störmer–Verlet, or the Velocity Verlet. For example, in the code line I quoted earlier you are subtracting v[i]*dt but you should be adding it.

也许您应该考虑实现相关的 Leapfrog集成方法. Leapfrog的同步版本易于编码,并且非常有效,IME.

Perhaps you should consider implementing the related Leapfrog integration method. The synchronised version of Leapfrog is easy to code, and quite effective, IME.

从Wikipedia链接:

From the Wikipedia link:

x[i+1] = x[i] + v[i] * dt + 0.5 * a[i] * dt * dt
v[i+1] = v[i] + 0.5 * (a[i] + a[i+1]) * dt

通常,没有必要将a值实际存储在列表中,因为它们将使用相关的力方程根据其他参数进行计算.

Generally, it's not necessary to actually store the a values in a list, since they will be calculated from the other parameters using the relevant force equation.

这篇关于Python中的Verlet算法实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆