Phaser: an R Package for Phase Plane Analysis of Autonomous Ode Systems

When modelling physical systems, analysts will frequently be confronted by differential equations which cannot be solved analytically. In this instance, numerical integration will usually be the only way forward. However, for autonomous systems of ordinary differential equations (ODEs) in one or two dimensions, it is possible to employ an instructive qualitative analysis foregoing this requirement, using so-called phase plane methods. Moreover, this qualitative analysis can even prove to be highly useful for systems that can be solved analytically, or will be solved numerically anyway. The package phaseR allows the user to perform such phase plane analyses: determining the stability of any equilibrium points easily, and producing informative plots.


Introduction
Repeatedly, when a system of differential equations is written down, it cannot be solved analytically.This is particularly true in the non-linear case, which unfortunately habitually arises when modelling physical systems.As such, it is common that numerical integration is the only way for a modeller to analyse the properties of their system.Consequently, many software packages exist today to assist in this step.In R, for example, the package deSolve (Soetaert et al., 2010) deals with many classes of differential equation.It allows users to solve first-order stiff and non-stiff initial value problem ODEs, as well as stiff and non-stiff delay differential equations (DDEs), and differential algebraic equations (DAEs) up to index 3.Moreover, it can tackle partial differential equations (PDEs) with the assistance ReacTran (Soetaert and Meysman, 2012).For such PDE problems users also have available the package rootSolve (Soetaert et al., 2009), which is additionally particularly suited to analysing the equilibrium of differential equations.Finally, bvpSolve (Soetaert et al., 2013) can tackle boundary value problems of systems of ODEs, whilst sde (Iacus, 2009) is available for stochastic differential equations (SDEs).However, for autonomous ODE systems in either one or two dimensions, phase plane methods, as developed by mathematicians such as Henri Poincaré in the 19th Century (Farlow, 2006), allow for a qualitative analysis of the system's properties without the need for numerical integration.Specifically, it is usually possible to determine the long-term behaviour of the system for any initial condition, via a highly graphical procedure coupled with algorithmic mathematical analysis.
As a result of these considerations, phase plane analysis is an important device in any modeller's toolbox.It stands as a key technique not only for mathematicians, but biologists, physicists and engineers amongst others.See for example Jordan and Smith (2007), Barnes and Fulford (2009), Choudhury (2005), and Mooney (1999) for descriptions of uses in various fields.Since the method is extremely algorithmic in nature, software tools would clearly provide a powerful system by which to execute the techniques.Despite this, few programmes are available for implementing phase plane methods.Presently, a programme entitled pplane that employs a simple GUI system is available for Matlab (MATLAB, 2014;Polking, 2009).In addition, several java applets are available across the internet for executing phase plane methods.But, they vary widely in quality and often struggle when confronted with complex systems, whilst they also provide no simple way to export produced plots.Now, with R ever growing in popularity as an open-source general purpose scientific computing lenguage, it is logical that available code to implement phase plane methods would serve as a valuable resource.The first attempts in this direction were implemented by Kaplan and Flath (2004) at Macalester College who created several short programmes to analyse two dimensional ODEs and provided several example systems.phaseR extends their initial ideas in order to create a complete package; one providing phase plane novices with the opportunity for independent learning, instructors with a useful package for practical sessions, and experienced modellers with an easy means by which to produce quality plots.
Overall, phaseR maintains the original structure of Kaplan and Flath's code; with key functions available for each of the steps of a full phase plane analysis.Specifically, it can assist in identifying and classifying equilibrium points, allows direction fields as well as nullclines to be plotted, and allows trajectories to be added to plots for user specified initial conditions.phaseR, however, extends the functionality of the code to allow analysis of both one and two dimensional systems, whilst also allowing for far greater control over the final produced plot.Moreover, phaseR comes complete with an extensive guide detailing the techniques of phase plane analysis, also containing numerous examples and exercises.Finally, phaseR makes use of available R packages for finding numerical solutions to ODEs in order to ensure maximum stability of the required integration step.I will now proceed by briefly reviewing the techniques of phase plane analysis, before discussing the usage of the

One dimensional autonomous ODE systems
Any one dimensional autonomous ODE, of a function y = y(t) = y t , can be written in the following form dy t dt = f (y t ) .
Thus, autonomous systems are characterised by not explicitly depending on the independent variable t.For such systems, phase plane analysis begins by plotting at a range of values for both the dependent and the independent variable, a small arrow indicating the rate of change of y t as provided by the ODE.This plot, commonly referred to as the direction field, is useful because solutions to the ODE must pass through the arrows in a tangential manner.Therefore, once produced, it provides an easy construction from which to draw trajectories for any initial condition without the need to solve the ODE.
Following this, so-called equilibrium points are determined.Defined as the points y * where f (y * ) = 0, the reason for their name is easy to see: beginning at such a point, because of the lack of explicit dependence upon t in f , means the solution will remain there for all values of t.Moreover, these points are then classified as either stable or unstable, depending upon whether solutions converge towards, or diverge away, from them.Stability may here be determined from a 'phase portrait plot' of f (y t ) against y t ; on which arrows are placed indicating the direction of change of y t with t.Arrows pointing towards each other on either side of an equilibrium point denote stability, whilst arrows pointing away from each other indicate the presence of an unstable point.Alternatively, a Taylor Series argument can be utilised to define a simple criterion.The argument proceeds by supposing the system begins a small distance δ 0 away from the fixed point y * , i. e. y 0 = y * + δ 0 .Then, writing our general expression for y t as y t = y * + δ t , we use the Taylor Series expansion of f to form a differential equation for how δ t changes with t where we have assumed higher order terms are negligible.Recalling f (y * ) = 0, our ODE becomes This autonomous ODE for δ t can be solved easily to give δ t = δ 0 e kt .This analysis is useful to us since stability can be determined based upon whether δ t grows, or decays, as t increases, i. e. using the simple criterion

Two dimensional autonomous ODE systems
In the two dimensional case, for functions x = x(t) = x t and y = y(t) = y t , any autonomous ODE system can be written as Thus as before, these systems are characterised by a lack of explicit dependence upon the independent variable t in the functional forms of f and g.
The direction field, more commonly referred to here as the velocity field, is again produced to provide a powerful way by which to plot trajectories for any initial condition.However, here this is done so in the x t -y t plane, rather than that containing the independent variable.This visualisation proves to be the best way by which to examine an autonomous two dimensional system.
Commonly, to assist the plotting of the velocity field, nullclines are first computed.These are defined as the location where f (x t , y t ) = 0 or g (x t , y t ) = 0. Thus they define the curves across which either x t or y t does not change in t.Any velocity arrows produced will therefore either be perfectly horizontal or vertical.Since a velocity field must vary continuously across a particular nullcline, by the uniqueness of solutions to ODEs, nullclines can be used to check that no arrows have been plotted The R Journal Vol.6/2, December 2014 ISSN 2073-4859 incorrectly.
As one would expect, equilibrium points retain their great importance in two dimensions.Here, their definition generalises as the locations (x * , y * ) where f (x * , y * ) = g (x * , y * ) = 0. Consequently, the intersection of the x t and y t nullclines can be used to determine their location.Stability here is defined in an analogous manner to the one dimensional case, whilst a Taylor Series argument again allows the formulation of a simple rule for determining said stability.As before, it supposes that we have an equilibrium point, now given by (x * , y * ), and that the system initially lies slightly away from this point at (x * + δ 0 , y * + 0 ), and in general we have (x t , y t ) = (x * + δ t , y * + t ).Then using the Taylor expansion for f , the differential equation for x t becomes Similarly, the differential equation for y t becomes In both equations we have again assumed terms of second order and higher are negligible.Now if we write this system in matrix form we acquire Here J is called the Jacobian of the system, and δ = (δ, ) .It is consideration of the characteristic equation of J, and the values of its eigenvalues it provides, that allows equilibrium points to be classified, not only as either stable or unstable, but also in to sub-categories.Full details of this can be found in the guide that accompanies phaseR, available in the documentation folder of its installation.However, classification ultimately, and simply, depends upon the values of the determinant and trace of J.
Thus, in both the one and two dimensional cases, phase plane methods allow trajectories to be easily plotted, and the long-term behaviour of a system identified by classifying the equilibria.We shall next see how phaseR can be used to perform such analyses using several simple commands.

