为此摆码添加恒定加速度

时间:2011-05-11 22:21:54

标签: python performance acceleration

在使用此代码时,如何为此钟摆添加恒定加速度?现在的代码是描述一个钟摆,我怎么改变它来描述一个移动的火车(火车有恒定的加速度)的钟摆?任何帮助将不胜感激,谢谢你。

from math import sin, pi

from time import sleep

from turtle import *


GA = 9.80665 # Gravitational Acceleration (meters per second squared)

FORM = 'Time={:6.3f}, Angle={:6.3f}, Speed={:6.3f}'

def main():

    length = 10.0            # Of pendulum (meters)
    ngol = - GA / length    # Negative G over L
    total_time = 0.0        # Seconds
    angle = 1.0             # Initial angle of pendulum (radians)
    speed = 0.0             # Initial angular velocity (radians/second)
    time_step = 0.05        # Seconds
    acc = 1
    while total_time < 30.0:
        total_time += time_step
        speed += ngol * sin(angle) * time_step
        angle += speed * time_step
        #print(FORM.format(total_time, angle, speed))
        if draw(angle, length): break
        sleep(time_step)

def init():

    setup()
    mode('logo')
    radians()
    speed(0)
    hideturtle()
    tracer(False)
    penup()

def draw(angle, length):

    if speed() != 0: return True
    clear()
    setheading(angle + pi)
    pensize(max(round(length), 1))
    pendown()
    forward(length * 25)
    penup()
    dot(length * 10)
    home()
    update()

if __name__ == '__main__':

    init()
    main()
    bye()

2 个答案:

答案 0 :(得分:0)

对于恒定角加速度w(t):

acc = 1
while total_time < 30.0:
    ...
    angle += acc * speed * time_step
    acc += 0.1

有一篇很好的Simple harmonic motion维基百科文章,描述了摆动。

答案 1 :(得分:0)

在我看来,你只需要指定水平位置(假设你在一条只在水平方向上移动的火车上)到你的绘图功能。

def draw(angle, length, horiz_pos):

if speed() != 0: return True
clear()
forward(horiz_pos)
setheading(angle + pi)
pensize(max(round(length), 1))
pendown()
forward(length * 25)
penup()
dot(length * 10)
home()
update()

然后通过传递基于速度* time_step的位置来修改对draw()函数的调用,其中速度正在增加(即加速)。

acc = 1
while total_time < 30.0:
    total_time += time_step
    speed_horiz += accel_constant * time_step
    speed += ngol * sin(angle) * time_step
    pos += speed_horiz * time_step
    angle += speed * time_step
    if draw(angle, length, pos): break
    sleep(time_step)