June 01, 2016

Basins of Attraction for Newton's Method

Basins of Attraction - cool sounding term I've heard before but never really knew what it meant.  I think the pendulum-magnet example is the clearest way to intuitively understand it.

Pendulum - magnet


Picture three magnets at the same distance from each other, arranged in a triangle.  And there is a magnetic pendulum that swings above them.  It will eventually come to rest above one of the magnets. But it's the starting point from which the pendulum is let go that determines where the pendulum will eventually end up.


Above image is an awesome visualization from this awesome article that shows the basin of attraction for this magnetic pendulum - basically, it shows how the initial starting point affects the end result of the simulation.  Any point in the image gives you two pieces of information: the starting point of the pendulum, and which magnet it ended up on.  Some other simulations go on to alter the initial parameters - increasing/decreasing the damping factor, strength of the magnets, number of magnets all go on to produce different graphs of differing complexity.

Newton's Method

I am not awesome enough to make a simulation of the magnetic pendulum but graphing the basin of attraction for newton's method is a bit simpler.

Newton's method is a numerical method for finding an approximation to the root of a function for things not as simple as (x - 5)(x - 2) = 0.  What I mean by numerical method is that you take a starting guess, run it through a function to get a better guess, and keep applying the same function to each next guess until the answer is good enough.

And by doing this on the complex plane, you can get some cool graphs.  I decided to try this out for z^5 = 1 on shadertoy.  By taking each pixel and assigning it a real value based on its x coordinate and an imaginary value based on its y coordinate and applying Newton's Method to it, the pixel will eventually land on a root and be assigned a color based on the root it ended up on.  Similarly to the magnetic pendulum graph, the starting point of the pixel determines what its color will end up being.

Basin of Attraction for z^5 = 1 using Newton's Method
(link to shadertoy) You can get this graph by taking an initial z value, applying z -> z - (z^5 - 1)/(5*z^4) a bunch of times, and assigning it a color based on its last calculation. (I think the black circles mean that I needed more iterations for the value to converge, I probably need better software if I wanna do that)

Basins of attractions have other real world examples - in ecology, when a population reaches a stable equilibrium, that equilibrium is thought of as an attractor for the ecosystem.  A ball rolling on a bunch of little hills (remember those wire Fischer-Price toys at the dentists office?) will have several equilibrium points and therefore resulting basins of attraction.