In this assignment I will be using the opensourcephysics library to simulate the motion of a compass in a magnetic field. A Java applet is provided to experiment with allowing the user to perform simulations of compasses in various oscillating magnetic fields.
" Consider a compass needle that is free to rotate in a periodically reversing magnetic field which is perpendicular to the axis of the needle. Choose an appropriate numerical method for solving the equation of motion and plot the Poincaré map at time t=2πn/ω . Verify that if the parameter
The simulation
The physics involved
The source code
The compass class
The applet above uses a Verlet algorithm to solve the differential equation of motion. The results are plotted in output windows of Phase Space and Poincare. The Poincare plot is simply a reading of the position of the system in phase space at times 2πn/ω. A typical run is shown in figure 1 below:


Chaotic behaviour is described by the exponential deviation of nearby trajectories. In laymans terms this means that changing a variable a very small amount can cause a big change in the system. In figure 2 below we see a phase space plot in three colour - black, red and blue, for three very close initial points at times at which the phase of the magnetic field was the same. The paths taken through phase space do diverge very quickly and after a small initial stage appear to bear no resemblance to one another:
This is easy to compare to a similar system with a different set of parameters, set so that the λ value was < 1. In figure 3 below we see that nearby points in phase space stay fairly close to one another as time progresses for this setup:
It's all very well for us physicists to look at a phase-space diagram and nod in agreement that indeed we have found a chaotic motion. However since a simulation is not always intended to be viewed by those with the exact same background as ourselves I decided to extend the project somewhat.
Compass Display
The most natural thing to do when simulating a compass is in fact display the compass!
Phase Space Interactivity
The next logical step was to allow the phase-space plot to become interactive. Clicking at any point in phase space will shift the system to that point in phase space. This is particularly useful since we are trying to see how close initial points in phase space diverge over time, and because the control interface for this program involves an unwieldy number of user defined variables. Each time the phase space plot is clicked the color of the line drawn is changed to make it easier to distinguish between altered data sets. If the left button is pressed, the time is reset, so we can easily assess the effect of altering the initial conditions slightly about a point - just click around the initial point in phase space.
As described in the page on the physics I have extended the equation of motion to include a damping effect. This is based on the idea that the compass is in fact suspended in a liquid and not entirely frictionless. The damping parameter, γ can be set from the program control and will typically be a number less than 1. This can be altered to give an anti-damping effect which although unphysical can lead to some interesting simulations.
There are a number of interesting intial sets of data to try for various results:
| Example Name | Damping | Frequency | B Field | μ | I |
| Simple Harmonic Motion | 0 | 0 | Any | Any | Any |
| High Frequency Field | 0 | 50 | 1 | 1 | 1 |
| Damped Harmonic Motion | 1 | 0 | 5 | 5 | 1 |
| Unstable Confinement | 0 | 12 | 15 | 1 | 1 |
| Two Stable Points | 2 | 10 | 25 | 1 | 1 |
| Anti-Damping! | -0.1 | 8 | 11 | 1 | 1 |
Figure 6: The Damped Harmonic Motion, Unstable Confinement and 2 Stable Points examples with a variety of initial velocities and angles.
We have clearly observed, in a number of cases, chaotic motion dependent on the paramter λ. Since all our calculations involve Bμ/I we could simply have left this as one parameter - in fact I simply leave I and μ as 1 for most of my simulations just altering B. However, all are included as alterable parameters in the simulation for a sense of completeness.
The simulation is successful, and does exactly what it has set out to do - show the chaotic motion of a compass in an oscillating magnetic field. To this I added a few features to make the chaotic nature of the motion more readily apparent.