Abstract
Differential equations constitute one of the most powerful mathematical tools to understand and predict the behavior of dynamical systems in nature, engineering, and society. A dynamical system is some system with some state, usually expressed by a set of variables, that evolves in time. For example, an oscillating pendulum, the spreading of a disease, and the weather are examples of dynamical systems. We can use basic laws of physics, or plain intuition, to express mathematical rules that govern the evolution of the system in time. These rules take the form of differential equations. You are probably well experienced with equations, at least equations like \(ax+b=0\) or \(ax^{2}+bx+c=0\). Such equations are known as algebraic equations, and the unknown is a number. The unknown in a differential equation is a function, and a differential equation will almost always involve this function and one or more derivatives of the function. For example, \(f^{\prime}(x)=f(x)\) is a simple differential equation (asking if there is any function f such that it equals its derivative – you might remember that e ^{x} is a candidate).
The present chapter starts with explaining how easy it is to solve both single (scalar) firstorder ordinary differential equations and systems of firstorder differential equations by the Forward Euler method. We demonstrate all the mathematical and programming details through two specific applications: population growth and spreading of diseases.
Keywords
 Nonlinear Algebraic Equation
 Differential Equation Model
 Differential Equation System
 Discontinuous Coefficient
 Forward Euler Method
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.
Download chapter PDF
Differential equations constitute one of the most powerful mathematical tools to understand and predict the behavior of dynamical systems in nature, engineering, and society. A dynamical system is some system with some state, usually expressed by a set of variables, that evolves in time. For example, an oscillating pendulum, the spreading of a disease, and the weather are examples of dynamical systems. We can use basic laws of physics, or plain intuition, to express mathematical rules that govern the evolution of the system in time. These rules take the form of differential equations. You are probably well experienced with equations, at least equations like \(ax+b=0\) or \(ax^{2}+bx+c=0\). Such equations are known as algebraic equations, and the unknown is a number. The unknown in a differential equation is a function, and a differential equation will almost always involve this function and one or more derivatives of the function. For example, \(f^{\prime}(x)=f(x)\) is a simple differential equation (asking if there is any function f such that it equals its derivative – you might remember that e ^{x} is a candidate).
The present chapter starts with explaining how easy it is to solve both single (scalar) firstorder ordinary differential equations and systems of firstorder differential equations by the Forward Euler method. We demonstrate all the mathematical and programming details through two specific applications: population growth and spreading of diseases.
Then we turn to a physical application: oscillating mechanical systems, which arise in a wide range of engineering situations. The differential equation is now of second order, and the Forward Euler method does not perform well. This observation motivates the need for other solution methods, and we derive the EulerCromer scheme^{Footnote 1}, the 2nd and 4thorder RungeKutta schemes, as well as a finite difference scheme (the latter to handle the secondorder differential equation directly without reformulating it as a firstorder system). The presentation starts with undamped free oscillations and then treats general oscillatory systems with possibly nonlinear damping, nonlinear spring forces, and arbitrary external excitation. Besides developing programs from scratch, we also demonstrate how to access readymade implementations of more advanced differential equation solvers in Python.
As we progress with more advanced methods, we develop more sophisticated and reusable programs, and in particular, we incorporate good testing strategies so that we bring solid evidence to correct computations. Consequently, the beginning with population growth and disease modeling examples has a very gentle learning curve, while that curve gets significantly steeper towards the end of the treatment of differential equations for oscillatory systems.
4.1 Population Growth
Our first taste of differential equations regards modeling the growth of some population, such as a cell culture, an animal population, or a human population. The ideas even extend trivially to growth of money in a bank. Let \(N(t)\) be the number of individuals in the population at time t. How can we predict the evolution of \(N(t)\) in time? Below we shall derive a differential equation whose solution is \(N(t)\). The equation reads
where r is a number. Note that although N is an integer in real life, we model N as a realvalued function. We are forced to do this because the solution of differential equations are (normally continuous) realvalued functions. An integervalued \(N(t)\) in the model would lead to a lot of mathematical difficulties.
With a bit of guessing, you may realize that \(N(t)=Ce^{rt}\), where C is any number. To make this solution unique, we need to fix C, done by prescribing the value of N at some time, usually t = 0. Say \(N(0)\) is given as N _{0}. Then \(N(t)=N_{0}e^{rt}\).
In general, a differential equation model consists of a differential equation, such as (4.1) and an initial condition, such as \(N(0)=N_{0}\). With a known initial condition, the differential equation can be solved for the unknown function and the solution is unique.
It is, of course, very seldom that we can find the solution of a differential equation as easy as in this example. Normally, one has to apply certain mathematical methods, but these can only handle some of the simplest differential equations. However, we can easily deal with almost any differential equation by applying numerical methods and a bit of programming. This is exactly the topic of the present chapter.
4.1.1 Derivation of the Model
It can be instructive to show how an equation like (4.1) arises. Consider some population of (say) an animal species and let \(N(t)\) be the number of individuals in a certain spatial region, e.g. an island. We are not concerned with the spatial distribution of the animals, just the number of them in some spatial area where there is no exchange of individuals with other spatial areas. During a time interval \(\Delta t\), some animals will die and some new will be born. The number of deaths and births are expected to be proportional to N. For example, if there are twice as many individuals, we expect them to get twice as many newborns. In a time interval \(\Delta t\), the net growth of the population will be
where \(\bar{b}N(t)\) is the number of newborns and \(\bar{d}N(t)\) is the number of deaths. If we double \(\Delta t\), we expect the proportionality constants \(\bar{b}\) and \(\bar{d}\) to double too, so it makes sense to think of \(\bar{b}\) and \(\bar{d}\) as proportional to \(\Delta t\) and ‘‘factor out’’ \(\Delta t\). That is, we introduce \(b=\bar{b}/\Delta t\) and \(d=\bar{d}/\Delta t\) to be proportionality constants for newborns and deaths independent of \(\Delta t\). Also, we introduce \(r=bd\), which is the net rate of growth of the population per time unit. Our model then becomes
Equation (4.2) is actually a computational model. Given \(N(t)\), we can advance the population size by
This is called a difference equation. If we know \(N(t)\) for some t, e.g., \(N(0)=N_{0}\), we can compute
where k is some arbitrary integer. A computer program can easily compute \(N((k+1)\Delta t)\) for us with the aid of a little loop.
Warning
Observe that the computational formula cannot be started unless we have an initial condition!
The solution of \(N^{\prime}=rN\) is \(N=Ce^{rt}\) for any constant C, and the initial condition is needed to fix C so the solution becomes unique. However, from a mathematical point of view, knowing \(N(t)\) at any point t is sufficient as initial condition. Numerically, we more literally need an initial condition: we need to know a starting value at the left end of the interval in order to get the computational formula going.
In fact, we do not need a computer since we see a repetitive pattern when doing hand calculations, which leads us to a mathematical formula for \(N((k+1)\Delta t)\), :
Rather than using (4.2) as a computational model directly, there is a strong tradition for deriving a differential equation from this difference equation. The idea is to consider a very small time interval \(\Delta t\) and look at the instantaneous growth as this time interval is shrunk to an infinitesimally small size. In mathematical terms, it means that we let \(\Delta t\rightarrow 0\). As (4.2) stands, letting \(\Delta t\rightarrow 0\) will just produce an equation \(0=0\), so we have to divide by \(\Delta t\) and then take the limit:
The term on the lefthand side is actually the definition of the derivative \(N^{\prime}(t)\), so we have
which is the corresponding differential equation.
There is nothing in our derivation that forces the parameter r to be constant – it can change with time due to, e.g., seasonal changes or more permanent environmental changes.
Detour: Exact mathematical solution
If you have taken a course on mathematical solution methods for differential equations, you may want to recap how an equation like \(N^{\prime}=rN\) or \(N^{\prime}=r(t)N\) is solved. The method of separation of variables is the most convenient solution strategy in this case:
which for constant r results in \(N=N_{0}e^{rt}\). Note that \(\exp{(t)}\) is the same as e ^{t}.
As will be described later, r must in more realistic models depend on N. The method of separation of variables then requires to integrate \(\int_{N_{0}}^{N}N/r(N)dN\), which quickly becomes nontrivial for many choices of \(r(N)\). The only generally applicable solution approach is therefore a numerical method.
4.1.2 Numerical Solution
There is a huge collection of numerical methods for problems like (4.2), and in general any equation of the form \(u^{\prime}=f(u,t)\), where \(u(t)\) is the unknown function in the problem, and f is some known formula of u and optionally t. For example, \(f(u,t)=ru\) in (4.2). We will first present a simple finite difference method solving \(u^{\prime}=f(u,t)\). The idea is fourfold:

1.
Introduce a mesh in time with \(N_{t}+1\) points \(t_{0},t_{1},\ldots,t_{N_{t}}\). We seek the unknown u at the mesh points t _{ n }, and introduce u ^{n} as the numerical approximation to \(u(t_{n})\), see Fig. 4.1.

2.
Assume that the differential equation is valid at the mesh points.

3.
Approximate derivatives by finite differences, see Fig. 4.2.