Key functions
phaseR employs six key functions for the employment of phase plane analysis.
• flowField: Plots the direction or velocity field of one or two dimensional autonomous ODE systems.
• nullclines: Plots the nullclines of two dimensional autonomous ODE systems.Alternatively, it can be used to plot horizontal lines at equilibrium points for one dimensional autonomous ODE systems.
• numericalSolution: For two dimensional systems, this function numerically solves the ODEs for a given initial condition via deSolve, and then plots the dependent variables against the independent variable.Thus, it behaves as a wrapper for the user to deSolve, allowing for easier implementation and subsequent plotting.
• phasePortrait: For one dimensional autonomous ODEs, it plots the phase portrait i. e. the derivative against the dependent variable.In addition, it adds arrows to the dependent variable axis from which the stability of any equilibrium points can be determined.
• stability: Classifies equilibrium points, using the criteria that result from the aforementioned Taylor Series arguments.
• trajectory: Plots trajectories in the x t -y t plane by performing numerical integration of the chosen ODE system, again via deSolve.Initial conditions can be specified as an argument, or by clicking with the cursor on locations in a pre-existing plot.
The R Journal Vol.6/2, December 2014 ISSN 2073-4859 As an example, standard phase plane analyses for a two dimensional ODE system would proceed by using flowField, nullclines and then trajectory to create a summarising plot, and finally stability to classify the equilibria.

Example 1: logistic growth model
To describe how phaseR can be used to analyse a one dimensional autonomous system of interest, we will consider the logistic growth model.Originally proposed by Verhulst (1838), this ODE is frequently used in Biology to model the growth of a population under density dependence.It is given in its most general case by dy t dt = βy t 1 − y t K .
To analyse any system using phaseR, a function must first be created to return the value of the derivative at any point (t, y), in a style compatible with deSolve.For this model such a function is already present in the package as logistic, which comprises the following code

dy) }
The general format of this derivative function should be a common one: given t, y and a vector parameters, it should simply return a list with first element the value of the derivative.
With this, we can then proceed with our desired analysis.From here we shall consider the particular case beta = 1 and K = 2.The following code creates Figure 1; adding the direction field, several trajectories and horizontal lines at any equilibrium points.

Example 2: Lotka-Volterra model
As an example of the capabilities of phaseR for a two dimensional system, we will study a particular case of the Lotka-Volterra model (Lotka, 1925;Volterra, 1926) for interacting predator and prey.This model can be written in its general form as Again, we must first specify a derivative function.As before, this is already provided in the package as lotkaVolterra, which consists of the following code deSolve again requires us here to create a function taking values for t, y and parameters, but here returning a list whose first element is a vector of the two derivatives, and also accepting y as a vector argument.
Requiring that both derivatives are zero identifies the equilibrium points of the model; clearly given here by (0, 0) and (2/3, 2).To classify them we turn to the Jacobian Thus for (0, 0); det(J) = −4, which classifies the point as a saddle.Additionally, for (2/3, 2) we have that det(J) = 4, and tr(J) = 0, which classifies the point as a centre.Readers are again pointed to the package guide for further details of classification using the Jacobian.

Concluding remarks and future developments
Rarely can a system of differential equations be solved in closed form.Consequently, numerical solutions are often required and therefore much theory exists around integration techniques for each class of differential equations.In R many of these methods have been implemented in packages such as deSolve, bvpSolve, sde, ReacTran and rootSolve.Between them, these packages allow modellers to deal with many classes of ODEs, DDEs, DAEs, SDEs and PDEs.However, for a one or two dimensional system of autonomous ODEs, an alternative approach to direct numerical solution is available.This so-called phase plane analysis allows much important inference to be made about the system using relatively simplistic graphical and mathematical techniques.The algorithmic nature of the phase plane approach lends itself well to software tools, and phaseR provides the first such complete package in R for its implementation.I have demonstrated how phaseR allows users to easily perform this qualitative analysis with little initial system set-up, and few commands for execution, required.Sometimes, phase plane analysis is criticised on two grounds; that it is limited to systems of two or less dimension (Mutambara, 1999), and that it is highly labour intensive.Whilst the former still stands, and thus only problems that can be at least well approximated by a second order system may be analysed using the methods discussed above, the latter issue is at least relieved further by the creation of phaseR.Whilst to provide greater detail on the dimensional limitation future development of the package will seek to incorporate further example systems from nature, including approximation techniques for higher order problems.In addition, package development will see the one dimensional tools extended to non-autonomous ODEs.Therefore, given all these considerations, in conjunction with the extensive accompanying guide describing in increased detail both the above techniques and providing many worked examples and exercises, phaseR should hopefully serve as a useful package for both independent and group led learning, as well as for those simply seeking to produce high-quality figures.

Figure 1 :
Figure 1: The direction field and several trajectories for the logistic growth model with β = 1 and K = 2.It can be seen that two equilibrium points have been located.

Figure 2 :
Figure 2: The phase portrait for the logistic growth model with β = 1 and K = 2.The line y = 0 has been identified as unstable, and the line y = 2 as stable.