Differential equation

Differential equations describe exchanges of matter, energy, information or any other quantities, often as they vary in time and/or space. Their thorough analytical treatment forms the basis of fundamental theories in mathematics and physics, and they are increasingly applied in chemistry, life sciences and economics. Actually, it enlightens the basic inherent mechanism of any biological or any physical system. Basically it is a backbone of any system through which we can easily understand the underlying dynamics of that system.  

Differential equations are solved by integration, but unfortunately, for many practical applications in science and engineering, systems of differential equations cannot be integrated to give an analytical solution, but rather need to be solved numerically. 

Depending on the problem, mathematical formalization may consist of ordinary differential equations (ODE), partial differential equations (PDE), differential algebraic equations (DAE), or delay differential equations (DDE). In addition, a distinction is made between initial value problems (IVP) and boundary value problems (BVP). All of these differential equation described above represents the deterministic counterpart of any system i.e. there does not exist any sorts of environmental influence i.e. these kind of system exhibits their deterministic counterpart. Now, when natural phenomena affects any system then that system  becomes random and in case of mathematical language, it is known Stochastic i.e. we can predict that system with a certain probability. This kind of situation can be represented through Stochastic Differential equation (SDE). 

In our regular graduation level course we studied a lots of method in order to solve the differential equation but as i mentioned above if we can't express the underlying mechanism of any system through our known methods then it can be solved with some numerical procedure like Runga-Kutta 4th order method (RK4), Euler method, Picard method, Adams- Baseforth method, Adams-Moulton method, etc. These methods are famous due to their order of convergence i.e. how quick they approach towards their solution by minimizing the error. 

All of the aforementioned methods are available in the R software. Moreover for each class of differential equation, there exist several packages. I have listed all of the packages in the following:

Beside all of these methods there exist another method, the discretisation method i.e. if we consider the unit step length difference between the independent variable. The following codes illustrates how to solve differential equations in R software. 

First, i will describe the easiest method i.e. the discrete approximation method 

### Let us first consider the following 1st order differential equation

### dx/dt = rx(1-x/K)

## We can easily solve this problem with the help of integration method

### But we will try to solve numerically 


## First, Create a function demonstrating the discrete approximation method

## Basically, we are using the iteration scheme here

diff.eqn = function(x0 = 0.2, K = 100, r = 0.3, t = 200)  

            {

              x = c(x0, numeric(t))

               for(i in 1:t)

                 {

                   x[i+1] = x[i] + r*x[i]*(1-x[i]/K)

                 }

              return(x)

             }

solution = diff.eqn(); solution ### Numerical solution of the method


plot(time(solution), solution, type = "l", xlab = expression(bold(time)), ylab =    expression(bold(x(t))) #### Graphical representation of the solution



#### Now, we use any standard numerical procedure as mentioned above 


diff.eqn.continuous.set.up = function(t, state, param){

  with(as.list(c(state,param)),{

    dX = r*X*(1-X/K);

    return(list(c(dX)));

  })

}

state = c(X = 0.2)

parameters = c(r=0.3, K = 17.3)

library(deSolve)

times = seq(0, 30, by = 0.01)

out = ode(y = state, times = times, func = diff.eqn.continuous.set.up, parms = parameters)

head(out)

plot(out, ylim=c(0, 100), main = "Solution of the differential equation", method = "rk4", xlab = "time", ylab = "X(t)", col = 2, lwd = 2)


##### Solving simultaneous system of equation

  


The following codes give a demonstration on the solving procedure of the Partial differential equation (PDE)

##### Solving PDE

### Here first we consider the wave equation i.e. the hyperbolic equation

### The numerical procedure basically illustrate the finite difference technique


library(ReacTran)

dx <- 0.2

xgrid <- setup.grid.1D(-100, 100, dx.1 = dx); xgrid

x <- xgrid$x.mid

N <- xgrid$N


uini <- exp(-0.05 * x^2)

vini <- rep(0, N)

yini <- c(uini, vini)

times <- seq (from = 0, to = 50, by = 1)

wave <- function (t, y, parms) {

  u1 <- y[1:N]

  u2 <- y[-(1:N)]

  du1 <- u2

  du2 <- tran.1D(C = u1, C.up = 0, C.down = 0, D = 1, dx = xgrid)$dC

  return(list(c(du1, du2)))

}

out <- ode.1D(func = wave, y = yini, times = times, parms = NULL,

              nspec = 2, method = "ode45", dimens = N, names = c("u", "v"))

u <- out[ ,2:(N+1)]

plot(x, u[1,], xlab = "x", ylab = "u", type = "l", lwd = 2,

     ylim = range(u), xlim = range(-50, 50))

iout <- seq(11, length(times), by=10)

for(i in iout)

  lines(x, u[i,], lwd = 2, col = "darkgrey", lty = i/10)

legend("topright", lty = 1:5, lwd = 2, col = "darkgrey",

       legend=paste("t = ", times[iout]))

par(mar=c(0,0,0,0))

image(out, which = "u", method = "persp", main = "",

      border = NA, col = "lightblue", box = FALSE, shade = 0.5, theta = 0,

      phi = 60)


GPDD handling with R software:

Global Population Dynamics Database, popularly known as GPDD is most probably the largest collection of animal and plant population data throughout the world, within nearly 5,000 time series. . You can easily download this datasets through the link http://www3.imperial.ac.uk/cpb/research/patternsandprocesses/gpdd. Several authors (Sibly et al. (2005, 2007), Bhowmick et al. (2015), Saha et al. (2013) etc.) uses this huge data sets to pursue their research work.  GPDD not only contains just the time series data, it also contains the location from where the data of the species is collected. Now-a-days, it becomes very much problematic to access their website. In order to overcome this situation, we can use R software to access the GPDD data without downloading the data sets. The name of the package which would help in this case is "remotes" . I am very much thankful to Dipali Mestry for helping me in this case. 

The following R code will give a brief demonstration about the usage of this package to handle GPDD. 

  

######## Package for GPDD #######


##### The following code helps you to extract some species name using the package #######


library(remotes)

remotes::install_github("ropensci/rgpdd")

library(rgpdd)


main_id = c(57, 59, 1401, 2781, 2857, 2952, 3132, 3157, 3282, 3543, 3790, 3833, 6537, 6617, 6633)

length(main_id)


temp1 = match(main_id, rgpdd::gpdd_main$MainID)

temp1


taxon_id = rgpdd::gpdd_main[temp1,]$TaxonID

taxon_id


temp2 = match(taxon_id, rgpdd::gpdd_taxon$TaxonID)

temp2


final_table = data.frame(main_id, rgpdd::gpdd_taxon[temp2,]$TaxonID, rgpdd::gpdd_taxon[temp2,]$TaxonName, rgpdd::gpdd_taxon[temp2,]$CommonName)

View(final_table)


##### Just type the following thing and see what options are coming out


rgpdd::