简体   繁体   English

我正在编写飞机模拟,为什么我的计算变得不稳定?

[英]I'm coding an airplane simulation, why do my computations become unstable?

I am currently attempting to code a basic 2D simulation of an airplane's flight (computing acceleration, speed and position at each timestep).我目前正在尝试编写飞机飞行的基本 2D 模拟(计算每个时间步的加速度、速度和位置)。

I have finally achieved a satisfactory simulation but now I face a new problem : After a period of time my computations start to become instable and quickly diverge towards infinity.我终于实现了令人满意的模拟,但现在我面临一个新问题:一段时间后,我的计算开始变得不稳定并迅速向无穷大发散。 Example of instability .不稳定的例子

I have done some experiments and it seems that the higher the theta (angle between the ground and the axis of the plane) the later the instability comes (or does not even come).我做了一些实验,似乎 theta 越高(地面和平面轴之间的角度)越晚,不稳定出现(或什至不出现)。 However, for realistic results I need to be able to compute with values of theta between -15 and +15 degrees.但是,对于实际结果,我需要能够使用 -15 到 +15 度之间的 theta 值进行计算。

I think this might be due to the approximations made by Python regarding small values.我认为这可能是由于 Python 对小值所做的近似。 I have tried to use Decimals but the problem was the same.我尝试使用小数但问题是一样的。

Here is the link to the code : https://github.com/YannBerthelot/PlaneModel/blob/master/Plane-Env/env/FlightModel_2.py这是代码的链接: https : //github.com/YannBerthelot/PlaneModel/blob/master/Plane-Env/env/FlightModel_2.py

Does anyone have an idea on how I can suppress or at least greatly reduce instability (making it happen after a much longer number of timesteps)?有没有人知道我如何抑制或至少大大减少不稳定性(使其在更长的时间步数后发生)? The problem may come from a wrong definition of the dynamics, if you have ideas on how to improve it please share :)问题可能来自对动力学的错误定义,如果您有关于如何改进它的想法,请分享:)

Thank you谢谢

Without any code it is not possible to find out exactly what is wrong.如果没有任何代码,就不可能准确找出问题所在。 It is likely that you are using trigonometric functions like "tan" with inut values that yield values close to infinity - in this case even the smallest error in the way computers deal with numbers (not a Python peculiarity) will multiply enormously很可能您正在使用诸如“tan”之类的三角函数,其 inut 值产生接近无穷大的值 - 在这种情况下,即使计算机处理数字的方式(不是 Python 特性)中最小的错误也会成倍增加

In [15]: math.tan(math.pi/2 - 0.000001)                                                                                                             
Out[15]: 1000000.000020701

In [16]: math.tan(math.pi/2 - 0.000002)                                                                                                             
Out[16]: 499999.99996964744

What you have to do is to identify in your formula where is that you are getting these runaway values, and solve it symbolically, getting to an equivalent formula that won't need to calculate numeric results at these regions.您需要做的是在您的公式中确定您在哪里获得这些失控值,并象征性地解决它,获得不需要计算这些区域的数值结果的等效公式。

Then, the standard float precision of ~15 decimal places should more than suffice.然后,约 15 位小数的标准浮点精度应该就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM