The Physics


The energy of an object rotating in one dimesion is:
Ithetadot^2

Where I is the moment of inertia, θ the angle and a . denotes differentiation with respect to time.
And the energy of an object with magnetic moment in a magnetic field is
mudotB

Where B is the magnitude of the magnetic field, μ the magnetic moment of the object. For a field in a set direction oscillating with time this gives us a Lagrangian:

Lagrangian
Thus we find that the equation of motion for the system is:
thetaddot=muBcos(wt)sin(theta)/I

To this we can add a damping factor proportional to the square of the velocity:
thetaddot-muBcos(wt)sin(theta)/I-gammatheatdotsquared

Here I have decided that since most compasses are suspended in a fluid there should be an appropriate damping factor. This should be approximately porportional to the square of the linear velocity of object moving in the fluid. Obviously this would need to be correctly adjusted for the compass that is being simulated. For most of this assignment the factor of γ will be set to 0 since this is just a little extra that I added for completeness.