绘制轨迹(python)

2024-05-31 23:15:51 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我想绘制火箭轨迹,它给了我一个错误:float()参数必须是字符串或数字,而不是'function'。我想绘制一个失去质量以获得推力的火箭的整个轨迹。当燃料结束时,它描述了一个抛物线轨迹。可以更改问题的数据。这就是我所说的值,其中mo是火箭的初始质量,q是气体的流量(质量如何随时间变化),g是重力加速度,xo是初始位置,t是时间。

我的代码是:

    import math
    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline

数据:

    mo = 1500
    q = 2.5
    u = 6000
    vo = 0
    g = 9.8
    x0 = 0
    t = np.arange(0,1001)
    t

速度执行:

def v(t):
    return vo + u*(math.log(mo/(mo-q*t))-g*t)

位置执行:

def x(t):
    return x0 + vo*t - 0.5*g*t^2 + u*t*math.log(mo) + (u/q)*((mo - q*t)*math.log(mo - q*t) + q*t - mo*math.log(mo))

 for t in (0,100):
 plt.plot(x,t)
 plt.grid()

谢谢你帮我,我真的很感激。


Tags: 数据importlogmatplotlib轨迹asnp时间
2条回答

应该是

plt.plot(x(t), t)

而不是

plt.plot(x, t)

上面要做的是将每个(x,y)视为一组数据。这是不正确的,因为这是您的数据集((0, x(0)), (1, x(1))...)的集合。一种可读的方法是为x轴和y轴创建一个数组:

x_ = np.arange(0,100)
y_ = x(x_) # available in newer versions of numpy.

plt.plot(x_, y_)

有四个问题。

  • 你需要调用一个函数,而不是声明它,x(t)
  • 使用数组时不要使用math。而是使用numpy
  • python中的power被写成**,而不是^
  • 不要在对数内使用负值。

正确的代码可能如下所示:

import numpy as np
import matplotlib.pyplot as plt


mo = 1500
q = 2.5
u = 6000
vo = 0
g = 9.8
x0 = 0
t = np.arange(0,int(1500/2.5))

def v(t):
    return vo + u*(np.log(mo/(mo-q*t))-g*t)

def x(t):
    return x0 + vo*t - 0.5*g*t**2 + u*t*np.log(mo) + \
            (u/q)*((mo - q*t)*np.log(mo - q*t) + q*t - mo*np.log(mo))


plt.plot(x(t),t)
plt.grid()

plt.show()

生产

enter image description here

相关问题 更多 >