# DE solution in R (nonlinear oscillator)

June 15, 2014
By

(This article was first published on Dan Kelley Blog/R, and kindly contributed to R-bloggers)

# Introduction

The function lsoda() from the deSolve package is a handy function for
solving differential equations in R. This is illustrated here with a classic
example: the nonlinear oscillator.

# Theory

As explained in any introductory Physics textbook, the nondimensionalized
oscillator equation $d\theta/dt + \sin\theta = 0$ can be simplified to
$d\theta/dt + \theta = 0$ provided that $\theta \ll 1$, i.e. in the “small
angle” limit.

The linear form has solution $\theta = a \sin(t)$ for initial conditions
$\theta=0$ and $d\theta/dt=a$ at $t=0$.

The nonlinear form is harder to solve analytically, but numerical integration
can be applied to any situation of interest. This is made simpler in the
present statement of the problem in nondimensional form, since there is but a
single parameter, $a$, that describes the system.

A few questions may come to mind before proceeding further. For example, it is
clear that if $a\ll 1$, the numerical solution should match the solution to
the linear equation. But just how small must $a$ be, for a given precision?
A second question is about the qualitative form of the numerical solution, e.g
is it still a sinusoid but a different frequency, or something of a different
character? Might there different classes of solutions in various ranges of
$a$?

Showing that such questions are easily answered with R is the point of the
present exercise.

# Framing the DE for solution in R

As an exercise, lsoda() from the deSolve package can be used to
integrate the nonlinear DE without the small angle assumption.

The first step is to break the second-order DE down into two first-order DEs,
$\phi = d\theta/dt$ and $d\phi/dt = -\sin\theta$, which are to be defined
with a function named func that is used by the DE solver named lsoda.
Of course, we also need initial conditions and a set of times at which to
report the solution. All of these things are set out in the R code given
below, which plots the solution for $a=0.1$ as the red dashed line, on top of
the theoretical solution as the blue line. Readers might wish to try this with
slightly larger and smaller values of $a$, to get a feel for the solution.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 library(deSolve) de <- function(t, y, parms, ...) { theta <- y[1] phi <- y[2] list(c(dthetadt = phi, dphidt = -sin(theta))) } a <- 0.1 y0 <- c(0, a) t <- seq(0, 4 * pi, pi/100) sol <- lsoda(y = y0, times = t, func = de) ylim <- max(range(sol[, 2])) * c(-1, 1) par(mar = c(3.5, 3.5, 1, 1), mgp = c(2, 0.7, 0)) plot(sol[, 1], sol[, 2], type = "l", ylim = ylim, col = "blue", xlab = expression(t), ylab = expression(theta(t))) grid() lines(t, a * sin(t), col = "red", lty = "dashed") 

# Some test cases

For more exploration, it is convenient to define the above as a stand-alone
function that takes a as a parameter.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 library(deSolve) oscillator <- function(a = 0.1) { de <- function(t, y, parms, ...) { theta <- y[1] phi <- y[2] list(c(dthetadt = phi, dphidt = -sin(theta))) } y0 <- c(0, a) t <- seq(0, 8 * pi, pi/100) sol <- lsoda(y = y0, times = t, func = de) ylim <- max(range(sol[, 2])) * c(-1, 1) par(mar = c(3.5, 3.5, 1, 1), mgp = c(2, 0.7, 0)) plot(sol[, 1], sol[, 2], type = "l", ylim = ylim, col = "blue", xlab = expression(t), ylab = expression(theta(t))) grid() lines(t, a * sin(t), col = "red") legend("bottomleft", col = c("blue", "red"), lwd = 1, legend = c("linear", "nonlinear"), bg = "white") } 

Now, a few examples are easy to construct.

 1 oscillator(1) 

Readers should try increasing $a$ a bit at a time, e.g. the example below has
a distinctly non-sinusoidal character.

 1 oscillator(1.999) 

# Exercises

Further explore the behaviour in the neighborhood of $a=2$. Are changes
subtle or dramatic in that region? Are there other regions of interest?
Consult the literature if this problem interests you.

# Resources

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...
{"found_math": true, "script_math_tex": 17, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}