﻿
• Trending Topics
If you are browsing Open Discussion Forums as a Guest, please register. Registering takes only a few moments but gives you increased capabilities. It takes less than 2 minutes to create an account and sign in!

## Solving Differential Equations in R

Topic Authored by
Eli
Senior Expert Member
Total Posts: 831
Joined: Thu Dec 25, 2014 3:33 pm
Total Topics: 748
Location: Tanzania
Issued   Likes: 35 times
Gender:
Zodiac:
Contact:

### Solving Differential Equations in R

R is not a prominent tool for solving differential equations when compared to the historically well known proprietary tools, MATLAB, Mathematica, Maple and emerging open source software such as Octave, Sage and Scilab, but it is very powerful. R is rapidly becoming useful for both teaching and research to many scientists apart from mathematicians due to its excellence and strength in statistical computations, visualization and analysis and hence scientists ranging from biologists to chemists, physicists, ecologists and economists will take an advantage over R to solve differential equations (DEs).

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

Solve $\dfrac{dy}{dt} = 1 - y; \ y(0) = 0$.

Open the command line and invoke R by typing R, you will see something similar to this one below in the console:

2. R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
3. Copyright (C) 2013 The R Foundation for Statistical Computing
4. Platform: x86_64-pc-linux-gnu (64-bit)
5.
6. R is free software and comes with ABSOLUTELY NO WARRANTY.
7. You are welcome to redistribute it under certain conditions.
8. Type 'license()' or 'licence()' for distribution details.
9.
10.  Natural language support but running in an English locale
11.
12. R is a collaborative project with many contributors.
14. 'citation()' on how to cite R or R packages in publications.
15.
16. Type 'demo()' for some demos, 'help()' for on-line help, or
17. 'help.start()' for an HTML browser interface to help.
18. Type 'q()' to quit R.

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

Solution 1

solution_1.png
solution_1.png

Example 2

Next we show how to solve the logistic equation $\dfrac{dy}{dt} = ry\left( 1- \frac{y}{K}\right); \ y(0) = 2$.

This equation is used to describe population growth in terms of density changes of one species ($y$) in an environment due to competition for available resources. $K$ is carrying capacity, $r>0$ is a growth rate.

Screen shot showing steps that produce Solution 2 for this logistic equation

Solution 2

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

$\dfrac{dS}{dt} = -aSI, \\ \\ \dfrac{dI}{dt} = aSI - bI; \\ \\ S(0) = 762, \ \ I(0) = 1.$

Below is the screen shot showing steps that produce solution 3

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 $X \ Y$ and $Z$. The model system of equations is

$\dfrac{dX}{dt} = a.X + Y.Z, \\ \\ \dfrac{dY}{dt} = b.\left( Y- Z\right), \\ \\ \dfrac{dZ}{dt} = -XY + c.Y - Z; \\ \\ X(0) = Y(0) = Z(0) = 1.$

Here, $X$, $Y$, respectively, refer to the horizontal and vertical temperature distribution and $Z$ represents convective flow.

$a, \ b, \ c$ are three parametric variables with values of $-\frac{8}{3}, \ -10,$ and $28$.

See the screen shot below for implementation and the corresponding Solution 4

Solution 4

$\Huge \int$ d

Forbidden_Technology
Member
Total Posts: 20
Joined: Sun Apr 26, 2015 6:07 pm
Total Topics: 14
Issued   Likes: 21 times
Gender:
Contact:

### Re: Solving Differential Equations in R

This is awesome work! I wonder why many people waste too much time posting useless stuffs especially on social websites rather than finding a forum like this one and write something sensible!

Here is another great tool for solving (partial) differential equations: FreeFem++, http://en.wikipedia.org/wiki/FreeFem%2B%2B