4.
Formulate a computational algorithm that can compute a new value u ^{n} based on previously computed values u ^{i}, \(i<n\).
An example will illustrate the steps. First, we introduce the mesh, and very often the mesh is uniform, meaning that the spacing between points t _{ n } and \(t_{n+1}\) is constant. This property implies that
Second, the differential equation is supposed to hold at the mesh points. Note that this is an approximation, because the differential equation is originally valid at all real values of t. We can express this property mathematically as
For example, with our model equation \(u^{\prime}=ru\), we have the special case
or
if r depends explicitly on t.
Third, derivatives are to be replaced by finite differences. To this end, we need to know specific formulas for how derivatives can be approximated by finite differences. One simple possibility is to use the definition of the derivative from any calculus book,
At an arbitrary mesh point t _{ n } this definition can be written as
Instead of going to the limit \(\Delta t\rightarrow 0\) we can use a small \(\Delta t\), which yields a computable approximation to \(u^{\prime}(t_{n})\):
This is known as a forward difference since we go forward in time (\(u^{n+1}\)) to collect information in u to estimate the derivative. Figure 4.2 illustrates the idea. The error of the forward difference is proportional to \(\Delta t\) (often written as \(\mathcal{O}(\Delta t)\), but we will not use this notation in the present book).
We can now plug in the forward difference in our differential equation sampled at the arbitrary mesh point t _{ n }:
or with \(f(u,t)=ru\) in our special model problem for population growth,
If r depends on time, we insert \(r(t_{n})=r^{n}\) for r in this latter equation.
The fourth step is to derive a computational algorithm. Looking at (4.3), we realize that if u ^{n} should be known, we can easily solve with respect to \(u^{n+1}\) to get a formula for u at the next time level \(t_{n+1}\):
Provided we have a known starting value, \(u^{0}=U_{0}\), we can use (4.5) to advance the solution by first computing u ^{1} from u ^{0}, then u ^{2} from u ^{1}, u ^{3} from u ^{2}, and so forth.
Such an algorithm is called a numerical scheme for the differential equation and often written compactly as
This scheme is known as the Forward Euler scheme, also called Euler’s method.
In our special population growth model, we have
We may also write this model using the problemspecific symbol N instead of the generic u function:
The observant reader will realize that (4.8) is nothing but the computational model (4.2) arising directly in the model derivation. The formula (4.8) arises, however, from a detour via a differential equation and a numerical method for the differential equation. This looks rather unnecessary! The reason why we bother to derive the differential equation model and then discretize it by a numerical method is simply that the discretization can be done in many ways, and we can create (much) more accurate and more computationally efficient methods than (4.8) or (4.6). This can be useful in many problems! Nevertheless, the Forward Euler scheme is intuitive and widely applicable, at least when \(\Delta t\) is chosen to be small.
The numerical solution between the mesh points
Our numerical method computes the unknown function u at discrete mesh points \(t_{1},t_{2},\ldots,t_{N_{t}}\). What if we want to evaluate the numerical solution between the mesh points? The most natural choice is to assume a linear variation between the mesh points, see Fig. 4.3. This is compatible with the fact that when we plot the array \(u^{0},u^{1},\ldots\) versus \(t_{0},t_{1},\ldots\), a straight line is drawn between the discrete points.
4.1.3 Programming the Forward Euler Scheme; the Special Case
Let us compute (4.8) in a program. The input variables are N _{0}, \(\Delta t\), r, and N _{ t }. Note that we need to compute \(N_{t}+1\) new values \(N^{1},\ldots,N^{N_{t}+1}\). A total of \(N_{t}+2\) values are needed in an array representation of N ^{n}, \(n=0,\ldots,N_{t}+1\).
Our first version of this program is as simple as possible:
The complete code above resides in the file growth1.py .
Let us demonstrate a simulation where we start with 100 animals, a net growth rate of 10 percent (0.1) per time unit, which can be one month, and \(t\in[0,20]\) months. We may first try \(\Delta t\) of half a month (0.5), which implies \(N_{t}=40\) (or to be absolutely precise, the last time point to be computed according to our setup above is \(t_{N_{t}+1}=20.5\)). Figure 4.4 shows the results. The solid line is the exact solution, while the circles are the computed numerical solution. The discrepancy is clearly visible. What if we make \(\Delta t\) 10 times smaller? The result is displayed in Fig. 4.5, where we now use a solid line also for the numerical solution (otherwise, 400 circles would look very cluttered, so the program has a test on how to display the numerical solution, either as circles or a solid line). We can hardly distinguish the exact and the numerical solution. The computing time is also a fraction of a second on a laptop, so it appears that the Forward Euler method is sufficiently accurate for practical purposes. (This is not always true for large, complicated simulation models in engineering, so more sophisticated methods may be needed.)
It is also of interest to see what happens if we increase \(\Delta t\) to 2 months. The results in Fig. 4.6 indicate that this is an inaccurate computation.
4.1.4 Understanding the Forward Euler Method
The good thing about the Forward Euler method is that it gives an understanding of what a differential equation is and a geometrical picture of how to construct the solution. The first idea is that we have already computed the solution up to some time point t _{ n }. The second idea is that we want to progress the solution from t _{ n } to \(t_{n+1}\) as a straight line.
We know that the line must go through the solution at t _{ n }, i.e., the point \((t_{n},u^{n})\). The differential equation tells us the slope of the line: \(u^{\prime}(t_{n})=f(u^{n},t_{n})=ru^{n}\). That is, the differential equation gives a direct formula for the further direction of the solution curve. We can say that the differential equation expresses how the system (u) undergoes changes at a point.
There is a general formula for a straight line \(y=ax+b\) with slope a that goes through the point \((x_{0},y_{0})\): \(y=a(xx_{0})+y_{0}\). Using this formula adapted to the present case, and evaluating the formula for \(t_{n+1}\), results in
which is nothing but the Forward Euler formula. You are now encouraged to do Exercise 4.1 to become more familiar with the geometric interpretation of the Forward Euler method.
4.1.5 Programming the Forward Euler Scheme; the General Case
Our previous program was just a flat main program tailored to a special differential equation. When programming mathematics, it is always good to consider a (large) class of problems and making a Python function to solve any problem that fits into the class. More specifically, we will make software for the class of differential equation problems of the form
for some given function f, and numbers U _{0} and T. We also take the opportunity to illustrate what is commonly called a demo function. As the name implies, the purpose of such a function is solely to demonstrate how the function works (not to be confused with a test function, which does verification by use of assert). The Python function calculating the solution must take f, U _{0}, \(\Delta t\), and T as input, find the corresponding N _{ t }, compute the solution, and return an array with \(u^{0},u^{1},\ldots,u^{N_{t}}\) and an array with \(t_{0},t_{1},\ldots,t_{N_{t}}\). The Forward Euler scheme reads
The corresponding program may now take the form (file ode_FE.py ):
This program file, called ode_FE.py, is a reusable piece of code with a general ode_FE function that can solve any differential equation \(u^{\prime}=f(u,t)\) and a demo function for the special case \(u^{\prime}=0.1u\), \(u(0)=100\). Observe that the call to the demo function is placed in a test block. This implies that the call is not active if ode_FE is imported as a module in another program, but active if ode_FE.py is run as a program.
The solution should be identical to what the growth1.py program produces with the same parameter settings (r = 0.1, \(N_{0}=100\)). This feature can easily be tested by inserting a print statement, but a much better, automated verification is suggested in Exercise 4.1. You are strongly encouraged to take a ‘‘break’’ and do that exercise now.
Remark on the use of u as variable
In the ode_FE program, the variable u is used in different contexts. Inside the ode_FE function, u is an array, but in the f(u,t) function, as exemplified in the demo_population_growth function, the argument u is a number. Typically, we call f (in ode_FE) with the u argument as one element of the array u in the ode_FE function: u[n].
4.1.6 Making the Population Growth Model More Realistic
Exponential growth of a population according the model \(N^{\prime}=rN\), with exponential solution \(N=N_{0}e^{rt}\), is unrealistic in the long run because the resources needed to feed the population are finite. At some point there will not be enough resources and the growth will decline. A common model taking this effect into account assumes that r depends on the size of the population, N:
The corresponding differential equation becomes
The reader is strongly encouraged to repeat the steps in the derivation of the Forward Euler scheme and establish that we get
which computes as easy as for a constant r, since \(r(N^{n})\) is known when computing \(N^{n+1}\). Alternatively, one can use the Forward Euler formula for the general problem \(u^{\prime}=f(u,t)\) and use \(f(u,t)=r(u)u\) and replace u by N.
The simplest choice of \(r(N)\) is a linear function, starting with some growth value \(\bar{r}\) and declining until the population has reached its maximum, M, according to the available resources:
In the beginning, \(N\ll M\) and we will have exponential growth \(e^{\bar{r}t}\), but as N increases, \(r(N)\) decreases, and when N reaches M, \(r(N)=0\) so there is now more growth and the population remains at \(N(t)=M\). This linear choice of \(r(N)\) gives rise to a model that is called the logistic model. The parameter M is known as the carrying capacity of the population.
Let us run the logistic model with aid of the ode_FE function in the ode_FE module. We choose \(N(0)=100\), \(\Delta t=0.5\) month, T = 60 months, r = 0.1, and M = 500. The complete program, called logistic.py , is basically a call to ode_FE:
Figure 4.7 shows the resulting curve. We see that the population stabilizes around M = 500 individuals. A corresponding exponential growth would reach \(N_{0}e^{rt}=100e^{0.1\cdot 60}\approx 40{,}300\) individuals!
It is always interesting to see what happens with large \(\Delta t\) values. We may set \(\Delta t=20\) and T = 100. Now the solution, seen in Fig. 4.8, oscillates and is hence qualitatively wrong, because one can prove that the exact solution of the differential equation is monotone. (However, there is a corresponding difference equation model, \(N_{n+1}=rN_{n}(1N_{n}/M)\), which allows oscillatory solutions and those are observed in animal populations. The problem with large \(\Delta t\) is that it just leads to wrong mathematics – and two wrongs don’t make a right in terms of a relevant model.)
Remark on the world population
The number of people on the planet ^{Footnote 2} follows the model \(N^{\prime}=r(t)N\), where the net reproduction \(r(t)\) varies with time and has decreased since its top in 1990. The current world value of r is 1.2%, and it is difficult to predict future values ^{Footnote 3}. At the moment, the predictions of the world population point to a growth to 9.6 billion before declining.
This example shows the limitation of a differential equation model: we need to know all input parameters, including \(r(t)\), in order to predict the future. It is seldom the case that we know all input parameters. Sometimes knowledge of the solution from measurements can help estimate missing input parameters.
4.1.7 Verification: Exact Linear Solution of the Discrete Equations
How can we verify that the programming of an ODE model is correct? The best method is to find a problem where there are no unknown numerical approximation errors, because we can then compare the exact solution of the problem with the result produced by our implementation and expect the difference to be within a very small tolerance. We shall base a unit test on this idea and implement a corresponding test function (see Sect. 3.4.4) for automatic verification of our implementation.
It appears that most numerical methods for ODEs will exactly reproduce a solution u that is linear in t. We may therefore set \(u=at+b\) and choose any f whose derivative is a. The choice \(f(u,t)=a\) is very simple, but we may add anything that is zero, e.g.,
This is a valid \(f(u,t)\) for any a, b, and m. The corresponding ODE looks highly nontrivial, however:
Using the general ode_FE function in ode_FE.py , we may write a proper test function as follows (in file test_ode_FE_exact_linear.py ):
Recall that test functions should start with the name test_, have no arguments, and formulate the test as a boolean expression success that is True if the test passes and False if it fails. Test functions should make the test as assert success (here success can also be a boolean expression as in assert diff < tol).
Observe that we cannot compare diff to zero, which is what we mathematically expect, because diff is a floatingpoint variable that most likely contains small rounding errors. Therefore, we must compare diff to zero with a tolerance, here \(10^{15}\).
You are encouraged to do Exercise 4.2 where the goal is to make a test function for a verification based on comparison with handcalculated results for a few time steps.
4.2 Spreading of Diseases
Our aim with this section is to show in detail how one can apply mathematics and programming to investigate spreading of diseases. The mathematical model is now a system of three differential equations with three unknown functions. To derive such a model, we can use mainly intuition, so no specific background knowledge of diseases is required.
4.2.1 Spreading of a Flu
Imagine a boarding school out in the country side. This school is a small and closed society. Suddenly, one or more of the pupils get a flu. We expect that the flu may spread quite effectively or die out. The question is how many of the pupils and the school’s staff will be affected. Some quite simple mathematics can help us to achieve insight into the dynamics of how the disease spreads.
Let the mathematical function \(S(t)\) count how many individuals, at time t, that have the possibility to get infected. Here, t may count hours or days, for instance. These individuals make up a category called susceptibles, labeled as S. Another category, I, consists of the individuals that are infected. Let \(I(t)\) count how many there are in category I at time t. An individual having recovered from the disease is assumed to gain immunity. There is also a small possibility that an infected will die. In either case, the individual is moved from the I category to a category we call the removed category, labeled with R. We let \(R(t)\) count the number of individuals in the R category at time t. Those who enter the R category, cannot leave this category.
To summarize, the spreading of this disease is essentially the dynamics of moving individuals from the S to the I and then to the R category:
We can use mathematics to more precisely describe the exchange between the categories. The fundamental idea is to describe the changes that take place during a small time interval, denoted by \(\Delta t\).
Our disease model is often referred to as a compartment model, where quantities are shuffled between compartments (here a synonym for categories) according to some rules. The rules express changes in a small time interval \(\Delta t\), and from these changes we can let \(\Delta t\) go to zero and obtain derivatives. The resulting equations then go from difference equations (with finite \(\Delta t\)) to differential equations (\(\Delta t\rightarrow 0\)).
We introduce a uniform mesh in time, \(t_{n}=n\Delta t\), \(n=0,\ldots,N_{t}\), and seek S at the mesh points. The numerical approximation to S at time t _{ n } is denoted by S ^{n}. Similarly, we seek the unknown values of \(I(t)\) and \(R(t)\) at the mesh points and introduce a similar notation I ^{n} and R ^{n} for the approximations to the exact values \(I(t_{n})\) and \(R(t_{n})\).
In the time interval \(\Delta t\) we know that some people will be infected, so S will decrease. We shall soon argue by mathematics that there will be \(\beta\Delta tSI\) new infected individuals in this time interval, where β is a parameter reflecting how easy people get infected during a time interval of unit length. If the loss in S is \(\beta\Delta tSI\), we have that the change in S is
Dividing by \(\Delta t\) and letting \(\Delta t\rightarrow 0\), makes the lefthand side approach \(S^{\prime}(t_{n})\) such that we obtain a differential equation
The reasoning in going from the difference equation (4.9) to the differential equation (4.10) follows exactly the steps explained in Sect. 4.1.1.
Before proceeding with how I and R develops in time, let us explain the formula \(\beta\Delta tSI\). We have S susceptibles and I infected people. These can make up SI pairs. Now, suppose that during a time interval T we measure that m actual pairwise meetings do occur among n theoretically possible pairings of people from the S and I categories. The probability that people meet in pairs during a time T is (by the empirical frequency definition of probability) equal to \(m/n\), i.e., the number of successes divided by the number of possible outcomes. From such statistics we normally derive quantities expressed per unit time, i.e., here we want the probability per unit time, μ, which is found from dividing by T: \(\mu=m/(nT)\).
Given the probability μ, the expected number of meetings per time interval of SI possible pairs of people is (from basic statistics) \(\mu SI\). During a time interval \(\Delta t\), there will be \(\mu SI\Delta t\) expected number of meetings between susceptibles and infected people such that the virus may spread. Only a fraction of the \(\mu\Delta tSI\) meetings are effective in the sense that the susceptible actually becomes infected. Counting that m people get infected in n such pairwise meetings (say 5 are infected from 1000 meetings), we can estimate the probability of being infected as \(p=m/n\). The expected number of individuals in the S category that in a time interval \(\Delta t\) catch the virus and get infected is then \(p\mu\Delta tSI\). Introducing a new constant \(\beta=p\mu\) to save some writing, we arrive at the formula \(\beta\Delta tSI\).
The value of β must be known in order to predict the future with the disease model. One possibility is to estimate p and μ from their meanings in the derivation above. Alternatively, we can observe an ‘‘experiment’’ where there are S _{0} susceptibles and I _{0} infected at some point in time. During a time interval T we count that N susceptibles have become infected. Using (4.9) as a rough approximation of how S has developed during time T (and now T is not necessarily small, but we use (4.9) anyway), we get
We need an additional equation to describe the evolution of \(I(t)\). Such an equation is easy to establish by noting that the loss in the S category is a corresponding gain in the I category. More precisely,
However, there is also a loss in the I category because people recover from the disease. Suppose that we can measure that m out of n individuals recover in a time period T (say 10 of 40 sick people recover during a day: m = 10, n = 40, T = 24 h). Now, \(\gamma=m/(nT)\) is the probability that one individual recovers in a unit time interval. Then (on average) \(\gamma\Delta tI\) infected will recover in a time interval \(\Delta t\). This quantity represents a loss in the I category and a gain in the R category. We can therefore write the total change in the I category as
The change in the R category is simple: there is always an increase from the I category:
Since there is no loss in the R category (people are either recovered and immune, or dead), we are done with the modeling of this category. In fact, we do not strictly need the equation (4.14) for R, but extensions of the model later will need an equation for R.
Dividing by \(\Delta t\) in (4.13) and (4.14) and letting \(\Delta t\rightarrow 0\), results in the corresponding differential equations
and
To summarize, we have derived difference equations (4.9)–(4.14), and alternative differential equations (4.15)–(4.16). For reference, we list the complete set of the three difference equations:
Note that we have isolated the new unknown quantities \(S^{n+1}\), \(I^{n+1}\), and \(R^{n+1}\) on the lefthand side, such that these can readily be computed if S ^{n}, I ^{n}, and R ^{n} are known. To get such a procedure started, we need to know S ^{0}, I ^{0}, R ^{0}. Obviously, we also need to have values for the parameters β and γ.
We also list the system of three differential equations:
This differential equation model (and also its discrete counterpart above) is known as an SIR model. The input data to the differential equation model consist of the parameters β and γ as well as the initial conditions \(S(0)=S_{0}\), \(I(0)=I_{0}\), and \(R(0)=R_{0}\).
4.2.2 A Forward Euler Method for the Differential Equation System
Let us apply the same principles as we did in Sect. 4.1.2 to discretize the differential equation system by the Forward Euler method. We already have a time mesh and timediscrete quantities S ^{n}, I ^{n}, R ^{n}, \(n=0,\ldots,N_{t}\). The three differential equations are assumed to be valid at the mesh points. At the point t _{ n } we then have
for \(n=0,1,\ldots,N_{t}\). This is an approximation since the differential equations are originally valid at all times t (usually in some finite interval \([0,T]\)). Using forward finite differences for the derivatives results in an additional approximation,
As we see, these equations are identical to the difference equations that naturally arise in the derivation of the model. However, other numerical methods than the Forward Euler scheme will result in slightly different difference equations.
4.2.3 Programming the Numerical Method; the Special Case
The computation of (4.26)–(4.28) can be readily made in a computer program SIR1.py :
This program was written to investigate the spreading of a flu at the mentioned boarding school, and the reasoning for the specific choices β and γ goes as follows. At some other school where the disease has already spread, it was observed that in the beginning of a day there were 40 susceptibles and 8 infected, while the numbers were 30 and 18, respectively, 24 hours later. Using 1 h as time unit, we then have from (4.11) that \(\beta=10/(40\cdot 8\cdot 24)\). Among 15 infected, it was observed that 3 recovered during a day, giving \(\gamma=3/(15\cdot 24)\). Applying these parameters to a new case where there is one infected initially and 50 susceptibles, gives the graphs in Fig. 4.9. These graphs are just straight lines between the values at times \(t_{i}=i\Delta t\) as computed by the program. We observe that S reduces as I and R grows. After about 30 days everyone has become ill and recovered again.
We can experiment with β and γ to see whether we get an outbreak of the disease or not. Imagine that a ‘‘wash your hands’’ campaign was successful and that the other school in this case experienced a reduction of β by a factor of 5. With this lower β the disease spreads very slowly so we simulate for 60 days. The curves appear in Fig. 4.10.
4.2.4 Outbreak or Not
Looking at the equation for I, it is clear that we must have \(\beta SI\gamma I> 0\) for I to increase. When we start the simulation it means that
or simpler
to increase the number of infected people and accelerate the spreading of the disease. You can run the SIR1.py program with a smaller β such that (4.29 ) is violated and observe that there is no outbreak.
The power of mathematical modeling
The reader should notice our careful use of words in the previous paragraphs. We started out with modeling a very specific case, namely the spreading of a flu among pupils and staff at a boarding school. With purpose we exchanged words like pupils and flu with more neutral and general words like individuals and disease, respectively. Phrased equivalently, we raised the abstraction level by moving from a specific case (flu at a boarding school) to a more general case (disease in a closed society). Very often, when developing mathematical models, we start with a specific example and see, through the modeling, that what is going on of essence in this example also will take place in many similar problem settings. We try to incorporate this generalization in the model so that the model has a much wider application area than what we aimed at in the beginning. This is the very power of mathematical modeling: by solving one specific case we have often developed more generic tools that can readily be applied to solve seemingly different problems. The next sections will give substance to this assertion.
4.2.5 Abstract Problem and Notation
When we had a specific differential equation with one unknown, we quickly turned to an abstract differential equation written in the generic form \(u^{\prime}=f(u,t)\). We refer to such a problem as a scalar ODE. A specific equation corresponds to a specific choice of the formula \(f(u,t)\) involving u and (optionally) t.
It is advantageous to also write a system of differential equations in the same abstract notation,
but this time it is understood that u is a vector of functions and f is also vector. We say that \(u^{\prime}=f(u,t)\) is a vector ODE or system of ODEs in this case. For the SIR model we introduce the two 3vectors, one for the unknowns,
and one for the righthand side functions,
The equation \(u^{\prime}=f(u,t)\) means setting the two vectors equal, i.e., the components must be pairwise equal. Since \(u^{\prime}=(S^{\prime},I^{\prime},R^{\prime})\), we get that \(u^{\prime}=f\) implies
The generalized short notation \(u^{\prime}=f(u,t)\) is very handy since we can derive numerical methods and implement software for this abstract system and in a particular application just identify the formulas in the f vector, implement these, and call functionality that solves the differential equation system.
4.2.6 Programming the Numerical Method; the General Case
In Python code, the Forward Euler step
being a scalar or a vector equation, can be coded as
both in the scalar and vector case. In the vector case, u[n] is a onedimensional numpy array of length \(m+1\) holding the mathematical quantity u ^{n}, and the Python function f must return a numpy array of length \(m+1\). Then the expression u[n] + dt*f(u[n], t[n]) is an array plus a scalar times an array.
For all this to work, the complete numerical solution must be represented by a twodimensional array, created by u = zeros((N_t+1, m+1)). The first index counts the time points and the second the components of the solution vector at one time point. That is, u[n,i] corresponds to the mathematical quantity \(u^{n}_{i}\). When we use only one index, as in u[n], this is the same as u[n,:] and picks out all the components in the solution at the time point with index n. Then the assignment u[n+1] = ... becomes correct because it is actually an inplace assignment u[n+1, :] = .... The nice feature of these facts is that the same piece of Python code works for both a scalar ODE and a system of ODEs!
The ode_FE function for the vector ODE is placed in the file ode_system_FE. py and was written as follows:
The line f_ = lambda ... needs an explanation. For a user, who just needs to define the f in the ODE system, it is convenient to insert the various mathematical expressions on the righthand sides in a list and return that list. Obviously, we could demand the user to convert the list to a numpy array, but it is so easy to do a general such conversion in the ode_FE function as well. To make sure that the result from f is indeed an array that can be used for array computing in the formula u[n] + dt*f(u[n], t[n]), we introduce a new function f_ that calls the user’s f and sends the results through the numpy function asarray, which ensures that its argument is converted to a numpy array (if it is not already an array).
Note also the extra parenthesis required when calling zeros with two indices.
Let us show how the previous SIR model can be solved using the new general ode_FE that can solve any vector ODE. The user’s f(u, t) function takes a vector u, with three components corresponding to S, I, and R as argument, along with the current time point t[n], and must return the values of the formulas of the righthand sides in the vector ODE. An appropriate implementation is
Note that the S, I, and R values correspond to S ^{n}, I ^{n}, and R ^{n}. These values are then just inserted in the various formulas in the vector ODE. Here we collect the values in a list since the ode_FE function will anyway wrap this list in an array. We could, of course, returned an array instead:
The list version looks a bit nicer, so that is why we prefer a list and rather introduce f_ = lambda u, t: asarray(f(u,t)) in the general ode_FE function.
We can now show a function that runs the previous SIR example, while using the generic ode_FE function:
Recall that the u returned from ode_FE contains all components (S, I, R) in the solution vector at all time points. We therefore need to extract the S, I, and R values in separate arrays for further analysis and easy plotting.
Another key feature of this higherquality code is the consistency check. By adding the three differential equations in the SIR model, we realize that \(S^{\prime}+I^{\prime}+R^{\prime}=0\), which means that \(S+I+R=\mbox{const}\). We can check that this relation holds by comparing \(S^{n}+I^{n}+R^{n}\) to the sum of the initial conditions. The check is not a fullfledged verification, but it is a much better than doing nothing and hoping that the computation is correct. Exercise 4.5 suggests another method for controlling the quality of the numerical solution.
4.2.7 TimeRestricted Immunity
Let us now assume that immunity after the disease only lasts for some certain time period. This means that there is transport from the R state to the S state:
Modeling the loss of immunity is very similar to modeling recovery from the disease: the amount of people losing immunity is proportional to the amount of recovered patients and the length of the time interval \(\Delta t\). We can therefore write the loss in the R category as \(\nu\Delta tR\) in time \(\Delta t\), where \(\nu^{1}\) is the typical time it takes to lose immunity. The loss in \(R(t)\) is a gain in \(S(t)\). The ‘‘budgets’’ for the categories therefore become
Dividing by \(\Delta t\) and letting \(\Delta t\rightarrow 0\) gives the differential equation system
This system can be solved by the same methods as we demonstrated for the original SIR model. Only one modification in the program is necessary: adding nu*R[n] to the S[n+1] update and subtracting the same quantity in the R[n+1] update:
The modified code is found in the file SIR2.py.
Setting \(\nu^{1}\) to 50 days, reducing β by a factor of 4 compared to the previous example (\(\beta=0.00033\)), and simulating for 300 days gives an oscillatory behavior in the categories, as depicted in Fig. 4.11. It is easy now to play around and study how the parameters affect the spreading of the disease. For example, making the disease slightly more effective (increase β to 0.00043) and increasing the average time to loss of immunity to 90 days lead to other oscillations, see Fig. 4.12.
4.2.8 Incorporating Vaccination
We can extend the model to also include vaccination. To this end, it can be useful to track those who are vaccinated and those who are not. So, we introduce a fourth category, V, for those who have taken a successful vaccination. Furthermore, we assume that in a time interval \(\Delta t\), a fraction \(p\Delta t\) of the S category is subject to a successful vaccination. This means that in the time \(\Delta t\), \(p\Delta tS\) people leave from the S to the V category. Since the vaccinated ones cannot get the disease, there is no impact on the I or R categories. We can visualize the categories, and the movement between them, as
The new, extended differential equations with the V quantity become
We shall refer to this model as the SIRV model.
The new equation for \(V^{\prime}\) poses no difficulties when it comes to the numerical method. In a Forward Euler scheme we simply add an update
The program needs to store \(V(t)\) in an additional array V, and the plotting command must be extended with more arguments to plot V versus t as well. The complete code is found in the file SIRV1.py.
Using p = 0.0005 and p = 0.0001 as values for the vaccine efficiency parameter, the effect of vaccination is seen in Fig. 4.13 (other parameters are as in Fig. 4.11).
4.2.9 Discontinuous Coefficients: A Vaccination Campaign
What about modeling a vaccination campaign? Imagine that six days after the outbreak of the disease, the local health station launches a vaccination campaign. They reach out to many people, say 10 times as efficiently as in the previous (constant vaccination) case. If the campaign lasts for 10 days we can write
Note that we must multiply the t value by 24 because t is measured in hours, not days. In the differential equation system, \(pS(t)\) must be replaced by \(p(t)S(t)\), and in this case we get a differential equation system with a term that is discontinuous. This is usually quite a challenge in mathematics, but as long as we solve the equations numerically in a program, a discontinuous coefficient is easy to treat.
There are two ways to implement the discontinuous coefficient \(p(t)\): through a function and through an array. The function approach is perhaps the easiest:
In the code for updating the arrays S and V we get a term p(t[n])*S[n].
We can also let \(p(t)\) be an array filled with correct values prior to the simulation. Then we need to allocate an array p of length N_t+1 and find the indices corresponding to the time period between 6 and 15 days. These indices are found from the time point divided by \(\Delta t\). That is,
The \(p(t)S(t)\) term in the updating formulas for S and V simply becomes p[n]*S[n]. The file SIRV2.py contains a program based on filling an array p.
The effect of a vaccination campaign is illustrated in Fig. 4.14. All the data are as in Fig. 4.13 (left), except that p is ten times stronger for a period of 10 days and p = 0 elsewhere.
4.3 Oscillating OneDimensional Systems
Numerous engineering constructions and devices contain materials that act like springs. Such springs give rise to oscillations, and controlling oscillations is a key engineering task. We shall now learn to simulate oscillating systems.
As always, we start with the simplest meaningful mathematical model, which for oscillations is a secondorder differential equation:
where ω is a given physical parameter. Equation (4.40) models a onedimensional system oscillating without damping (i.e., with negligible damping). Onedimensional here means that some motion takes place along one dimension only in some coordinate system. Along with (4.40) we need the two initial conditions \(u(0)\) and \(u^{\prime}(0)\).
4.3.1 Derivation of a Simple Model
Many engineering systems undergo oscillations, and differential equations constitute the key tool to understand, predict, and control the oscillations. We start with the simplest possible model that captures the essential dynamics of an oscillating system. Some body with mass m is attached to a spring and moves along a line without friction, see Fig. 4.15 for a sketch (rolling wheels indicate ‘‘no friction’’). When the spring is stretched (or compressed), the spring force pulls (or pushes) the body back and work ‘‘against’’ the motion. More precisely, let \(x(t)\) be the position of the body on the x axis, along which the body moves. The spring is not stretched when x = 0, so the force is zero, and x = 0 is hence the equilibrium position of the body. The spring force is −kx, where k is a constant to be measured. We assume that there are no other forces (e.g., no friction). Newton’s 2nd law of motion \(F=ma\) then has \(F=kx\) and \(a=\ddot{x}\),
which can be rewritten as
by introducing \(\omega=\sqrt{k/m}\) (which is very common).
Equation (4.42) is a secondorder differential equation, and therefore we need two initial conditions, one on the position \(x(0)\) and one on the velocity \(x^{\prime}(0)\). Here we choose the body to be at rest, but moved away from its equilibrium position:
The exact solution of (4.42) with these initial conditions is \(x(t)=X_{0}\cos\omega t\). This can easily be verified by substituting into (4.42) and checking the initial conditions. The solution tells that such a springmass system oscillates back and forth as described by a cosine curve.
The differential equation (4.42) appears in numerous other contexts. A classical example is a simple pendulum that oscillates back and forth. Physics books derive, from Newton’s second law of motion, that
where m is the mass of the body at the end of a pendulum with length L, g is the acceleration of gravity, and θ is the angle the pendulum makes with the vertical. Considering small angles θ, \(\sin\theta\approx\theta\), and we get (4.42) with \(x=\theta\), \(\omega=\sqrt{g/L}\), \(x(0)=\Theta\), and \(x^{\prime}(0)=0\), if Θ is the initial angle and the pendulum is at rest at t = 0.
4.3.2 Numerical Solution
We have not looked at numerical methods for handling secondorder derivatives, and such methods are an option, but we know how to solve firstorder differential equations and even systems of firstorder equations. With a little, yet very common, trick we can rewrite (4.42) as a firstorder system of two differential equations. We introduce \(u=x\) and \(v=x^{\prime}=u^{\prime}\) as two new unknown functions. The two corresponding equations arise from the definition \(v=u^{\prime}\) and the original equation (4.42):
(Notice that we can use \(u^{\prime\prime}=v^{\prime}\) to remove the secondorder derivative from Newton’s 2nd law.)
We can now apply the Forward Euler method to (4.43)–(4.44), exactly as we did in Sect. 4.2.2:
resulting in the computational scheme
4.3.3 Programming the Numerical Method; the Special Case
A simple program for (4.47)–(4.48) follows the same ideas as in Sect. 4.2.3:
(See file osc_FE.py .)
Since we already know the exact solution as \(u(t)=X_{0}\cos\omega t\), we have reasoned as follows to find an appropriate simulation interval \([0,T]\) and also how many points we should choose. The solution has a period \(P=2\pi/\omega\). (The period P is the time difference between two peaks of the \(u(t)\sim\cos\omega t\) curve.) Simulating for three periods of the cosine function, \(T=3P\), and choosing \(\Delta t\) such that there are 20 intervals per period gives \(\Delta t=P/20\) and a total of \(N_{t}=T/\Delta t\) intervals. The rest of the program is a straightforward coding of the Forward Euler scheme.
Figure 4.16 shows a comparison between the numerical solution and the exact solution of the differential equation. To our surprise, the numerical solution looks wrong. Is this discrepancy due to a programming error or a problem with the Forward Euler method?
First of all, even before trying to run the program, you should sit down and compute two steps in the time loop with a calculator so you have some intermediate results to compare with. Using \(X_{0}=2\), dt = 0.157079632679, and \(\omega=2\), we get \(u^{1}=2\), \(v^{1}=1.25663706\), \(u^{2}=1.80260791\), and \(v^{2}=2.51327412\). Such calculations show that the program is seemingly correct. (Later, we can use such values to construct a unit test and a corresponding test function.)
The next step is to reduce the discretization parameter \(\Delta t\) and see if the results become more accurate. Figure 4.17 shows the numerical and exact solution for the cases \(\Delta t=P/40,P/160,P/2000\). The results clearly become better, and the finest resolution gives graphs that cannot be visually distinguished. Nevertheless, the finest resolution involves 6000 computational intervals in total, which is considered quite much. This is no problem on a modern laptop, however, as the computations take just a fraction of a second.
Although 2000 intervals per oscillation period seem sufficient for an accurate numerical solution, the lower right graph in Fig. 4.17 shows that if we increase the simulation time, here to 20 periods, there is a little growth of the amplitude, which becomes significant over time. The conclusion is that the Forward Euler method has a fundamental problem with its growing amplitudes, and that a very small \(\Delta t\) is required to achieve satisfactory results. The longer the simulation is, the smaller \(\Delta t\) has to be. It is certainly time to look for more effective numerical methods!
4.3.4 A Magic Fix of the Numerical Method
In the Forward Euler scheme,
we can replace u ^{n} in the last equation by the recently computed value \(u^{n+1}\) from the first equation:
Before justifying this fix more mathematically, let us try it on the previous example. The results appear in Fig. 4.18. We see that the amplitude does not grow, but the phase is not entirely correct. After 40 periods (Fig. 4.18 right) we see a significant difference between the numerical and the exact solution. Decreasing \(\Delta t\) decreases the error. For example, with 2000 intervals per period, we only see a small phase error even after 50,000 periods (!). We can safely conclude that the fix results in an excellent numerical method!
Let us interpret the adjusted scheme mathematically. First we order (4.49)–(4.50) such that the difference approximations to derivatives become transparent:
We interpret (4.51) as the differential equation sampled at mesh point t _{ n }, because we have v ^{n} on the righthand side. The lefthand side is then a forward difference or Forward Euler approximation to the derivative \(u^{\prime}\), see Fig. 4.2. On the other hand, we interpret (4.52) as the differential equation sampled at mesh point \(t_{n+1}\), since we have \(u^{n+1}\) on the righthand side. In this case, the difference approximation on the lefthand side is a backward difference,
Figure 4.19 illustrates the backward difference. The error in the backward difference is proportional to \(\Delta t\), the same as for the forward difference (but the proportionality constant in the error term has different sign). The resulting discretization method for (4.52) is often referred to as a Backward Euler scheme.
To summarize, using a forward difference for the first equation and a backward difference for the second equation results in a much better method than just using forward differences in both equations.
The standard way of expressing this scheme in physics is to change the order of the equations,
and apply a forward difference to (4.53) and a backward difference to (4.54):
That is, first the velocity v is updated and then the position u, using the most recently computed velocity. There is no difference between (4.55)–(4.56) and (4.49)–(4.50) with respect to accuracy, so the order of the original differential equations does not matter. The scheme (4.55)–(4.56) goes under the names Semiimplicit Euler ^{Footnote 4} or EulerCromer. The implementation of (4.55)–(4.56) is found in the file osc_EC.py. The core of the code goes like
4.3.5 The 2ndOrder RungeKutta Method (or Heun’s Method)
A very popular method for solving scalar and vector ODEs of first order is the 2ndorder RungeKutta method (RK2), also known as Heun’s method. The idea, first thinking of a scalar ODE, is to form a centered difference approximation to the derivative between two time points:
The centered difference formula is visualized in Fig. 4.20 . The error in the centered difference is proportional to \(\Delta t^{2}\), one order higher than the forward and backward differences, which means that if we halve \(\Delta t\), the error is more effectively reduced in the centered difference since it is reduced by a factor of four rather than two.
The problem with such a centered scheme for the general ODE \(u^{\prime}=f(u,t)\) is that we get
which leads to difficulties since we do not know what \(u^{n+\frac{1}{2}}\) is. However, we can approximate the value of f between two time levels by the arithmetic average of the values at t _{ n } and \(t_{n+1}\):
This results in
which in general is a nonlinear algebraic equation for \(u^{n+1}\) if \(f(u,t)\) is not a linear function of u. To deal with the unknown term \(f(u^{n+1},t_{n+1})\), without solving nonlinear equations, we can approximate or predict \(u^{n+1}\) using a Forward Euler step:
This reasoning gives rise to the method
The scheme applies to both scalar and vector ODEs.
For an oscillating system with \(f=(v,\omega^{2}u)\) the file osc_Heun.py implements this method. The demo function in that file runs the simulation for 10 periods with 20 time steps per period. The corresponding numerical and exact solutions are shown in Fig. 4.21. We see that the amplitude grows, but not as much as for the Forward Euler method. However, the EulerCromer method is much better!
We should add that in problems where the Forward Euler method gives satisfactory approximations, such as growth/decay problems or the SIR model, the 2ndorder RungeKutta method or Heun’s method, usually works considerably better and produces greater accuracy for the same computational cost. It is therefore a very valuable method to be aware of, although it cannot compete with the EulerCromer scheme for oscillation problems. The derivation of the RK2/Heun scheme is also good general training in ‘‘numerical thinking’’.
4.3.6 Software for Solving ODEs
There is a jungle of methods for solving ODEs, and it would be nice to have easy access to implementations of a wide range of methods, especially the sophisticated and complicated adaptive methods that adjust \(\Delta t\) automatically to obtain a prescribed accuracy. The Python package Odespy ^{Footnote 5} gives easy access to a lot of numerical methods for ODEs.
The simplest possible example on using Odespy is to solve \(u^{\prime}=u\), \(u(0)=2\), for 100 time steps until t = 4:
In other words, you define your righthand side function f(u, t), initialize an Odespy solver object, set the initial condition, compute a collection of time points where you want the solution, and ask for the solution. The returned arrays u and t can be plotted directly: plot(t, u).
A nice feature of Odespy is that problem parameters can be arguments to the user’s f(u, t) function. For example, if our ODE problem is \(u^{\prime}=au+b\), with two problem parameters a and b, we may write our f function as
The extra, problemdependent arguments a and b can be transferred to this function if we collect their values in a list or tuple when creating the Odespy solver and use the f_args argument:
This is a good feature because problem parameters must otherwise be global variables – now they can be arguments in our righthand side function in a natural way. Exercise 4.16 asks you to make a complete implementation of this problem and plot the solution.
Using Odespy to solve oscillation ODEs like \(u^{\prime\prime}+\omega^{2}u=0\), reformulated as a system \(u^{\prime}=v\) and \(v^{\prime}=\omega^{2}u\), is done as follows. We specify a given number of time steps per period and compute the associated time steps and end time of the simulation (T), given a number of periods to simulate:
The last two statements are important since our two functions u and v in the ODE system are packed together in one array inside the Odespy solver. The solution of the ODE system is returned as a twodimensional array where the first column (sol[:,0]) stores u and the second (sol[:,1]) stores v. Plotting u and v is a matter of running plot(t, u, t, v).
Remark
In the righthand side function we write f(sol, t, omega) instead of f(u, t, omega) to indicate that the solution sent to f is a solution at time t where the values of u and v are packed together: sol = [u, v]. We might well use u as argument:
This just means that we redefine the name u inside the function to mean the solution at time t for the first component of the ODE system.
To switch to another numerical method, just substitute RK2 by the proper name of the desired method. Typing pydoc odespy in the terminal window brings up a list of all the implemented methods. This very simple way of choosing a method suggests an obvious extension of the code above: we can define a list of methods, run all methods, and compare their u curves in a plot. As Odespy also contains the EulerCromer scheme, we rewrite the system with \(v^{\prime}=\omega^{2}u\) as the first ODE and \(u^{\prime}=v\) as the second ODE, because this is the standard choice when using the EulerCromer method (also in Odespy):
This change of equations also affects the initial condition: the first component is zero and second is X_0 so we need to pass the list [0, X_0] to solver.set_ initial_condition.
The code osc_odespy.py contains the details:
A new feature in this code is the ability to plot only the last p periods, which allows us to perform long time simulations and watch the end results without a cluttered plot with too many periods. The syntax t[m:] plots the last m elements in t (a negative index in Python arrays/lists counts from the end).
We may compare Heun’s method (or equivalently the RK2 method) with the EulerCromer scheme:
Figure 4.22 shows how Heun’s method (the blue line with small disks) has considerable error in both amplitude and phase already after 14–20 periods (upper left), but using three times as many time steps makes the curves almost equal (upper right). However, after 194–200 periods the errors have grown (lower left), but can be sufficiently reduced by halving the time step (lower right).
With all the methods in Odespy at hand, it is now easy to start exploring other methods, such as backward differences instead of the forward differences used in the Forward Euler scheme. Exercise 4.17 addresses that problem.
Odespy contains quite sophisticated adaptive methods where the user is ‘‘guaranteed’’ to get a solution with prescribed accuracy. There is no mathematical guarantee, but the error will for most cases not deviate significantly from the user’s tolerance that reflects the accuracy. A very popular method of this type is the RungeKuttaFehlberg method, which runs a 4thorder RungeKutta method and uses a 5thorder RungeKutta method to estimate the error so that \(\Delta t\) can be adjusted to keep the error below a tolerance. This method is also widely known as ode45, because that is the name of the function implementing the method in Matlab. We can easily test the RungeKuttaFehlberg method as soon as we know the corresponding Odespy name, which is RKFehlberg:
Note that the time_intervals_per_period argument refers to the time points where we want the solution. These points are also the ones used for numerical computations in the odespy.EulerCromer solver, while the odespy.RKFehlberg solver will use an unknown set of time points since the time intervals are adjusted as the method runs. One can easily look at the points actually used by the method as these are available as an array solver.t_all (but plotting or examining the points requires modifications inside the compare method).
Figure 4.23 shows a computational example where the RungeKuttaFehlberg method is clearly superior to the EulerCromer scheme in long time simulations, but the comparison is not really fair because the RungeKuttaFehlberg method applies about twice as many time steps in this computation and performs much more work per time step. It is quite a complicated task to compare two so different methods in a fair way so that the computational work versus accuracy is scientifically well reported.
4.3.7 The 4thOrder RungeKutta Method
The 4thorder RungeKutta method (RK4) is clearly the most widely used method to solve ODEs. Its power comes from high accuracy even with not so small time steps.
The algorithm
We first just state the fourstage algorithm:
where
Application
We can run the same simulation as in Figs. 4.16, 4.18, and 4.21, for 40 periods. The 10 last periods are shown in Fig. 4.24. The results look as impressive as those of the EulerCromer method.
Implementation
The stages in the 4thorder RungeKutta method can easily be implemented as a modification of the osc_Heun.py code. Alternatively, one can use the osc_odespy.py code by just providing the argument odespy_methods= [odespy.RK4] to the compare function.
Derivation
The derivation of the 4thorder RungeKutta method can be presented in a pedagogical way that brings many fundamental elements of numerical discretization techniques together and that illustrates many aspects of ‘‘numerical thinking’’ when constructing approximate solution methods.
We start with integrating the general ODE \(u^{\prime}=f(u,t)\) over a time step, from t _{ n } to \(t_{n+1}\),
The goal of the computation is \(u(t_{n+1})\) (\(u^{n+1}\)), while \(u(t_{n})\) (u ^{n}) is the most recently known value of u. The challenge with the integral is that the integrand involves the unknown u between t _{ n } and \(t_{n+1}\).
The integral can be approximated by the famous Simpson’s rule ^{Footnote 6}:
The problem with this formula is that we do not know \(f^{n+\frac{1}{2}}=f(u^{n+\frac{1}{2}},t_{n+\frac{1}{2}})\) and \(f^{n+1}=(u^{n+1},t_{n+1})\) as only u ^{n} is available and only f ^{n} can then readily be computed.
To proceed, the idea is to use various approximations for \(f^{n+\frac{1}{2}}\) and \(f^{n+1}\) based on using wellknown schemes for the ODE in the intervals \([t_{n},t_{n+\frac{1}{2}}]\) and \([t_{n},t_{n+1}]\). Let us split the integral into four terms:
where \(\hat{f}^{n+\frac{1}{2}}\), \(\tilde{f}^{n+\frac{1}{2}}\), and \(\bar{f}^{n+1}\) are approximations to \(f^{n+\frac{1}{2}}\) and \(f^{n+1}\) that can utilize already computed quantities. For \(\hat{f}^{n+\frac{1}{2}}\) we can simply apply an approximation to \(u^{n+\frac{1}{2}}\) based on a Forward Euler step of size \(\frac{1}{2}\Delta t\):
This formula provides a prediction of \(f^{n+\frac{1}{2}}\), so we can for \(\tilde{f}^{n+\frac{1}{2}}\) try a Backward Euler method to approximate \(u^{n+\frac{1}{2}}\):
With \(\tilde{f}^{n+\frac{1}{2}}\) as an approximation to \(f^{n+\frac{1}{2}}\), we can for the final term \(\bar{f}^{n+1}\) use a midpoint method (or central difference, also called a CrankNicolson method) to approximate \(u^{n+1}\):
We have now used the Forward and Backward Euler methods as well as the centered difference approximation in the context of Simpson’s rule. The hope is that the combination of these methods yields an overall timestepping scheme from t _{ n } to \(t_{n}{+1}\) that is much more accurate than the individual steps which have errors proportional to \(\Delta t\) and \(\Delta t^{2}\). This is indeed true: the numerical error goes in fact like \(C\Delta t^{4}\) for a constant C, which means that the error approaches zero very quickly as we reduce the time step size, compared to the Forward Euler method (error \(\sim\Delta t\)), the EulerCromer method (error \(\sim\Delta t\)) or the 2ndorder RungeKutta, or Heun’s, method (error \(\sim\Delta t^{2}\)).
Note that the 4thorder RungeKutta method is fully explicit so there is never any need to solve linear or nonlinear algebraic equations, regardless of what f looks like. However, the stability is conditional and depends on f. There is a large family of implicit RungeKutta methods that are unconditionally stable, but require solution of algebraic equations involving f at each time step. The Odespy package has support for a lot of sophisticated explicit RungeKutta methods, but not yet implicit RungeKutta methods.
4.3.8 More Effects: Damping, Nonlinearity, and External Forces
Our model problem \(u^{\prime\prime}+\omega^{2}u=0\) is the simplest possible mathematical model for oscillating systems. Nevertheless, this model makes strong demands to numerical methods, as we have seen, and is very useful as a benchmark for evaluating the performance of numerical methods.
Reallife applications involve more physical effects, which lead to a differential equation with more terms and also more complicated terms. Typically, one has a damping force \(f(u^{\prime})\) and a spring force \(s(u)\). Both these forces may depend nonlinearly on their argument, \(u^{\prime}\) or u. In addition, environmental forces \(F(t)\) may act on the system. For example, the classical pendulum has a nonlinear ‘‘spring’’ or restoring force \(s(u)\sim\sin(u)\), and air resistance on the pendulum leads to a damping force \(f(u^{\prime})\simu^{\prime}u^{\prime}\). Examples on environmental forces include shaking of the ground (e.g., due to an earthquake) as well as forces from waves and wind.
With three types of forces on the system: F, f, and s, the sum of forces is written \(F(t)f(u^{\prime})s(u)\). Note the minus sign in front of f and s, which indicates that these functions are defined such that they represent forces acting against the motion. For example, springs attached to the wheels in a car are combined with effective dampers, each providing a damping force \(f(u^{\prime})=bu^{\prime}\) that acts against the spring velocity \(u^{\prime}\). The corresponding physical force is then −f: \(bu^{\prime}\), which points downwards when the spring is being stretched (and \(u^{\prime}\) points upwards), while −f acts upwards when the spring is being compressed (and \(u^{\prime}\) points downwards).
Figure 4.25 shows an example of a mass m attached to a potentially nonlinear spring and dashpot, and subject to an environmental force \(F(t)\). Nevertheless, our general model can equally well be a pendulum as in Fig. 4.26 with \(s(u)=mg\sin\theta\) and \(f(\dot{u})=\frac{1}{2}C_{D}A\varrho\dot{\theta}\dot{\theta}\) (where \(C_{D}=0.4\), A is the cross sectional area of the body, and \(\varrho\) is the density of air).
Newton’s second law for the system can be written with the mass times acceleration on the lefthand side and the forces on the righthand side:
This equation is, however, more commonly reordered to
Because the differential equation is of second order, due to the term \(u^{\prime\prime}\), we need two initial conditions:
Note that with the choices \(f(u^{\prime})=0\), \(s(u)=ku\), and \(F(t)=0\) we recover the original ODE \(u^{\prime\prime}+\omega^{2}u=0\) with \(\omega=\sqrt{k/m}\).
How can we solve (4.66)? As for the simple ODE \(u^{\prime\prime}+\omega^{2}u=0\), we start by rewriting the secondorder ODE as a system of two firstorder ODEs:
The initial conditions become \(u(0)=U_{0}\) and \(v(0)=V_{0}\).
Any method for a system of firstorder ODEs can be used to solve for \(u(t)\) and \(v(t)\).
The EulerCromer scheme
An attractive choice from an implementational, accuracy, and efficiency point of view is the EulerCromer scheme where we take a forward difference in (4.68) and a backward difference in (4.69):
We can easily solve for the new unknowns \(v^{n+1}\) and \(u^{n+1}\):
Remark on the ordering of the ODEs
The ordering of the ODEs in the ODE system is important for the extended model (4.68)–(4.69). Imagine that we write the equation for \(u^{\prime}\) first and then the one for \(v^{\prime}\). The EulerCromer method would then first use a forward difference for \(u^{n+1}\) and then a backward difference for \(v^{n+1}\). The latter would lead to a nonlinear algebraic equation for \(v^{n+1}\),
if \(f(v)\) is a nonlinear function of v. This would require a numerical method for nonlinear algebraic equations to find \(v^{n+1}\), while updating \(v^{n+1}\) through a forward difference gives an equation for \(v^{n+1}\) that is linear and trivial to solve by hand.
The file osc_EC_general.py has a function EulerCromer that implements this method:
The 4th order RungeKutta method
The RK4 method just evaluates the righthand side of the ODE system,
for known values of u, v, and t, so the method is very simple to use regardless of how the functions \(s(u)\) and \(f(v)\) are chosen.
4.3.9 Illustration of Linear Damping
We consider an engineering system with a linear spring, \(s(u)=kx\), and a viscous damper, where the damping force is proportional to \(u^{\prime}\), \(f(u^{\prime})=bu^{\prime}\), for some constant b > 0. This choice may model the vertical spring system in a car (but engineers often like to illustrate such a system by a horizontal moving mass like the one depicted in Fig. 4.25). We may choose simple values for the constants to illustrate basic effects of damping (and later excitations). Choosing the oscillations to be the simple \(u(t)=\cos t\) function in the undamped case, we may set m = 1, k = 1, b = 0.3, \(U_{0}=1\), \(V_{0}=0\). The following function implements this case:
The plot_u function is a collection of plot statements for plotting \(u(t)\), or a part of it. Figure 4.27 shows the effect of the \(bu^{\prime}\) term: we have oscillations with (an approximate) period \(2\pi\), as expected, but the amplitude is efficiently damped.
Remark about working with a scaled problem
Instead of setting b = 0.3 and \(m=k=U_{0}=1\) as fairly ‘‘unlikely’’ physical values, it would be better to scale the equation \(mu^{\prime\prime}+bu^{\prime}+ku=0\). This means that we introduce dimensionless independent and dependent variables:
where t _{ c } and u _{ c } are characteristic sizes of time and displacement, respectively, such that \(\bar{t}\) and \(\bar{u}\) have their typical size around unity. In the present problem, we can choose \(u_{c}=U_{0}\) and \(t_{c}=\sqrt{m/k}\). This gives the following scaled (or dimensionless) problem for the dimensionless quantity \(\bar{u}(\bar{t})\):
The striking fact is that there is only one physical parameter in this problem: the dimensionless number β. Solving this problem corresponds to solving the original problem (with dimensions) with the parameters \(m=k=U_{0}=1\) and \(b=\beta\). However, solving the dimensionless problem is more general: if we have a solution \(\bar{u}(\bar{t};\beta)\), we can find the physical solution of a range of problems since
As long as β is fixed, we can find u for any U _{0}, k, and m from the above formula! In this way, a time consuming simulation can be done only once, but still provide many solutions. This demonstrates the power of working with scaled or dimensionless problems.
4.3.10 Illustration of Linear Damping with Sinusoidal Excitation
We now extend the previous example to also involve some external oscillating force on the system: \(F(t)=A\sin(wt)\). Driving a car on a road with sinusoidal bumps might give such an external excitation on the spring system in the car (w is related to the velocity of the car).
With A = 0.5 and w = 3,
we get the graph in Fig. 4.28. The striking difference from Fig. 4.27 is that the oscillations start out as a damped \(\cos t\) signal without much influence of the external force, but then the free oscillations of the undamped system (\(\cos t\)) \(u^{\prime\prime}+u=0\) die out and the external force \(0.5\sin(3t)\) induces oscillations with a shorter period \(2\pi/3\). You are encouraged to play around with a larger A and switch from a sine to a cosine in F and observe the effects. If you look this up in a physics book, you can find exact analytical solutions to the differential equation problem in these cases.
A particularly interesting case arises when the excitation force has the same frequency as the free oscillations of the undamped system, i.e., \(F(t)=A\sin t\). With the same amplitude A = 0.5, but a smaller damping b = 0.1, the oscillations in Fig. 4.28 becomes qualitatively very different as the amplitude grows significantly larger over some periods. This phenomenon is called resonance and is exemplified in Fig. 4.29 . Removing the damping results in an amplitude that grows linearly in time.
4.3.11 SpringMass System with Sliding Friction
A body with mass m is attached to a spring with stiffness k while sliding on a plane surface. The body is also subject to a friction force \(f(u^{\prime})\) due to the contact between the body and the plane. Figure 4.30 depicts the situation. The friction force \(f(u^{\prime})\) can be modeled by Coulomb friction:
where μ is the friction coefficient, and mg is the normal force on the surface where the body slides. This formula can also be written as \(f(u^{\prime})=\mu mg\,\mbox{sign}(u^{\prime})\), provided the signum function \(\mbox{sign}(x)\) is defined to be zero for x = 0 (numpy.sign has this property). To check that the signs in the definition of f are right, recall that the actual physical force is −f and this is positive (i.e., f < 0) when it works against the body moving with velocity \(u^{\prime}<0\).
The nonlinear spring force is taken as
which is approximately −ku for small u, but stabilizes at \(\pm k/\alpha\) for large \(\pm\alpha u\). Here is a plot with k = 1000 and \(u\in[0.1,0.1]\) for three α values:
If there is no external excitation force acting on the body, we have the equation of motion
Let us simulate a situation where a body of mass 1 kg slides on a surface with \(\mu=0.4\), while attached to a spring with stiffness \(k=1000\,\hbox{kg}/\hbox{s}^{2}\). The initial displacement of the body is 10 cm, and the α parameter in \(s(u)\) is set to 60 1/m. Using the EulerCromer function from the osc_EC_general code, we can write a function sliding_friction for solving this problem:
Running the sliding_friction function gives us the results in Fig. 4.31 with \(s(u)=k\alpha^{1}\tanh(\alpha u)\) (left) and the linearized version \(s(u)=ku\) (right).
4.3.12 A finite Difference Method; Undamped, Linear Case
We shall now address numerical methods for the secondorder ODE
without rewriting the ODE as a system of firstorder ODEs. The primary motivation for ‘‘yet another solution method’’ is that the discretization principles result in a very good scheme, and more importantly, the thinking around the discretization can be reused when solving partial differential equations.
The main idea of this numerical method is to approximate the secondorder derivative \(u^{\prime\prime}\) by a finite difference. While there are several choices of difference approximations to firstorder derivatives, there is one dominating formula for the secondorder derivative:
The error in this approximation is proportional to \(\Delta t^{2}\). Letting the ODE be valid at some arbitrary time point t _{ n },
we just insert the approximation (4.74) to get
We now assume that \(u^{n1}\) and u ^{n} are already computed and that \(u^{n+1}\) is the new unknown. Solving with respect to \(u^{n+1}\) gives
A major problem arises when we want to start the scheme. We know that \(u^{0}=U_{0}\), but applying (4.76) for n = 0 to compute u ^{1} leads to
where we do not know \(u^{1}\). The initial condition \(u^{\prime}(0)=0\) can help us to eliminate \(u^{1}\)  and this condition must anyway be incorporated in some way. To this end, we discretize \(u^{\prime}(0)=0\) by a centered difference,
It follows that \(u^{1}=u^{1}\), and we can use this relation to eliminate \(u^{1}\) in (4.77):
With \(u^{0}=U_{0}\) and u ^{1} computed from (4.78), we can compute u ^{2}, u ^{3}, and so forth from (4.76). Exercise 4.19 asks you to explore how the steps above are modified in case we have a nonzero initial condition \(u^{\prime}(0)=V_{0}\).
Remark on a simpler method for computing u ^{1}
We could approximate the initial condition \(u^{\prime}(0)\) by a forward difference:
leading to \(u^{1}=u^{0}\). Then we can use (4.76) for the coming time steps. However, this forward difference has an error proportional to \(\Delta t\), while the centered difference we used has an error proportional to \(\Delta t^{2}\), which is compatible with the accuracy (error goes like \(\Delta t^{2}\)) used in the discretization of the differential equation.
The method for the secondorder ODE described above goes under the name Störmer’s method or Verlet integration ^{Footnote 7}. It turns out that this method is mathematically equivalent with the EulerCromer scheme (!). Or more precisely, the general formula (4.76) is equivalent with the EulerCromer formula, but the scheme for the first time level (4.78) implements the initial condition \(u^{\prime}(0)\) slightly more accurately than what is naturally done in the EulerCromer scheme. The latter will do
which differs from u ^{1} in (4.78) by an amount \(\frac{1}{2}\Delta t^{2}\omega^{2}u^{0}\).
Because of the equivalence of (4.76) with the EulerCromer scheme, the numerical results will have the same nice properties such as a constant amplitude. There will be a phase error as in the EulerCromer scheme, but this error is effectively reduced by reducing \(\Delta t\), as already demonstrated.
The implementation of (4.78) and (4.76) is straightforward in a function (file osc_2nd_order.py ):
4.3.13 A Finite Difference Method; Linear Damping
A key issue is how to generalize the scheme from Sect. 4.3.12 to a differential equation with more terms. We start with the case of a linear damping term \(f(u^{\prime})=bu^{\prime}\), a possibly nonlinear spring force \(s(u)\), and an excitation force \(F(t)\):
We need to find the appropriate difference approximation to \(u^{\prime}\) in the \(bu^{\prime}\) term. A good choice is the centered difference
Sampling the equation at a time point t _{ n },
and inserting the finite difference approximations to \(u^{\prime\prime}\) and \(u^{\prime}\) results in
where F ^{n} is a short notation for \(F(t_{n})\). Equation (4.81) is linear in the unknown \(u^{n+1}\), so we can easily solve for this quantity:
As in the case without damping, we need to derive a special formula for u ^{1}. The initial condition \(u^{\prime}(0)=0\) implies also now that \(u^{1}=u^{1}\), and with (4.82) for n = 0, we get
In the more general case with a nonlinear damping term \(f(u^{\prime})\),
we get
which is a nonlinear algebraic equation for \(u^{n+1}\) that must be solved by numerical methods. A much more convenient scheme arises from using a backward difference for \(u^{\prime}\),
because the damping term will then be known, involving only u ^{n} and \(u^{n1}\), and we can easily solve for \(u^{n+1}\).
The downside of the backward difference compared to the centered difference (4.80) is that it reduces the order of the accuracy in the overall scheme from \(\Delta t^{2}\) to \(\Delta t\). In fact, the EulerCromer scheme evaluates a nonlinear damping term as \(f(v^{n})\) when computing \(v^{n+1}\), and this is equivalent to using the backward difference above. Consequently, the convenience of the EulerCromer scheme for nonlinear damping comes at a cost of lowering the overall accuracy of the scheme from second to first order in \(\Delta t\). Using the same trick in the finite difference scheme for the secondorder differential equation, i.e., using the backward difference in \(f(u^{\prime})\), makes this scheme equally convenient and accurate as the EulerCromer scheme in the general nonlinear case \(mu^{\prime\prime}+f(u^{\prime})+s(u)=F\).
4.4 Exercises
Exercise 4.1 (Geometric construction of the Forward Euler method)
Section 4.1.4 describes a geometric interpretation of the Forward Euler method. This exercise will demonstrate the geometric construction of the solution in detail. Consider the differential equation \(u^{\prime}=u\) with \(u(0)=1\). We use time steps \(\Delta t=1\).

