It uses the Runge-Kutta method. It's more complicated than mine.

Code: Select all

```
' FreeBasic swinging pendulum simulation using the Runge-Kutta method
screenres 800, 600, 32
dim shared as double g = 9.81 ' acceleration due to gravity
dim shared as double l = 200 ' length of the pendulum
function f(theta as double, omega as double) as double
return -g/l * sin(theta) ' differential equation for pendulum motion
end function
sub RungeKutta(theta as double, omega as double, dt as double, ByRef theta_new as double, ByRef omega_new as double)
dim as double k1_theta, k2_theta, k3_theta, k4_theta
dim as double k1_omega, k2_omega, k3_omega, k4_omega
k1_theta = omega
k1_omega = f(theta, omega)
k2_theta = omega + 0.5 * dt * k1_omega
k2_omega = f(theta + 0.5 * dt * k1_theta, omega + 0.5 * dt * k1_omega)
k3_theta = omega + 0.5 * dt * k2_omega
k3_omega = f(theta + 0.5 * dt * k2_theta, omega + 0.5 * dt * k2_omega)
k4_theta = omega + dt * k3_omega
k4_omega = f(theta + dt * k3_theta, omega + dt * k3_omega)
theta_new = theta + (dt/6) * (k1_theta + 2*k2_theta + 2*k3_theta + k4_theta)
omega_new = omega + (dt/6) * (k1_omega + 2*k2_omega + 2*k3_omega + k4_omega)
end sub
dim as double theta = 1 ' initial angle in radians
dim as double omega = 0 ' initial angular velocity
dim as double dt = 0.08 ' time step
dim as double theta_new, omega_new
do
screenlock
cls
circle(400 + l*sin(theta), 300 + l*cos(theta)), 20, rgb(255,255,0),,,,F ' draw pendulum bob
line(400, 300) - (400 + l*sin(theta), 300 + l*cos(theta)),rgb(255,255,0) ' draw pendulum rod
RungeKutta(theta, omega, dt, theta_new, omega_new)
theta = theta_new
omega = omega_new
screenunlock
sleep 10
loop until len(inkey)
```