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