a)
Start at t = 0 and draw a straight line with slope \(u^{\prime}(0)=u(0)=1\). Go one time step forward to \(t=\Delta t\) and mark the solution point on the line.

b)
Draw a straight line through the solution point \((\Delta t,u^{1})\) with slope \(u^{\prime}(\Delta t)=u^{1}\). Go one time step forward to \(t=2\Delta t\) and mark the solution point on the line.

c)
Draw a straight line through the solution point \((2\Delta t,u^{2})\) with slope \(u^{\prime}(2\Delta t)=u^{2}\). Go one time step forward to \(t=3\Delta t\) and mark the solution point on the line.

d)
Set up the Forward Euler scheme for the problem \(u^{\prime}=u\). Calculate u ^{1}, u ^{2}, and u ^{3}. Check that the numbers are the same as obtained in a)c).
Filename: ForwardEuler_geometric_solution.py.
Exercise 4.2 (Make test functions for the Forward Euler method)
The purpose of this exercise is to make a file test_ode_FE.py that makes use of the ode_FE function in the file ode_FE.py and automatically verifies the implementation of ode_FE.

a)
The solution computed by hand in Exercise 4.1 can be used as a reference solution. Make a function test_ode_FE_1() that calls ode_FE to compute three time steps in the problem \(u^{\prime}=u\), \(u(0)=1\), and compare the three values u ^{1}, u ^{2}, and u ^{3} with the values obtained in Exercise 4.1.

