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.

Monday 17 January 2011

Data analysis in MATLAB

I thought I might share some slides on using MATLAB to handle large volumes of data. While this blog is devoted primarily to Mathematica, there are some tasks for which MATLAB is better suited, particularly when it comes to matrix manipulation. One rather exciting possibility covered in the attached presentation is the use of logical vectors, which allow for transforming data as if by the touch of a magic wand.

Sunday 9 January 2011

How to parametrize your graphs and wake up the audience

Today I'm going to visualize the first stage of the Hotelling model. Based on results derived in the previous post, we may define the equilibrium price of firm i (given rival's location xj) as:

My Plot will be built around two curves, each representing the total cost of purchasing the product from one of the two shops (located at x1 and x2), with the cost function ti defined as before:

I'll start by setting a few basic options of the plot: its range, the origin of the axes and the thickness of the curves.


I can now set the locations to anything I want and adjust the prices to the corresponding equilibrium values:


resulting in the following graph:

It is useful to obtain the location at which the two curves intersect (call it xTilde), and to make the axes display it, together with the firms' locations and the corresponding equilibrium prices:




giving the following result:


I will now add a few 'extras' to the graph, starting with two slightly transparent Rectangles with dashed edges: a blue one, corresponding to the profit of the firm located on the left, as well as a pink one, reflecting the profit of the other firm.


The following will add the extras to the plot and display it:



Finally, we might Append a few dotted lines to our collection of extras for the purpose of clarity:


Which brings us to why it's actually worth doing all this (rather than using, say, Adobe Illustrator). The reason is that any graphs created in this way are parametrized, which means we can adjust the values of any variables and see what happens straight away, rather than having to draw the whole thing all over again. Further still, we can create interactive dynamic visualizations, illustrating the comparative statics of our model.

The following code will allow the user to 'drag and drop' the locations of the two firms and observe the outcome.



To understand the syntax, specifically, using the Locator option to allow the 'drag and drop', check out the instructions for Manipulate under 'more information'.

The end product can be recorded as a stand-alone video, such as the one below.


Observe why it is always optimal for the firms' to locate at the opposite ends of the market: moving towards the competitor hardly increases one's share of the market, as it prompts the rival to respond with more aggressive pricing, pushing the profits of both firms down. Easy.