Thursday 20 January 2011

A dynamic phase portrait of a 2x2 differential equation system

And now for something which proved to be a real eye opener for my Mathematical Economics students, generating plenty of Aaah! and a bit of Aha! as well (the thing I love most about teaching). 

We will create a phase portrait with an additional twist in the form of being able to track the evolution of the process in time, starting from a point arbitrarily chosen by a click of the mouse. The result is going to look like this :



The first step is to specify the system of differential equations which we want to visualize (the code below should work for any 2x2 1st order system, possibly non-linear).

Obtaining a static phase portrait is a simple application of the StreamPlot command:



The following solution returns the specific trajectory of the system (as a function of t) which attains a given point at a given time. This is obtained using NDSolve, which solves the system numerically, allowing for more generally specified equations. Note that the expression below sets up a function which returns another function.





We will control the flow of time using an Animator, including buttons to pause/start the time and change its speed:

Finally, we set things in motion using a DynamicModule, including a LocatorPane with the above portrait as its background.

Every time the user clicks, the cursor position is saved as point and the module uses solution to obtain the trajectory which reaches point at the current time, storing it as Fxy.  As the Animator updates the time variable, the DynamicModule displays a Point at location Fxy[time], i.e. based on the current trajectory.

With this, the demonstration is complete and one can simply change the values assigned to fx and fy to visualize different systems from the one in the above video.