PROGRAM pendula ! animation of linear and nonlinear pendula DIM ball$(2) LIBRARY "csgraphics" CALL initial(#1,#2,theta1,v1,theta2,v2,omega2,t,dt,dt_2,ball$(),r) DO CALL linear(theta1,v1,omega2,dt,dt_2,x1old) CALL nonlinear(theta2,v2,omega2,dt,dt_2,x2old) LET t = t + dt CALL animation(#1,theta1,x1old,ball$(1),r) CALL animation(#2,theta2,x2old,ball$(2),r) LOOP until key input CLOSE #1 CLOSE #2 END SUB initial(#1,#2,theta1,v1,theta2,v2,omega2,t,dt,dt_2,ball$(),r) LET t = 0 LET theta1 = 0.25 ! position (radians) of linear oscillator LET theta2 = theta1 ! position of nonlinear oscillator LET v1 = 0 LET v2 = v1 LET omega2 = 9 ! g/L ratio LET dt = 0.01 LET dt_2 = 0.5*dt LET xmax = theta1 ! dimension of window OPEN #1: screen 0,1,0,0.5 ! bottom half of screen CALL compute_aspect_ratio(xmax,xwin,ywin) SET WINDOW -xwin,xwin,-ywin,ywin PRINT "linear oscillator" ! draw circle SET COLOR "red" LET r = 0.1 ! radius of circle BOX CIRCLE theta1 - r,theta1 + r,- r,r ! store circle in string variable ball$ BOX KEEP theta1 - r, theta1 + r,-r,r in ball$(1) OPEN #2: screen 0,1,0.5,1 ! top half of screen CALL compute_aspect_ratio(xmax,xwin,ymin) SET WINDOW -xwin,xwin,-ywin,ywin PRINT "nonlinear oscillator" SET COLOR "blue" BOX CIRCLE theta2 - r,theta2 + r,- r,r BOX KEEP theta2 - r, theta2 + r,-r,r in ball$(2) END SUB SUB linear(theta,v,omega2,dt,dt_2,x1old) LET x1old = theta LET a = -omega2*theta LET vmid = v + a*dt_2 LET theta_mid = theta + v*dt_2 LET amid = -omega2*theta_mid LET v = v + amid*dt LET theta = theta + vmid*dt END SUB SUB nonlinear(theta,v,omega2,dt,dt_2,x2old) LET x2old = theta LET a = -omega2*sin(theta) LET vmid = v + a*dt_2 LET theta_mid = theta + v*dt_2 LET amid = -omega2*sin(theta_mid) LET v = v + amid*dt LET theta = theta + vmid*dt END SUB SUB animation(#9,theta,theta_old,circle$,r) WINDOW #9 LET x = theta_old ! erase old circle; using 0 same as box clear BOX SHOW circle$ at x - r,-r using 0 LET x = theta BOX SHOW circle$ at x - r,-r ! draw new circle END SUB