Most differential equations that model real-life problems are complicated in nature, and their solutions cannot be obtained analytically. Thus, it is often a practice to approximate solutions to these problems numerically.
R can be used to solve initial-value problems (IVPs) of ordinary differential equations (ODEs), partial differential equations (PDEs), differential algebraic equations (DAEs) and delay differential equations (DeDEs). We will go about and see how we can use R as a problem solving environment for DEs.
Modelling with R
First, you need to install R (see also https://www.rstudio.com/, https://www.rstudio.com/resources/training/online-learning/) in your machine. To obtain R for any ubuntu distribution, follow these instructions: http://forums.tssfl.com/viewtopic.php?f=33&t=188.
Here we show how to use the R package "deSolve" (there are other R packages for solving DEs), the successor of package "odesolve" to solve IVPs numerically.
- Example 1
Open the command line and invoke R by typing R, you will see something similar to this one below in the console:
Steps to solve this simple ODE and solution (Solution 1) are found below. lwd = 2 means plot the function with the solid line twice as thick as the default, # marks the beginning of a comment, anything after it is ignored by R.
Screen shot for Solution 1
- Example 2
Next we show how to solve the logistic equation .
This equation is used to describe population growth in terms of density changes of one species () in an environment due to competition for available resources. is carrying capacity, is a growth rate.
Screen shot showing steps that produce Solution 2 for this logistic equation
- Example 3
The third problem under consideration is the SIR Model for spread of disease (see http://www.maa.org/publications/periodicals/loci/joma/the-sir-model-for-spread-of-disease-the-differential-equation-model for details). This is a multiple variable system of first-order ODEs given by
Below is the screen shot showing steps that produce solution 3
- Example 4
Finally, we consider the famous Lorenz (1963) model, first chaotic dynamic system to be described, assumed to represent the idealized behaviour of the Earth's atmosphere. The model consists of a system of three ODEs and characterize the dynamics of three state variables and . The model system of equations is
Here, , , respectively, refer to the horizontal and vertical temperature distribution and represents convective flow.
are three parametric variables with values of and .
See the screen shot below for implementation and the corresponding Solution 4