# 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.

# Some test cases

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

Now, a few examples are easy to construct.

Readers should try increasing $$a$$ a bit at a time, e.g. the example below has a distinctly non-sinusoidal character.
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.