绘制轨迹(python)

Plot trajectory (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()

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

应该是

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中的幂写成**,而不是^
  • 不要在对数中使用负值。

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

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()

生产