Python 为投射物设置动画';s轨迹蟒蛇

Python 为投射物设置动画';s轨迹蟒蛇,python,physics,projectile,Python,Physics,Projectile,现在实习期结束了,我遇到了一个问题,我正在比以前的问题更深入地学习python 我正在使用阿米特·萨哈(Amit Saha)的《用Python做数学》(Doing Math With Python)一书,我决定跳转到这本书中的“为投射物的轨迹设置动画”。我花了一个小时试图自己解决这个问题,然后又花了两天在互联网上,我仍然不明白为什么我会犯这样的错误 AttributeError:“float”对象没有属性“append” 如果我在代码中没有浮点数,那么它就根本不起作用,我得到了这个结果 Type

现在实习期结束了,我遇到了一个问题,我正在比以前的问题更深入地学习python

我正在使用阿米特·萨哈(Amit Saha)的《用Python做数学》(Doing Math With Python)一书,我决定跳转到这本书中的“为投射物的轨迹设置动画”。我花了一个小时试图自己解决这个问题,然后又花了两天在互联网上,我仍然不明白为什么我会犯这样的错误

AttributeError:“float”对象没有属性“append”

如果我在代码中没有浮点数,那么它就根本不起作用,我得到了这个结果

TypeError:需要浮点

我希望在我们离开高中物理中的抛射运动单元之前完成这个项目,就像我学会做的一件很酷的事情一样。请帮忙。我可以让它绘制轨迹,但不需要设置动画:(

从matplotlib导入pyplot作为plt
从matplotlib导入动画
输入数学
g=9.8
def get_间隔(u,θ):
t_flight=2*u*math.sin(θ)/g
间隔=[]
开始=0
间隔=0.005
启动
您的代码非常接近!现在有一个错误,因为变量
interval
定义了两次,变量
interval
从未定义过。因此将
interval=0.005
更改为
interval=0.005
,如下代码所示:

def get_intervals(u, theta):

    t_flight = 2*u*math.sin(theta)/g
    intervals = []
    start = 0
    interval = 0.005
    while start < t_flight:
        intervals.append(start)
        start = start + interval
    return intervals

get\u interval
中,您编写:
interval=0.005
然后
interval.append(…
)。难怪这不起作用。只需删除
interval=0.005
中的
s
def get_intervals(u, theta):

    t_flight = 2*u*math.sin(theta)/g
    intervals = []
    start = 0
    interval = 0.005
    while start < t_flight:
        intervals.append(start)
        start = start + interval
    return intervals
def create_animation(u, theta):
    intervals = get_intervals(u,theta)

    xmin = 0
    xmax = u*math.cos(theta)*intervals[-1]
    ymin = 0
    t_max = u*math.sin(theta)/g
    ymax = u*math.sin(theta)*t_max - 0.5*g*t_max**2

    plotmax = max(xmax, ymax) # Pick the largest dimension of the two
    fig = plt.gcf()

    # Set both maxima to the same value to make a square plot
    ax = plt.axes(xlim=(xmin, plotmax), ylim=(ymin, plotmax)) 

    # Make sure the two axes are scaled the same...
    #    (we want a circle.. not a messed up oval)
    ax.set_aspect('equal')


    rad = plotmax/20. # Make sure the circle doesn't dominate the plot

    circle = plt.Circle((xmin, ymin), rad) # Use rad instead of 1.0
    ax.add_patch(circle)
    anim = animation.FuncAnimation(fig, update_position,
                        fargs=(circle, intervals, u, theta),
                        frames=len(intervals), interval=1,
                        repeat=False)

    plt.title('Projectile Motion')
    plt.xlabel('X [m]') # Units are nice :)
    plt.ylabel('Y [m]')
    plt.show()