b)
The test in a) can be made more general using the fact that if f is linear in u and does not depend on t, i.e., we have \(u^{\prime}=ru\), for some constant r, the Forward Euler method has a closed form solution as outlined in Sect. 4.1.1: \(u^{n}=U_{0}(1+r\Delta t)^{n}\). Use this result to construct a test function test_ode_FE_2() that runs a number of steps in ode_FE and compares the computed solution with the listed formula for u ^{n}.
Filename: test_ode_FE.py.
Exercise 4.3 (Implement and evaluate Heun’s method)

a)
A 2ndorder RungeKutta method, also known has Heun’s method, is derived in Sect. 4.3.5. Make a function ode_Heun(f, U_0, dt, T) (as a counterpart to ode_FE(f, U_0, dt, T) in ode_FE.py) for solving a scalar ODE problem \(u^{\prime}=f(u,t)\), \(u(0)=U_{0}\), \(t\in(0,T]\), with this method using a time step size \(\Delta t\).

b)
Solve the simple ODE problem \(u^{\prime}=u\), \(u(0)=1\), by the ode_Heun and the ode_FE function. Make a plot that compares Heun’s method and the Forward Euler method with the exact solution \(u(t)=e^{t}\) for \(t\in[0,6]\). Use a time step \(\Delta t=0.5\).

