showmenu:
PRINT " Type a problem number and press ENTER." PRINT " 1) Particle in r^-1 central potential" PRINT " a) Perturbed motion ......................... Stable, Symplectic" PRINT " b) Elliptical motion ........................ Stable, Symplectic" PRINT " c) Eccentric elliptical motion .............. Stable, Symplectic" PRINT " 2) Particle in r^-2 central potential" PRINT " a) Attempted circular motion ................ Unstable, Symplectic" PRINT " 3) Mass on a spring" PRINT " a) Initial Momentum ..........*Incorrect*.... Unstable, Euler" PRINT " b) Initial Displacement ......*Incorrect*.... Unstable, Euler" PRINT " 4) Two equal masses connected by three springs" PRINT " a) Symmetric mode ........................... Stable, Symplectic" PRINT " b) Antisymmetric mode ....................... Stable, Symplectic" PRINT " c) Perturbed motion ......................... Stable, Symplectic" PRINT " 5) Plane pendulum (Part I)" PRINT " a) Initial Momentum ..........*Incorrect*.... Unstable, Euler" PRINT " 6) Plane pendulum (Part III)" PRINT " a) Sub-critical case ........................ Stable, RK4" PRINT " b) Over-critical case ....................... Stable, RK4" PRINT " 7) Plane pendulum (Part III)" PRINT " a) Initial Displacement ..................... Stable, Symplectic" PRINT " b) Sub-critical case ........................ Stable, Symplectic" PRINT " c) Over-critical case ....................... Stable, Symplectic" PRINT " 8) Plane pendulum (Part IV)" PRINT " a) Initial Displacement ..................... Stable, Mixed Euler";
"" INPUT " Enter a problem (ex. 1a): ", problem$
' Initial conditions
q10 = 0
p10 = 0
q20 = 0
p20 = 0
dt = .003
cyclic = 1
delayfactor = 1000
scalebig = 20
scalesmall = dt * 5
m1 = 1
m2 = 1
g = 1
q10 = 1: p10 = -.2: q20 = 0: p20 = 1
q10 = .5: p10 = 1: q20 = -.5: p20 = 1
q10 = 1: p10 = .5: q20 = 0: p20 = 1.15
dt = .001
cyclic = 0
delayfactor = 1000
scalebig = 20
scalesmall = dt * 10
m1 = 1
m2 = 1
g = 1
q10 = 1: p10 = 0: q20 = 0: p20 = 1.22437
dt = .03
cyclic = 0
delayfactor = 1000
scalebig = 10
scalesmall = dt * 7.5
m = 1
k = 1
q10 = 0: p10 = 2
q10 = 2: p10 = 0
dt = .003
cyclic = 1
delayfactor = 50000
scalebig = 12
scalesmall = dt * 7.5
m = 1
k = 1
q10 = 0: p10 = 2: q20 = 0: p20 = 2
q10 = 2: p10 = 0: q20 = -2: p20 = 0
q10 = 2: p10 = 0: q20 = 0: p20 = 0
dt = .03
cyclic = 1
delayfactor = 500000
scalebig = 10
scalesmall = dt * 5
m = 1
g = 1
l = 1
q10 = 0: p10 = 1.5: scalebig = 10
dt = .03
cyclic = 1
delayfactor = 500000
scalebig = 10
scalesmall = dt * 5
m = 1
g = 1
l = 1
q10 = 0: p10 = 2.19
q10 = 0: p10 = 2.25
dt = .03
cyclic = 1
delayfactor = 500000
scalebig = 10
scalesmall = dt * 5
m = 1
g = 1
l = 1
q10 = 0: p10 = -1.5
q10 = 0: p10 = 1.999
q10 = 0: p10 = 2.001
' Main loop.
iterations = 0
q1 = q10
p1 = p10
q2 = q20
p2 = p20
q1temp = q1
p1temp = p1
q2temp = q2
p2temp = p2
' Particle in r^-1 central potential - Symplectic integrator
q1 = q1temp + dt * (p1temp / m2)
q2 = q2temp + dt * (p2temp / m2)
p1 = p1temp - dt * g * m1 * m2 * (q1 / ((q1 ^ 2 + q2 ^ 2) ^ (3 / 2)))
p2 = p2temp - dt * g * m1 * m2 * (q2 / ((q1 ^ 2 + q2 ^ 2) ^ (3 / 2)))
' Particle in r^-2 central potential - Symplectic integrator
q1 = q1temp + dt * (p1temp / m2)
q2 = q2temp + dt * (p2temp / m2)
p1 = p1temp - dt * g * m1 * m2 * ((3 / 2) * q1 / ((q1 ^ 2 + q2 ^ 2) ^ (5 / 2)))
p2 = p2temp - dt * g * m1 * m2 * ((3 / 2) * q2 / ((q1 ^ 2 + q2 ^ 2) ^ (5 / 2)))
' Mass on a spring - Forward Euler method
q1 = q1temp + (p1temp / m) * dt
p1 = p1temp - (q1temp * k) * dt
' Mass on a spring - Backward Euler method
q2 = (q2temp + (p2temp / m) * dt) / (1 + dt ^ 2)
p2 = (p2temp - (q2temp * k) * dt) / (1 + dt ^ 2)
' Two equal masses connected by three springs - Symplectic integrator
q1 = q1temp + m * (p1temp) * dt
p1 = p1temp - dt * k * (2 * (q1temp + m * (p1temp) * dt) - (q2temp + m * (p2temp) * dt))
q2 = q2temp + m * (p2temp) * dt
p2 = p2temp - dt * k * (2 * (q2temp + m * (p2temp) * dt) - (q1temp + m * (p1temp) * dt))
' Plane pendulum - Forward Euler method
q1 = q1temp + (p1temp / m) * dt
p1
= p1temp
- (g
/ l
) * SIN(q1temp
) * dt
' Plane pendulum - Runge-Kutta 4th
k1w
= -(g
/ l
) * SIN(q1temp
) k1t = p1temp
w2 = p1temp + k1w * dt / 2
t2 = q1temp + k1t * dt / 2
k2t = w2
w3 = p1temp + k2w * dt / 2
t3 = q1temp + k2t * dt / 2
k3t = w3
w4 = p1temp + k3w * dt
t4 = q1temp + k3t * dt
dwdt = (k1w + 2 * k2w + 2 * k3w + k4w) / 6
dtdt = (k1t + 2 * k2t + 2 * k3t + k4t) / 6
p1 = p1temp + dwdt * dt
q1 = q1temp + dtdt * dt
' Plane pendulum - Symplectic integrator
q1 = q1temp + dt * (p1temp / m)
p1
= p1temp
- dt
* (g
/ l
) * (SIN(q1
)) ' Plane pendulum - Modified Euler method
q1 = q1temp + (p1temp / m) * dt
p1
= p1temp
- (g
/ l
) * SIN(q1
) * dt
x = (iterations * scalesmall - 180)
CALL cpset
(x
, (q1
* scalebig
+ 160), Yellow
) ' q1 plot CALL cpset
(x
, (p1
* scalebig
+ 60), Yellow
) ' p1 plot CALL cpset
(x
, (q2
* scalebig
- 60), Red
) ' q2 plot CALL cpset
(x
, (p2
* scalebig
- 160), Red
) ' p2 plot iterations = 0
' Phase portrait
CALL cpset
((q1temp
* (2 * scalebig
) + (190 + (320 - _WIDTH / 2))), (p1temp
* (2 * scalebig
) + 100), Yellow
) CALL cpset
((q1
* (2 * scalebig
) + (190 + (320 - _WIDTH / 2))), (p1
* (2 * scalebig
) + 100), Gray
) CALL cpset
((q2temp
* (2 * scalebig
) + (190 + (320 - _WIDTH / 2))), (p2temp
* (2 * scalebig
) + 100), Red
) CALL cpset
((q2
* (2 * scalebig
) + (190 + (320 - _WIDTH / 2))), (p2
* (2 * scalebig
) + 100), White
)
' Position portrait
CALL cpset
((q1temp
* (2 * scalebig
) + (190 + (320 - _WIDTH / 2))), (q2temp
* (2 * scalebig
) - 100), Blue
) CALL cpset
((q1
* (2 * scalebig
) + (190 + (320 - _WIDTH / 2))), (q2
* (2 * scalebig
) - 100), White
)
' System portrait - Requires 2x wide window.
CALL cline
(160, 0, 220 + 20 * q1
, 0, Green
) CALL cline
(220 + 20 * q1
, 0, 380 + 20 * q2
, 0, Yellow
) CALL cline
(380 + 20 * q2
, 0, 440, 0, Green
) CALL ccircle
(220 + 20 * q1temp
, 0, 15, Black
) CALL ccircle
(220 + 20 * q1
, 0, 15, Blue
) CALL ccircle
(380 + 20 * q2temp
, 0, 15, Black
) CALL ccircle
(380 + 20 * q2
, 0, 15, Red
) CALL cline
(200, 0, 400, 0, White
) CALL cline
(300, 0, 300 + 100 * SIN(q1temp
), -100 * COS(q1temp
), Black
) CALL cline
(300, 0, 300 + 100 * SIN(q1
), -100 * COS(q1
), Blue
)
iterations = iterations + 1
drawaxes:
' Axis for q1 plot
' Axis for p1 plot
' Axis for q2 plot
' Axis for p2 plot
' Axes for q & p plots
CALL cline
((190 + (320 - _WIDTH / 2)), 80 + 100, (190 + (320 - _WIDTH / 2)), -80 + 100, Gray
) CALL cline
((190 + (320 - _WIDTH / 2)) - 100, 100, (190 + (320 - _WIDTH / 2)) + 100, 100, Gray
) CALL cline
((190 + (320 - _WIDTH / 2)), -80 - 100, (190 + (320 - _WIDTH / 2)), 80 - 100, Gray
) CALL cline
((190 + (320 - _WIDTH / 2)) - 100, -100, (190 + (320 - _WIDTH / 2)) + 100, -100, Gray
)