This program was written per R.T.Giles's second assignment in 10COF180 (November 25, 2010). A student under his instruction wanted help writing a program, and while not required, a GUI was placed on the program's output to visually demonstrate what was taking place within the program. The recipe below provides a simple simulation of a undamped pendulum and produces a visual representation for about thirty seconds. The code could be greatly improved but gives a demonstration of what a few turtle commands can do in Python. Very little code must be written to produce graphics on the computer screen.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | ```
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 = 9.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
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()
``` |