c)
For the case in b), find through experimentation the largest value of \(\Delta t\) where the exact solution and the numerical solution by Heun’s method cannot be distinguished visually. It is of interest to see how far off the curve the Forward Euler method is when Heun’s method can be regarded as ‘‘exact’’ (for visual purposes).
Filename: ode_Heun.py.
Exercise 4.4 (Find an appropriate time step; logistic model)
Compute the numerical solution of the logistic equation for a set of repeatedly halved time steps: \(\Delta t_{k}=2^{k}\Delta t\), \(k=0,1,\ldots\). Plot the solutions corresponding to the last two time steps \(\Delta t_{k}\) and \(\Delta t_{k1}\) in the same plot. Continue doing this until you cannot visually distinguish the two curves in the plot. Then one has found a sufficiently small time step.
Hint
Extend the logistic.py file. Introduce a loop over k, write out \(\Delta t_{k}\), and ask the user if the loop is to be continued.
Filename: logistic_dt.py.
Exercise 4.5 (Find an appropriate time step; SIR model)
Repeat Exercise 4.4 for the SIR model.
Hint
Import the ode_FE function from the ode_system_FE module and make a modified demo_SIR function that has a loop over repeatedly halved time steps. Plot S, I, and R versus time for the two last time step sizes in the same plot.
Filename: SIR_dt.py.
Exercise 4.6 (Model an adaptive vaccination campaign)
In the SIRV model with timedependent vaccination from Sect. 4.2.9, we want to test the effect of an adaptive vaccination campaign where vaccination is offered as long as half of the population is not vaccinated. The campaign starts after Δ days. That is, \(p=p_{0}\) if \(V<\frac{1}{2}(S^{0}+I^{0})\) and \(t> \Delta\) days, otherwise p = 0.
Demonstrate the effect of this vaccination policy: choose β, γ, and ν as in Sect. 4.2.9, set p = 0.001, \(\Delta=10\) days, and simulate for 200 days.
Hint
This discontinuous \(p(t)\) function is easiest implemented as a Python function containing the indicated if test. You may use the file SIRV1.py as starting point, but note that it implements a timedependent \(p(t)\) via an array.
Filename: SIRV_p_adapt.py.
Exercise 4.7 (Make a SIRV model with timelimited effect of vaccination)
We consider the SIRV model from Sect. 4.2.8, but now the effect of vaccination is timelimited. After a characteristic period of time, π, the vaccination is no more effective and individuals are consequently moved from the V to the S category and can be infected again. Mathematically, this can be modeled as an average leakage \(\pi^{1}V\) from the V category to the S category (i.e., a gain \(\pi^{1}V\) in the latter). Write up the complete model, implement it, and rerun the case from Sect. 4.2.8 with various choices of parameters to illustrate various effects.
Filename: SIRV1_V2S.py.
Exercise 4.8 (Refactor a flat program)
Consider the file osc_FE.py implementing the Forward Euler method for the oscillating system model (4.43)–(4.44). The osc_FE.py is what we often refer to as a flat program, meaning that it is just one main program with no functions. To easily reuse the numerical computations in other contexts, place the part that produces the numerical solution (allocation of arrays, initializing the arrays at time zero, and the time loop) in a function osc_FE(X_0, omega, dt, T), which returns u, v, t. Place the particular computational example in osc_FE.py in a function demo(). Construct the file osc_FE_func.py such that the osc_FE function can easily be reused in other programs. In Python, this means that osc_FE_func.py is a module that can be imported in other programs. The requirement of a module is that there should be no main program, except in the test block. You must therefore call demo from a test block (i.e., the block after if __name__ == ’__main__’).
Filename: osc_FE_func.py.
Exercise 4.9 (Simulate oscillations by a general ODE solver)
Solve the system (4.43)–(4.44) using the general solver ode_FE in the file ode_ system_FE.py described in Sect. 4.2.6. Program the ODE system and the call to the ode_FE function in a separate file osc_ode_FE.py.
Equip this file with a test function that reads a file with correct u values and compares these with those computed by the ode_FE function. To find correct u values, modify the program osc_FE.py to dump the u array to file, run osc_FE.py, and let the test function read the reference results from that file.
Filename: osc_ode_FE.py.
Exercise 4.10 (Compute the energy in oscillations)

