代码之家  ›  专栏  ›  技术社区  ›  fghjk

绘制轨迹(python)

  •  0
  • fghjk  · 技术社区  · 6 年前

    嗨,我想绘制一个火箭轨迹,它给了我一个错误:float()参数必须是字符串或数字,而不是“函数”。我想画出一枚火箭的整个轨道,它正在失去质量以获得推力。当燃料结束时,它描述一条抛物线轨迹。可以更改问题的数据。这些是我输入的值,其中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()
    

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

    2 回复  |  直到 6 年前
        1
  •  0
  •   ImportanceOfBeingErnest    6 年前

    有四个问题。

    • 您需要调用函数,而不是声明它, 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

        2
  •  0
  •   OneRaynyDay    6 年前

    应该是这样的

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