Saturday, December 6, 2008

numerical pendulum

There was this "flash of idea" that I could solve "any" differential equations numerically, which implies I could "simulate" Maxwell's differential equations using a computer program. Sadly, that was too tall an order, so I could only manage to simulate the most basic of differential equations.

So far I managed to derive a simple method to simulate pendulums directly using the rigid pendulum equation (c is a negative constant)m
\frac{\mathrm{d}^2\theta}{\mathrm{d}t^2} = c \sin \theta
I do know Euler's method, which is really simple. But how do I apply it to second derivatives? That's the trick here.

I found that if I broke this equation into two by defining a variable ω,
\begin{align*} \omega &= \frac{\mathrm{d}\theta}{\mathrm{d}t} \\ \frac{\mathrm{d}\omega}{\mathrm{d}t} &= c \sin \theta \end{align*}
I can easily apply Euler's method using the finite differences approximation,
\begin{align*} \frac{\Delta \theta}{\Delta t} &= \omega \\ \frac{\Delta \omega}{\Delta t} &= c \sin \theta \end{align*}
Therefore, I can convert this into a form useful for programming,
\begin{align*} \Delta \omega_i &= c \sin \theta_i \Delta t \\ \Delta \theta_i &= \omega_i \Delta t \\ \omega_{i + 1} &= \omega_i + \Delta \omega_i \\ \theta_{i + 1} &= \theta_i + \Delta \theta_i \end{align*}
This is easily done by iteration; simply keep track of the index i and the values of ω and θ. By choosing the Δt to be small, one can approximate the pendulum with some precision. I have tested the method and it seems to be quite stable, though I'm not sure how good the precision.

0 comments:

Post a Comment