a)
Make a function osc_energy(u, v, omega) for returning the potential and kinetic energy of an oscillating system described by (4.43)–(4.44). The potential energy is taken as \(\frac{1}{2}\omega^{2}u^{2}\) while the kinetic energy is \(\frac{1}{2}v^{2}\). (Note that these expressions are not exactly the physical potential and kinetic energy, since these would be \(\frac{1}{2}mv^{2}\) and \(\frac{1}{2}ku^{2}\) for a model \(mx^{\prime\prime}+kx=0\).)
Place the osc_energy in a separate file osc_energy.py such that the function can be called from other functions.

b)
Add a call to osc_energy in the programs osc_FE.py and osc_EC.py and plot the sum of the kinetic and potential energy. How does the total energy develop for the Forward Euler and the EulerCromer schemes?
Filenames: osc_energy.py, osc_FE_energy.py, osc_EC_energy.py.
Exercise 4.11 (Use a Backward Euler scheme for population growth)
We consider the ODE problem \(N^{\prime}(t)=rN(t)\), \(N(0)=N_{0}\). At some time, \(t_{n}=n\Delta t\), we can approximate the derivative \(N^{\prime}(t_{n})\) by a backward difference, see Fig. 4.19:
which leads to
called the Backward Euler scheme.

a)
Find an expression for the N ^{n} in terms of \(N^{n1}\) and formulate an algorithm for computing N ^{n}, \(n=1,2,\ldots,N_{t}\).

