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, https://www.rstudio.com/resources/train ... -learning/ in your machine. To obtain R for any Ubuntu distribution, follow these instructions.
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:
- tssfl@tssfl:~$ R
- R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
- Copyright (C) 2013 The R Foundation for Statistical Computing
- Platform: x86_64-pc-linux-gnu (64-bit)
- R is free software and comes with ABSOLUTELY NO WARRANTY.
- You are welcome to redistribute it under certain conditions.
- Type 'license()' or 'licence()' for distribution details.
- Natural language support but running in an English locale
- R is a collaborative project with many contributors.
- Type 'contributors()' for more information and
- 'citation()' on how to cite R or R packages in publications.
- Type 'demo()' for some demos, 'help()' for on-line help, or
- 'help.start()' for an HTML browser interface to help.
- 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
- Example 2
This equation is used to describe population growth in terms of density changes of one species (
Screen shot showing steps that produce Solution 2 for this logistic equation
Solution 2
- Example 3
Below is the screen shot showing steps that produce solution 3
Solution 3
- Example 4
Here,
See the screen shot below for implementation and the corresponding Solution 4
Solution 4