b)
Implement the algorithm in a) in a function growth_BE(N_0, dt, T) for solving \(N^{\prime}=rN\), \(N(0)=N_{0}\), \(t\in(0,T]\), with time step \(\Delta t\) (dt).

c)
Implement the Forward Euler scheme in a function growth_FE(N_0, dt, T) as described in b).

d)
Compare visually the solution produced by the Forward and Backward Euler schemes with the exact solution when r = 1 and T = 6. Make two plots, one with \(\Delta t=0.5\) and one with \(\Delta t=0.05\).
Filename: growth_BE.py.
Exercise 4.12 (Use a CrankNicolson scheme for population growth)
It is recommended to do Exercise 4.11 prior to the present one. Here we look at the same population growth model \(N^{\prime}(t)=rN(t)\), \(N(0)=N_{0}\). The time derivative \(N^{\prime}(t)\) can be approximated by various types of finite differences. Exercise 4.11 considers a backward difference (Fig. 4.19), while Sect. 4.1.2 explained the forward difference (Fig. 4.2). A centered difference is more accurate than a backward or forward difference:
This type of difference, applied at the point \(t_{n+\frac{1}{2}}=t_{n}+\frac{1}{2}\Delta t\), is illustrated geometrically in Fig. 4.20.

a)
Insert the finite difference approximation in the ODE \(N^{\prime}=rN\) and solve for the unknown \(N^{n+1}\), assuming N ^{n} is already computed and hence known. The resulting computational scheme is often referred to as a CrankNicolson scheme.

b)
Implement the algorithm in a) in a function growth_CN(N_0, dt, T) for solving \(N^{\prime}=rN\), \(N(0)=N_{0}\), \(t\in(0,T]\), with time step \(\Delta t\) (dt).

c)
Make plots for comparing the CrankNicolson scheme with the Forward and Backward Euler schemes in the same test problem as in Exercise 4.11 .
Filename: growth_CN.py.
Exercise 4.13 (Understand finite differences via Taylor series)
The Taylor series around a point \(x=a\) can for a function \(f(x)\) be written
For a function of time, as addressed in our ODE problems, we would use u instead of f, t instead of x, and a time point t _{ n } instead of a:

a)
A forward finite difference approximation to the derivative \(f^{\prime}(a)\) reads
$$u^{\prime}(t_{n})\approx\frac{u(t_{n}+\Delta t)u(t_{n})}{\Delta t}\thinspace.$$We can justify this formula mathematically through Taylor series. Write up the Taylor series for \(u(t_{n}+\Delta t)\) (around \(t=t_{n}\), as given above), and then solve the expression with respect to \(u^{\prime}(t_{n})\). Identify, on the righthand side, the finite difference approximation and an infinite series. This series is then the error in the finite difference approximation. If \(\Delta t\) is assumed small (i.e. \(\Delta t<<1\)), \(\Delta t\) will be much larger than \(\Delta t^{2}\), which will be much larger than \(\Delta t^{3}\), and so on. The leading order term in the series for the error, i.e., the error with the least power of \(\Delta t\) is a good approximation of the error. Identify this term.

b)
Repeat a) for a backward difference:
$$u^{\prime}(t_{n})\approx\frac{u(t_{n})u(t_{n}\Delta t)}{\Delta t}\thinspace.$$This time, write up the Taylor series for \(u(t_{n}\Delta t)\) around t _{ n }. Solve with respect to \(u^{\prime}(t_{n})\), and identify the leading order term in the error. How is the error compared to the forward difference?

c)
A centered difference approximation to the derivative, as explored in Exercise 4.12, can be written
$$u^{\prime}(t_{n}+\frac{1}{2}\Delta t)\approx\frac{u(t_{n}+\Delta t)u(t_{n})}{\Delta t}\thinspace.$$Write up the Taylor series for \(u(t_{n})\) around \(t_{n}+\frac{1}{2}\Delta t\) and the Taylor series for \(u(t_{n}+\Delta t)\) around \(t_{n}+\frac{1}{2}\Delta t\). Subtract the two series, solve with respect to \(u^{\prime}(t_{n}+\frac{1}{2}\Delta t)\), identify the finite difference approximation and the error terms on the righthand side, and write up the leading order error term. How is this term compared to the ones for the forward and backward differences?

d)
Can you use the leading order error terms in a)c) to explain the visual observations in the numerical experiment in Exercise 4.12?

e)
Find the leading order error term in the following standard finite difference approximation to the secondorder derivative:
$$u^{\prime\prime}(t_{n})\approx\frac{u(t_{n}+\Delta t)2u(t_{n})+u(t_{n}\Delta t)}{\Delta t}\thinspace.$$
Hint
Express \(u(t_{n}\pm\Delta t)\) via Taylor series and insert them in the difference formula.
Filename: Taylor_differences.pdf.
Exercise 4.14 (Use a Backward Euler scheme for oscillations)
Consider (4.43)–(4.44) modeling an oscillating engineering system. This \(2\times 2\) ODE system can be solved by the Backward Euler scheme, which is based on discretizing derivatives by collecting information backward in time. More specifically, \(u^{\prime}(t)\) is approximated as
A general vector ODE \(u^{\prime}=f(u,t)\), where u and f are vectors, can use this approximation as follows:
which leads to an equation for the new value u ^{n}:
For a general f, this is a system of nonlinear algebraic equations.
However, the ODE (4.43)–(4.44) is linear, so a Backward Euler scheme leads to a system of two algebraic equations for two unknowns:

a)
Solve the system for u ^{n} and v ^{n}.

b)
Implement the found formulas for u ^{n} and v ^{n} in a program for computing the entire numerical solution of (4.43)–(4.44).

c)
Run the program with a \(\Delta t\) corresponding to 20 time steps per period of the oscillations (see Sect. 4.3.3 for how to find such a \(\Delta t\)). What do you observe? Increase to 2000 time steps per period. How much does this improve the solution?
Filename: osc_BE.py.
Remarks
While the Forward Euler method applied to oscillation problems \(u^{\prime\prime}+\omega^{2}u=0\) gives growing amplitudes, the Backward Euler method leads to significantly damped amplitudes.
Exercise 4.15 (Use Heun’s method for the SIR model)
Make a program that computes the solution of the SIR model from Sect. 4.2.1 both by the Forward Euler method and by Heun’s method (or equivalently: the 2ndorder RungeKutta method) from Sect. 4.3.5. Compare the two methods in the simulation case from Sect. 4.2.3. Make two comparison plots, one for a large and one for a small time step. Experiment to find what ‘‘large’’ and ‘‘small’’ should be: the large one gives significant differences, while the small one lead to very similar curves.
Filename: SIR_Heun.py.
Exercise 4.16 (Use Odespy to solve a simple ODE)
Solve
by the Odespy software. Let the problem parameters a and b be arguments to the righthand side function that specifies the ODE to be solved. Plot the solution for the case when a = 2, b = 1, \(T=6/a\), and we use 100 time intervals in \([0,T]\).
Filename: odespy_demo.py.
Exercise 4.17 (Set up a Backward Euler scheme for oscillations)
Write the ODE \(u^{\prime\prime}+\omega^{2}u=0\) as a system of two firstorder ODEs and discretize these with backward differences as illustrated in Fig. 4.19. The resulting method is referred to as a Backward Euler scheme. Identify the matrix and righthand side of the linear system that has to be solved at each time level. Implement the method, either from scratch yourself or using Odespy (the name is odespy.BackwardEuler). Demonstrate that contrary to a Forward Euler scheme, the Backward Euler scheme leads to significant nonphysical damping. The figure below shows that even with 60 time steps per period, the results after a few periods are useless:
Filename: osc_BE.py.
Exercise 4.18 (Set up a Forward Euler scheme for nonlinear and damped oscillations)
Derive a Forward Euler method for the ODE system (4.68)–(4.69). Compare the method with the EulerCromer scheme for the sliding friction problem from Sect. 4.3.11:

1.
Does the Forward Euler scheme give growing amplitudes?

2.
Is the period of oscillation accurate?

3.
What is the required time step size for the two methods to have visually coinciding curves?
Filename: osc_FE_general.py.
Exercise 4.19 (Discretize an initial condition)
Assume that the initial condition on \(u^{\prime}\) is nonzero in the finite difference method from Sect. 4.3.12: \(u^{\prime}(0)=V_{0}\). Derive the special formula for u ^{1} in this case.
Filename: ic_with_V_0.pdf.
Notes
 1.
The term scheme is used as synonym for method or computational recipe, especially in the context of numerical methods for differential equations.
 2.
 3.
 4.
 5.
 6.
 7.
Author information
Authors and Affiliations
Corresponding authors
Rights and permissions
Open Access This chapter is distributed under the terms of the Creative Commons Attribution‐NonCommercial 4.0 International License (http://creativecommons.org/licenses/bync/4.0/), which permits any noncommercial use, duplication, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, a link is provided to the Creative Commons license and any changes made are indicated.
The images or other third party material in this chapter are included in the work’s Creative Commons license, unless indicated otherwise in the credit line; if such material is not included in the work’s Creative Commons license and the respective action is not permitted by statutory regulation, users will need to obtain permission from the license holder to duplicate, adapt or reproduce the material.
Copyright information
© 2016 The Author(s)
About this chapter
Cite this chapter
Linge, S., Langtangen, H.P. (2016). Solving Ordinary Differential Equations. In: Programming for Computations  Python. Texts in Computational Science and Engineering, vol 15. Springer, Cham. https://doi.org/10.1007/9783319324289_4
Download citation
DOI: https://doi.org/10.1007/9783319324289_4
Published:
Publisher Name: Springer, Cham
Print ISBN: 9783319324272
Online ISBN: 9783319324289
eBook Packages: Mathematics and StatisticsMathematics and Statistics (R0)