## Keywords

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.

This chapter presents many mathematical models that all end up with ODEs of the type $$u^{\prime}=-au+b$$. The applications are taken from biology, finance, and physics, and cover population growth or decay, interacting predator-prey populations, compound interest and inflation, radioactive decay, chemical and biochemical reaction, spreading of diseases, cooling of objects, compaction of geological media, pressure variations in the atmosphere, viscoelastic response in materials, and air resistance on falling or rising bodies.

Before we turn to the applications, however, we take aÂ brief look at the technique of scaling, which is so useful in many applications.

## 4.1 Scaling

Real applications of aÂ model $$u^{\prime}=-au+b$$ will often involve aÂ lot of parameters in the expressions for a and b. It can be quite aÂ challenge to find relevant values of all parameters. In simple problems, however, it turns out that it is not always necessary to estimate all parameters because we can lump them into one or aÂ few dimensionless numbers by using aÂ very attractive technique called scaling. It simply means to stretch the u and t axis in the present problem â€“ and suddenly all parameters in the problem are lumped into one parameter if $$b\neq 0$$ and no parameter when bâ€‰=â€‰0!

### 4.1.1 Dimensionless Variables

Scaling means that we introduce aÂ new function $$\bar{u}(\bar{t})$$, with

$$\bar{u}=\frac{u-u_{m}}{u_{c}},\quad\bar{t}=\frac{t}{t_{c}},$$

where um is aÂ characteristic value of u, uc is aÂ characteristic size of the range of u values, and tc is aÂ characteristic size of the range of t where u shows significant variation. Choosing um, uc, and tc is not always easy and is often an art in complicated problems. We just state one choice first:

$$u_{c}=I,\quad u_{m}=b/a,\quad t_{c}=1/a\thinspace.$$

Inserting $$u=u_{m}+u_{c}\bar{u}$$ and $$t=t_{c}\bar{t}$$ in the problem $$u^{\prime}=-au+b$$, assuming a and b are constants, results (after some algebra) in the scaled problem

$$\frac{d\bar{u}}{d\bar{t}}=-\bar{u},\quad\bar{u}(0)=1-\beta,$$

where

$$\beta=\frac{b}{Ia}\thinspace.$$

### 4.1.2 Dimensionless Numbers

The parameter Î² is aÂ dimensionless number. From the equation we see that b must have the same unit as the term au. The initial condition I must have the same unit as u, so Ia has the same unit as b, making the fraction $$b/(Ia)$$ dimensionless.

An important observation is that $$\bar{u}$$ depends on $$\bar{t}$$ and Î². That is, only the special combination of $$b/(Ia)$$ matters, not what the individual values of b, a, and I are. The original unscaled function u depends on t, b, a, and I.

AÂ second observation is striking: if bâ€‰=â€‰0, the scaled problem is independent of a and I! In practice this means that we can perform aÂ single numerical simulation of the scaled problem and recover the solution of any problem for aÂ given a and I by stretching the axis in the plot: $$u=I\bar{u}$$ and $$t=\bar{t}/a$$. For $$b\neq 0$$, we simulate the scaled problem for aÂ few Î² values and recover the physical solution u by translating and stretching the u axis and stretching the t axis.

In general, scaling combines the parameters in aÂ problem to aÂ set of dimensionless parameters. The number of dimensionless parameters is usually much smaller than the number of original parameters. SectionÂ 4.11 presents an example where 11 parameters are reduced to one!

### 4.1.3 AÂ Scaling for Vanishing Initial Condition

The scaling breaks down if Iâ€‰=â€‰0. In that case we may choose $$u_{m}=0$$, $$u_{c}=b/a$$, and $$t_{c}=1/b$$, resulting in aÂ slightly different scaled problem:

$$\frac{d\bar{u}}{d\bar{t}}=1-\bar{u},\quad\bar{u}(0)=0\thinspace.$$

As with bâ€‰=â€‰0, the case Iâ€‰=â€‰0 has aÂ scaled problem with no physical parameters!

It is common to drop the bars after scaling and write the scaled problem as $$u^{\prime}=-u$$, $$u(0)=1-\beta$$, or $$u^{\prime}=1-u$$, $$u(0)=0$$. Any implementation of the problem $$u^{\prime}=-au+b$$, $$u(0)=I$$, can be reused for the scaled problem by setting aâ€‰=â€‰1, bâ€‰=â€‰0, and $$I=1-\beta$$ in the code, if $$I\neq 0$$, or one sets aâ€‰=â€‰1, bâ€‰=â€‰1, and Iâ€‰=â€‰0 when the physical I is zero. Falling bodies in fluids, as described in Sect.Â 4.11, involves $$u^{\prime}=-au+b$$ with seven physical parameters. All these vanish in the scaled version of the problem if we start the motion from rest!

Many more details about scaling are presented in the authorâ€™s book Scaling of Differential Equations [9].

## 4.2 Evolution of aÂ Population

### 4.2.1 Exponential Growth

Let N be the number of individuals in aÂ population occupying some spatial domain. Despite N being an integer in this problem, we shall compute with N as aÂ real number and view $$N(t)$$ as aÂ continuous function of time. The basic model assumption is that in aÂ time interval $$\Delta t$$ the number of newcomers to the populations (newborns) is proportional to N, with proportionality constant $$\bar{b}$$. The amount of newcomers will increase the population and result in

$$N(t+\Delta t)=N(t)+\bar{b}N(t)\thinspace.$$

It is obvious that aÂ long time interval $$\Delta t$$ will result in more newcomers and hence aÂ larger $$\bar{b}$$. Therefore, we introduce $$b=\bar{b}/\Delta t$$: the number of newcomers per unit time and per individual. We must then multiply b by the length of the time interval considered and by the population size to get the total number of new individuals, $$b\Delta tN$$.

If the number of removals from the population (deaths) is also proportional to N, with proportionality constant $$d\Delta t$$, the population evolves according to

$$N(t+\Delta t)=N(t)+b\Delta tN(t)-d\Delta tN(t)\thinspace.$$

Dividing by $$\Delta t$$ and letting $$\Delta t\rightarrow 0$$, we get the ODE

$$N^{\prime}=(b-d)N,\quad N(0)=N_{0}\thinspace.$$
(4.1)

In aÂ population where the death rate (d) is larger than then newborn rate (b), $$b-d<0$$, and the population experiences exponential decay rather than exponential growth.

In some populations there is an immigration of individuals into the spatial domain. With I individuals coming in per time unit, the equation for the population change becomes

$$N(t+\Delta t)=N(t)+b\Delta tN(t)-d\Delta tN(t)+\Delta tI\thinspace.$$

$$N^{\prime}=(b-d)N+I,\quad N(0)=N_{0}\thinspace.$$
(4.2)

Emigration is also modeled by this I term if we just change its sign: Iâ€‰<â€‰0. So, the I term models migration in and out of the domain in general.

Some simplification arises if we introduce aÂ fractional measure of the population: $$u=N/N_{0}$$ and set $$r=b-d$$. The ODE problem now becomes

$$u^{\prime}=ru+f,\quad u(0)=1,$$
(4.3)

where $$f=I/N_{0}$$ measures the net immigration per time unit as the fraction of the initial population. Very often, r is approximately constant, but f is usually aÂ function of time.

### 4.2.2 Logistic Growth

The growth rate r of aÂ population decreases if the environment has limited resources. Suppose the environment can sustain at most $$N_{\max}$$ individuals. We may then assume that the growth rate approaches zero as N approaches $$N_{\max}$$, i.e., as u approaches $$M=N_{\max}/N_{0}$$. The simplest possible evolution of r is then aÂ linear function: $$r(t)={\varrho}(1-u(t)/M)$$, where $$\varrho$$ is the initial growth rate when the population is small relative to the maximum size and there is enough resources. Using this $$r(t)$$ in (Ch4.E3) results in the logistic model for the evolution of aÂ population (assuming for the moment that fâ€‰=â€‰0):

$$u^{\prime}={\varrho}(1-u/M)u,\quad u(0)=1\thinspace.$$
(4.4)

Initially, u will grow at rate $$\varrho$$, but the growth will decay as u approaches M, and then there is no more change in u, causing $$u\rightarrow M$$ as $$t\rightarrow\infty$$. Note that the logistic equation $$u^{\prime}={\varrho}(1-u/M)u$$ is nonlinear because of the quadratic term $$-u^{2}{\varrho}/M$$.

## 4.3 Compound Interest and Inflation

Say the annual interest rate is r percent and that the bank adds the interest once aÂ year to your investment. If un is the investment in year n, the investment in year $$u^{n+1}$$ grows to

$$u^{n+1}=u^{n}+\frac{r}{100}u^{n}\thinspace.$$

In reality, the interest rate is added every day. We therefore introduce aÂ parameter m for the number of periods per year when the interest is added. If n counts the periods, we have the fundamental model for compound interest:

$$u^{n+1}=u^{n}+\frac{r}{100m}u^{n}\thinspace.$$
(4.5)

This model is aÂ difference equation, but it can be transformed to aÂ continuous differential equation through aÂ limit process. The first step is to derive aÂ formula for the growth of the investment over aÂ time t. Starting with an investment u0, and assuming that r is constant in time, we get

\begin{aligned}\displaystyle u^{n+1}&\displaystyle=\left(1+\frac{r}{100m}\right)u^{n}\\ \displaystyle&\displaystyle=\left(1+\frac{r}{100m}\right)^{2}u^{n-1}\\ \displaystyle&\displaystyle\ \ \vdots\\ \displaystyle&\displaystyle=\left(1+\frac{r}{100m}\right)^{n+1}u^{0}\end{aligned}

Introducing time t, which here is aÂ real-numbered counter for years, we have that $$n=mt$$, so we can write

$$u^{mt}=\left(1+\frac{r}{100m}\right)^{mt}u^{0}\thinspace.$$

The second step is to assume continuous compounding, meaning that the interest is added continuously. This implies $$m\rightarrow\infty$$, and in the limit one gets the formula

$$u(t)=u_{0}e^{rt/100},$$
(4.6)

which is nothing but the solution of the ODE problem

$$u^{\prime}=\frac{r}{100}u,\quad u(0)=u_{0}\thinspace.$$
(4.7)

This is then taken as the ODE model for compound interest if râ€‰>â€‰0. However, the reasoning applies equally well to inflation, which is just the case râ€‰<â€‰0. One may also take the r in (Ch4.E7) as the net growth of an investment, where r takes both compound interest and inflation into account. Note that for real applications we must use aÂ time-dependent r in (Ch4.E7).

Introducing $$a=\frac{r}{100}$$, continuous inflation of an initial fortune I is then aÂ process exhibiting exponential decay according to

$$u^{\prime}=-au,\quad u(0)=I\thinspace.$$

## 4.4 Newtonâ€™s Law of Cooling

When aÂ body at some temperature is placed in aÂ cooling environment, experience shows that the temperature falls rapidly in the beginning, and then the change in temperature levels off until the bodyâ€™s temperature equals that of the surroundings. Newton carried out some experiments on cooling hot iron and found that the temperature evolved as a â€˜â€˜geometric progression at times in arithmetic progressionâ€™â€™, meaning that the temperature decayed exponentially. Later, this result was formulated as aÂ differential equation: the rate of change of the temperature in aÂ body is proportional to the temperature difference between the body and its surroundings. This statement is known as Newtonâ€™s law of cooling, which mathematically can be expressed as

$$\frac{dT}{dt}=-k(T-T_{s}),$$
(4.8)

where T is the temperature of the body, Ts is the temperature of the surroundings (which may be time-dependent), t is time, and k is aÂ positive constant. Equation (Ch4.E8) is primarily viewed as an empirical law, valid when heat is efficiently convected away from the surface of the body by aÂ flowing fluid such as air at constant temperature Ts. The heat transfer coefficient k reflects the transfer of heat from the body to the surroundings and must be determined from physical experiments.

The cooling law (Ch4.E8) needs an initial condition $$T(0)=T_{0}$$.

An atomic nucleus of an unstable atom may lose energy by emitting ionizing particles and thereby be transformed to aÂ nucleus with aÂ different number of protons and neutrons. This process is known as radioactive decay. Actually, the process is stochastic when viewed for aÂ single atom, because it is impossible to predict exactly when aÂ particular atom emits aÂ particle. Nevertheless, with aÂ large number of atoms, N, one may view the process as deterministic and compute the mean behavior of the decay. Below we reason intuitively about an ODE for the mean behavior. Thereafter, we show mathematically that aÂ detailed stochastic model for single atoms leads to the same mean behavior.

### 4.5.1 Deterministic Model

Suppose at time t, the number of the original atom type is $$N(t)$$. AÂ basic model assumption is that the transformation of the atoms of the original type in aÂ small time interval $$\Delta t$$ is proportional to N, so that

$$N(t+\Delta t)=N(t)-a\Delta tN(t),$$

where aâ€‰>â€‰0 is aÂ constant. The proportionality factor is $$a\Delta t$$, i.e., proportional to $$\Delta t$$ since aÂ longer time interval will produce more transformations. We can introduce $$u=N(t)/N(0)$$, divide by $$\Delta t$$, and let $$\Delta t\rightarrow 0$$:

$$\lim_{r\rightarrow 0}N_{0}\frac{u(t+\Delta t)-u(t)}{\Delta t}=-aN_{0}u(t)\thinspace.$$

The left-hand side is the derivative of u. Dividing by the N0 gives the following ODE for u:

$$u^{\prime}=-au,\quad u(0)=1\thinspace.$$
(4.9)

The parameter a can for aÂ given nucleus be expressed through the half-life $$t_{1/2}$$, which is the time taken for the decay to reduce the initial amount by one half, i.e., $$u(t_{1/2})=0.5$$. With $$u(t)=e^{-at}$$, we get $$t_{1/2}=a^{-1}\ln 2$$ or $$a=\ln 2/t_{1/2}$$.

### 4.5.2 Stochastic Model

Originally, we have N0 atoms. Up to some particular time t, each atom may either have decayed or not. If not, they have â€˜â€˜survivedâ€™â€™. We want to count how many original atoms that have survived. The survival of aÂ single atom at time t is aÂ random event. Since there are only two outcomes, survival or decay, we have aÂ Bernoulli trial. Let p be the probability of survival (implying that the probability of decay is $$1-p$$). If each atom survives independently of the others, and the probability of survival is the same for every atom, we have N0 Bernoulli trials, known as aÂ binomial experiment from probability theory. The probability $$P(N)$$ that N out of the N0 atoms have survived at time t is then given by the famous binomial distribution

$$P(N)=\frac{N_{0}!}{N!(N_{0}-N)!}p^{N}(1-p)^{N_{0}-N}\thinspace.$$

The mean (or expected) value $$\hbox{E}[P]$$ of $$P(N)$$ is known to be $$N_{0}p$$.

It remains to estimate p. Let the interval $$[0,t]$$ be divided into m small subintervals of length $$\Delta t$$. We make the assumption that the probability of decay of aÂ single atom in an interval of length $$\Delta t$$ is $$\tilde{p}$$, and that this probability is proportional to $$\Delta t$$: $$\tilde{p}=\lambda\Delta t$$ (it sounds natural that the probability of decay increases with $$\Delta t$$). The corresponding probability of survival is $$1-\lambda\Delta t$$. Believing that Î» is independent of time, we have, for each interval of length $$\Delta t$$, aÂ Bernoulli trial: the atom either survives or decays in that interval. Now, p should be the probability that the atom survives in all the intervals, i.e., that we have m successful Bernoulli trials in aÂ row and therefore

$$p=(1-\lambda\Delta t)^{m}\thinspace.$$

The expected number of atoms of the original type at time t is

$$\hbox{E}[P]=N_{0}p=N_{0}(1-\lambda\Delta t)^{m},\quad m=t/\Delta t\thinspace.$$
(4.10)

To see the relation between the two types of Bernoulli trials and the ODE above, we go to the limit $$\Delta t\rightarrow 0$$, $$m\rightarrow\infty$$. It is possible to show that

$$p=\lim_{m\rightarrow\infty}(1-\lambda\Delta t)^{m}=\lim_{m\rightarrow\infty}\left(1-\lambda\frac{t}{m}\right)^{m}=e^{-\lambda t}$$

This is the famous exponential waiting time (or arrival time) distribution for aÂ Poisson process in probability theory (obtained here, as often done, as the limit of aÂ binomial experiment). The probability of decay, or more precisely that at least one atom has undergone aÂ transition, is $$1-p=1-e^{-\lambda t}$$. This is the exponential distribution. The limit means that m is very large, hence $$\Delta t$$ is very small, and $$\tilde{p}=\lambda\Delta t$$ is very small since the intensity of the events, Î», is assumed finite. This situation corresponds to aÂ very small probability that an atom will decay in aÂ very short time interval, which is aÂ reasonable model. The same model occurs in lots of different applications, e.g., when waiting for aÂ taxi, or when finding defects along aÂ rope.

### 4.5.3 Relation Between Stochastic and Deterministic Models

With $$p=e^{-\lambda t}$$ we get the expected number of original atoms at t as $$N_{0}p=N_{0}e^{-\lambda t}$$, which is exactly the solution of the ODE model $$N^{\prime}=-\lambda N$$. This also gives an interpretation of a via Î» or vice versa. Our important finding here is that the ODE model captures the mean behavior of the underlying stochastic model. This is, however, not always the common relation between microscopic stochastic models and macroscopic â€˜â€˜averagedâ€™â€™ models.

Also of interest, is that aÂ Forward Euler discretization of $$N^{\prime}=-\lambda N$$, $$N(0)=N_{0}$$, gives $$N^{m}=N_{0}(1-\lambda\Delta t)^{m}$$ at time $$t_{m}=m\Delta t$$, which is exactly the expected value of the stochastic experiment with N0 atoms and m small intervals of length $$\Delta t$$, where each atom can decay with probability $$\lambda\Delta t$$ in an interval.

AÂ fundamental question is how accurate the ODE model is. The underlying stochastic model fluctuates around its expected value. AÂ measure of the fluctuations is the standard deviation of the binomial experiment with N0 atoms, which can be shown to be $$\hbox{Std}[P]=\sqrt{N_{0}p(1-p)}$$. Compared to the size of the expectation, we get the normalized standard deviation

$$\frac{\sqrt{\hbox{Var}(P)}}{\hbox{E}[P]}=N_{0}^{-1/2}\sqrt{p^{-1}-1}=N_{0}^{-1/2}\sqrt{(1-e^{-\lambda t})^{-1}-1}\approx(N_{0}\lambda t)^{-1/2},$$

showing that the normalized fluctuations are very small if N0 is very large, which is usually the case.

### 4.5.4 Generalization of the Radioactive Decay Modeling

The modeling in Sect.Â 4.5 is in fact very general, despite aÂ focus on aÂ particular physical process. We may instead of atoms and decay speak about aÂ set of items, where each item can undergo aÂ stochastic transition from one state to another. In Sect.Â 4.6 the item is aÂ molecule and the transition is aÂ chemical reaction, while in Sect.Â 4.7 the item is an ill person and the transition is recovering from the illness (or an immune person who loses her immunity).

From the modeling in Sect.Â 4.5 we can establish aÂ deterministic model for aÂ large number of items and aÂ stochastic model for an arbitrary number of items, even aÂ single one. The stochastic model has aÂ parameter Î» reflecting the probability that aÂ transition takes place in aÂ time interval of unit length (or equivalently, that the probability is $$\lambda\Delta t$$ for aÂ transition during aÂ time interval of length $$\Delta t$$). The probability of making aÂ transition before time t is given by

$$F(t)=1-e^{-\lambda t}\thinspace.$$

The corresponding probability density is $$f(t)=F^{\prime}(t)=e^{-\lambda t}$$. The expected value of $$F(t)$$, i.e., the expected time to transition, is $$\lambda^{-1}$$. This interpretation of Î» makes it easy to measure its value: just carry out aÂ large number of experiments, measure the time to transition, and take one over the average of these times as an estimate of Î». The variance is $$\lambda^{-2}$$.

The deterministic model counts how many items, $$N(t)$$, that have undergone the transition (on average), and $$N(t)$$ is governed by the ODE

$$N^{\prime}=-\lambda N(t),\quad N(0)=N_{0}\thinspace.$$

## 4.6 Chemical Kinetics

### 4.6.1 Irreversible Reaction of Two Substances

Consider two chemical substances, A and B, and aÂ chemical reaction that turns A into B. In aÂ small time interval, some of the molecules of type A are transformed into molecules of B. This process is, from aÂ mathematical modeling point of view, equivalent to the radioactive decay process described in the previous section. We can therefore apply the same modeling approach. If NA is the number of molecules of substance A, we have that NA is governed by the differential equation

$$\frac{dN_{A}}{dt}=-kN_{A},$$

where (the constant) k is called the rate constant of the reaction. Rather than using the number of molecules, we use the concentration of molecules: $$[A](t)=N_{A}(t)/N_{A}(0)$$. We see that $$d[A]/dt=N_{A}(0)^{-1}dN_{A}/dt$$. Replacing NA by $$[A]$$ in the equation for NA leads to the equation for the concentration $$[A]$$:

$$\frac{d[A]}{dt}=-k[A],\quad t\in(0,T],\ [A](0)=1\thinspace.$$
(4.11)

Since substance A is transformed to substance B, we have that the concentration of $$[B]$$ grows by the loss of $$[A]$$:

$$\frac{d[B]}{dt}=k[A],\quad[B](0)=0\thinspace.$$

The mathematical model can either be (Ch4.E11) or the system

$$\frac{d[A]}{dt} =-k[A], t\in(0,T]$$
(4.12)
$$\frac{d[B]}{dt} =k[A], t\in(0,T]$$
(4.13)
$$(0) =1,$$
(4.14)
$$(0) =0\thinspace.$$
(4.15)

This reaction is known as aÂ first-order reaction, where each molecule of A makes an independent decision about whether to complete the reaction, i.e., independent of what happens to any other molecule.

An n-th order reaction is modeled by

$$\frac{d[A]}{dt} =-k[A]^{n},$$
(4.16)
$$\frac{d[B]}{dt} =k[A]^{n},$$
(4.17)

for $$t\in(0,T]$$ with initial conditions $$[A](0)=1$$ and $$[B](0)=0$$. Here, n can be aÂ real number, but is most often an integer. Note that the sum of the concentrations is constant since

$$\frac{d[A]}{dt}+\frac{d[B]}{dt}=0\quad\Rightarrow\quad[A](t)+[B](t)=\text{const}=[A](0)+[B](0)=1+0\thinspace.$$

### 4.6.2 Reversible Reaction of Two Substances

Let the chemical reaction turn substance A into B and substance B into A. The rate of change of $$[A]$$ has then two contributions: aÂ loss $$k_{A}[A]$$ and aÂ gain $$k_{B}[B]$$:

$$\frac{d[A]}{dt}=-k_{A}[A]+k_{B}[B],\quad t\in(0,T],\ [A](0)=A_{0}\thinspace.$$
(4.18)

Similarly for substance B,

$$\frac{d[B]}{dt}=k_{A}[A]-k_{B}[B],\quad t\in(0,T],\ [B](0)=B_{0}\thinspace.$$
(4.19)

This time we have allowed for arbitrary initial concentrations. Again,

$$\frac{d[A]}{dt}+\frac{d[B]}{dt}=0\quad\Rightarrow\quad[A](t)+[B](t)=A_{0}+B_{0}\thinspace.$$

### 4.6.3 Irreversible Reaction of Two Substances into aÂ Third

Now we consider two chemical substances, A and B, reacting with each other and producing aÂ substance C. In aÂ small time interval $$\Delta t$$, molecules of type A and B are occasionally colliding, and in some of the collisions, aÂ chemical reaction occurs, which turns A and B into aÂ molecule of type C. (More generally, MA molecules of A and MB molecules of B react to form MC molecules of C.) The number of possible pairings, and thereby collisions, of A and B is $$N_{A}N_{B}$$, where NA is the number of molecules of A, and NB is the number of molecules of B. AÂ fraction k of these collisions, $$\hat{k}\Delta tN_{A}N_{B}$$, features aÂ chemical reaction and produce NC molecules of C. The fraction is thought to be proportional to $$\Delta t$$: considering aÂ twice as long time interval, twice as many molecules collide, and twice as many reactions occur. The increase in molecules of substance C is now found from the reasoning

$$N_{C}(t+\Delta t)=N_{C}(t)+\hat{k}\Delta tN_{A}N_{B}\thinspace.$$

Dividing by $$\Delta t$$,

$$\frac{N_{C}(t+\Delta t)-N_{C}(t)}{\Delta t}=\hat{k}N_{A}N_{B},$$

and letting $$\Delta t\rightarrow 0$$, gives the differential equation

$$\frac{dN_{C}}{dt}=\hat{k}N_{A}N_{B}\thinspace.$$

(This equation is known as the important law of mass action discovered by the Norwegian scientists Cato M.Â Guldberg and Peter Waage. AÂ more general form of the right-hand side is $$\hat{k}N_{A}^{\alpha}N_{B}^{\beta}$$. All the constants $$\hat{k}$$, Î±, and Î² must be determined from experiments.)

Working instead with concentrations, we introduce $$[C](t)=N_{C}(t)/N_{C}(0)$$, with similar definitions for $$[A]$$ and $$[B]$$ we get

$$\frac{d[C]}{dt}=k[A][B]\thinspace.$$
(4.20)

The constant k is related to $$\hat{k}$$ by $$k=\hat{k}N_{A}(0)N_{B}(0)/N_{C}(0)$$. The gain in C is aÂ loss of A and B:

$$\frac{d[A]}{dt} =-k[A][B],$$
(4.21)
$$\frac{d[B]}{dt} =-k[A][B]\thinspace.$$
(4.22)

### 4.6.4 AÂ Biochemical Reaction

AÂ common reaction (known as Michaelisâ€“Menten kinetics) turns aÂ substrate S into aÂ product P with aid of an enzyme E. The reaction is aÂ two-stage process: first S and E reacts to form aÂ complex ES, where the enzyme and substrate are bound to each other, and then ES is turned into E and P. In the first stage, S and E react to produce aÂ growth of ES according to the law of mass action:

\begin{aligned}\displaystyle\frac{d[S]}{dt}&\displaystyle=-k_{+}[E][S],\\ \displaystyle\frac{d[ES]}{dt}&\displaystyle=k_{+}[E][S]\thinspace.\end{aligned}

The complex ES reacts and produces the product P at rate $$-k_{v}[ES]$$ and E at rate $$-k_{-}[ES]$$. The total set of reactions can then be expressed by

$$\frac{d[ES]}{dt} =k_{+}[E][S]-k_{v}[ES]-k_{-}[ES],$$
(4.23)
$$\frac{d[P]}{dt} =k_{v}[ES],$$
(4.24)
$$\frac{d[S]}{dt} =-k_{+}[E][S]+k_{-}[ES],$$
(4.25)
$$\frac{d[E]}{dt} =-k_{+}[E][S]+k_{-}[ES]+k_{v}[ES]\thinspace.$$
(4.26)

The initial conditions are $$[ES](0)=[P](0)=0$$, and $$[S]=S_{0}$$, $$[E]=E_{0}$$. The constants $$k_{+}$$, $$k_{-}$$, and kv must be determined from experiments.

The modeling of spreading of diseases is very similar to the modeling of chemical reactions in Sect.Â 4.6. The field of epidemiology speaks about susceptibles: people who can get aÂ disease; infectives: people who are infected and can infect susceptibles; and recovered: people who have recovered from the disease and become immune. Three categories are accordingly defined: S for susceptibles, I for infectives, and R for recovered. The number in each category is tracked by the functions $$S(t)$$, $$I(t)$$, and $$R(t)$$.

To model how many people that get infected in aÂ small time interval $$\Delta t$$, we reason as with reactions in Sect.Â 4.6. The possible number of pairings (â€˜â€˜collisionsâ€™â€™) between susceptibles and infected is SI. AÂ fraction of these, $$\beta\Delta tSI$$, will actually meet and the infected succeed in infecting the susceptible, where Î² is aÂ parameter to be empirically estimated. This leads to aÂ loss of susceptibles and aÂ gain of infected:

\begin{aligned}\displaystyle S(t+\Delta t)&\displaystyle=S(t)-\beta\Delta tSI,\\ \displaystyle I(t+\Delta t)&\displaystyle=I(t)+\beta\Delta tSI\thinspace.\end{aligned}

In the same time interval, aÂ fraction $$\nu\Delta tI$$ of the infected is recovered. It follows from Sect.Â 4.5.4 that the parameter $$\nu^{-1}$$ is interpreted as the average waiting time to leave the I category, i.e., the average length of the disease. The $$\nu\Delta tI$$ term is aÂ loss for the I category, but aÂ gain for the R category:

$$I(t+\Delta t)=I(t)+\beta\Delta tSI-\nu\Delta tI,\quad R(t+\Delta t)=R(t)+\nu\Delta tI\thinspace.$$

Dividing these equations by $$\Delta t$$ and going to the limit $$\Delta t\rightarrow 0$$, gives the ODE system

$$\frac{dS}{dt} =-\beta SI,$$
(4.27)
$$\frac{dI}{dt} =\beta SI-\nu I,$$
(4.28)
$$\frac{dR}{dt} =\nu I,$$
(4.29)

with initial values $$S(0)=S_{0}$$, $$I(0)=I_{0}$$, and $$R(0)=0$$. By adding the equations, we realize that

$$\frac{dS}{dt}+\frac{dI}{dt}+\frac{dR}{dt}=0\quad\Rightarrow\quad S+I+R=\text{const}=N,$$

where N is the total number in the population under consideration. This property can be used as aÂ partial verification during simulations.

Equations (Ch4.E27)â€“(Ch4.E29) are known as the SIR model in epidemiology. The model can easily be extended to incorporate vaccination programs, immunity loss after some time, etc. Typical diseases that can be simulated by the SIR model and its variants are measles, smallpox, flu, plague, and HIV.

## 4.8 Predator-Prey Models in Ecology

AÂ model for the interaction of predator and prey species can be based on reasoning from population dynamics and the SIR model. Let $$H(t)$$ be the number of preys in aÂ region, and let $$L(t)$$ be the number of predators. For example, H may be hares and L lynx, or rabbits and foxes.

The population of the prey evolves due to births and deaths, exactly as in aÂ population dynamics model from Sect.Â 4.2.1. During aÂ time interval $$\Delta t$$ the increase in the population is therefore

$$H(t+\Delta t)-H(t)=a\Delta tH(t),$$

where a is aÂ parameter to be measured from data. The increase is proportional to H, and the proportionality constant $$a\Delta t$$ is proportional to $$\Delta t$$, because doubling the interval will double the increase.

However, the prey population has an additional loss because they are eaten by predators. All the prey and predator animals can form LH pairs in total (assuming all individuals meet randomly). AÂ small fraction $$b\Delta t$$ of such meetings, during aÂ time interval $$\Delta t$$, ends up with the predator eating the prey. The increase in the prey population is therefore adjusted to

$$H(t+\Delta t)-H(t)=a\Delta tH(t)-b\Delta tH(t)L(t)\thinspace.$$

The predator population increases as aÂ result of eating preys. The amount of eaten preys is $$b\Delta tLH$$, but only aÂ fraction $$d\Delta tLH$$ of this amount contributes to increasing the predator population. In addition, predators die and this loss is set to $$c\Delta tL$$. To summarize, the increase in the predator population is given by

$$L(t+\Delta t)-L(t)=d\Delta tL(t)H(t)-c\Delta tL(t)\thinspace.$$

Dividing by $$\Delta t$$ in the equations for H and L and letting $$t\rightarrow 0$$ results in

\begin{aligned}\displaystyle\lim_{\Delta t\rightarrow 0}\frac{H(t+\Delta t)-H(t)}{\Delta t}=H^{\prime}(t)&\displaystyle=aH(t)-bL(t)H(t),\\ \displaystyle\lim_{\Delta t\rightarrow 0}\frac{L(t+\Delta t)-L(t)}{\Delta t}=L^{\prime}(t)&\displaystyle=dL(t)H(t)-cL(t)\thinspace.\end{aligned}

We can simplify the notation to the following two ODEs:

$$H^{\prime} =H(a-bL),$$
(4.30)
$$L^{\prime} =L(dH-c)\thinspace.$$
(4.31)

This is aÂ so-called Lokta-Volterra model. It contains four parameters that must be estimated from data: a, b, c, and d. In addition, two initial conditions are needed for $$H(0)$$ and $$L(0)$$.

## 4.9 Decay of Atmospheric Pressure with Altitude

### 4.9.1 The General Model

Vertical equilibrium of air in the atmosphere is governed by the equation

$$\frac{dp}{dz}=-\varrho g\thinspace.$$
(4.32)

Here, $$p(z)$$ is the air pressure, $$\varrho$$ is the density of air, and $$g=9.807\,\mathrm{m/s}^{2}$$ is aÂ standard value of the acceleration of gravity. (Equation (Ch4.E32) follows directly from the general Navier-Stokes equations for fluid motion, with the assumption that the air does not move.)

The pressure is related to density and temperature through the ideal gas law

$$\varrho=\frac{Mp}{R^{*}T},$$
(4.33)

where M is the molar mass of the Earthâ€™s air (0.029â€¯kg$$/$$mol), $$R^{*}$$ is the universal gas constant (8.314 Nm/(mol K)), and T is the temperature in Kelvin. All variables p, $$\varrho$$, and T vary with the height z. Inserting (Ch4.E33) in (Ch4.E32) results in an ODE with aÂ variable coefficient:

$$\frac{dp}{dz}=-\frac{Mg}{R^{*}T(z)}p\thinspace.$$
(4.34)

### 4.9.2 Multiple Atmospheric Layers

The atmosphere can be approximately modeled by seven layers. In each layer, (Ch4.E34) is applied with aÂ linear temperature of the form

$$T(z)=\bar{T}_{i}+L_{i}(z-h_{i}),$$

where $$z=h_{i}$$ denotes the bottom of layer number i, having temperature $$\bar{T}_{i}$$, and Li is aÂ constant in layer number i. The table below lists hi (m), $$\bar{T}_{i}$$ (K), and Li (K/m) for the layers $$i=0,\ldots,6$$.

For implementation it might be convenient to write (Ch4.E34) on the form

$$\frac{dp}{dz}=-\frac{Mg}{R^{*}(\bar{T}(z)+L(z)(z-h(z)))}p,$$
(4.35)

where $$\bar{T}(z)$$, $$L(z)$$, and $$h(z)$$ are piecewise constant functions with values given in the table. The value of the pressure at the sea level zâ€‰=â€‰0, $$p_{0}=p(0)$$, is 101,325â€¯Pa.

### Constant layer temperature

One common simplification is to assume that the temperature is constant within each layer. This means that Lâ€‰=â€‰0.

### One-layer model

Another commonly used approximation is to work with one layer instead of seven. This one-layer model is based on $$T(z)=T_{0}-Lz$$, with sea level standard temperature $$T_{0}=288$$â€¯K and temperature lapse rate Lâ€‰=â€‰0.0065â€¯K$$/$$m.

## 4.10 Compaction of Sediments

Sediments, originally made from materials like sand and mud, get compacted through geological time by the weight of new material that is deposited on the sea bottom. The porosity Ï• of the sediments tells how much void (fluid) space there is between the sand and mud grains. The porosity drops with depth, due to the weight of the sediments above. This makes the void space shrink, and thereby compaction increases.

AÂ typical assumption is that the change in Ï• at some depth z is negatively proportional to Ï•. This assumption leads to the differential equation problem

$$\frac{d\phi}{dz}=-c\phi,\quad\phi(0)=\phi_{0},$$
(4.36)

where the z axis points downwards, zâ€‰=â€‰0 is the surface with known porosity, and câ€‰>â€‰0 is aÂ constant.

The upper part of the Earthâ€™s crust consists of many geological layers stacked on top of each other, as indicated in Fig.Â 4.1. The model (Ch4.E36) can be applied for each layer. In layer number i, we have the unknown porosity function $$\phi_{i}(z)$$ fulfilling $$\phi_{i}^{\prime}(z)=-c_{i}z$$, since the constant c in the model (Ch4.E36) depends on the type of sediment in the layer. Alternatively, we can use (Ch4.E36) to describe the porosity through all layers if c is taken as aÂ piecewise constant function of z, equal to ci in layer i. From the figure we see that new layers of sediments are deposited on top of older ones as time progresses. The compaction, as measured by Ï•, is rapid in the beginning and then decreases (exponentially) with depth in each layer.

When we drill aÂ well at present time through the right-most column of sediments in Fig.Â 4.1, we can measure the thickness of the sediment in (say) the bottom layer. Let L1 be this thickness. Assuming that the volume of sediment remains constant through time, we have that the initial volume, $$\int_{0}^{L_{1,0}}\phi_{1}dz$$, must equal the volume seen today, $$\int_{\ell-L_{1}}^{\ell}\phi_{1}dz$$, where $$\ell$$ is the depth of the bottom of the sediment in the present day configuration. After having solved for $$\phi_{1}$$ as aÂ function of z, we can then find the original thickness $$L_{1,0}$$ of the sediment from the equation

$$\int_{0}^{L_{1,0}}\phi_{1}dz=\int_{\ell-L_{1}}^{\ell}\phi_{1}dz\thinspace.$$

In hydrocarbon exploration it is important to know $$L_{1,0}$$ and the compaction history of the various layers of sediments.

## 4.11 Vertical Motion of aÂ Body in aÂ Viscous Fluid

AÂ body moving vertically through aÂ fluid (liquid or gas) is subject to three different types of forces: the gravity force, the drag force, and the buoyancy force.

### 4.11.1 Overview of Forces

Taking the upward direction as positive, the gravity force is $$F_{g}=-mg$$, where m is the mass of the body and g is the acceleration of gravity. The uplift or buoyancy force (â€˜â€˜Archimedes forceâ€™â€™) is $$F_{b}=\varrho gV$$, where $$\varrho$$ is the density of the fluid and V is the volume of the body.

The drag force is of two types, depending on the Reynolds number

$$\text{Re}=\frac{\varrho d|v|}{\mu},$$
(4.37)

where d is the diameter of the body in the direction perpendicular to the flow, v is the velocity of the body, and Î¼ is the dynamic viscosity of the fluid. When $$\text{Re}<1$$, the drag force is fairly well modeled by the so-called Stokesâ€™ drag, which for aÂ spherical body of diameter d reads

$$F_{d}^{(S)}=-3\pi d\mu v\thinspace.$$
(4.38)

Quantities are taken as positive in the upwards vertical direction, so if vâ€‰>â€‰0 and the body moves upwards, the drag force acts downwards and become negative, in accordance with the minus sign in expression for $$F_{d}^{(S)}$$.

For large Re, typically $$\text{Re}> 10^{3}$$, the drag force is quadratic in the velocity:

$$F_{d}^{(q)}=-\frac{1}{2}C_{D}\varrho A|v|v,$$
(4.39)

where CD is aÂ dimensionless drag coefficient depending on the bodyâ€™s shape, and A is the cross-sectional area as produced by aÂ cut plane, perpendicular to the motion, through the thickest part of the body. The superscripts $$\,{}^{q}$$ and $$\,{}^{S}$$ in $$F_{d}^{(S)}$$ and $$F_{d}^{(q)}$$ indicate Stokesâ€™ drag and quadratic drag, respectively.

### 4.11.2 Equation of Motion

All the mentioned forces act in the vertical direction. Newtonâ€™s second law of motion applied to the body says that the sum of these forces must equal the mass of the body times its acceleration a in the vertical direction.

$$ma=F_{g}+F_{d}^{(S)}+F_{b}\thinspace.$$

Here we have chosen to model the fluid resistance by the Stokesâ€™ drag. Inserting the expressions for the forces yields

$$ma=-mg-3\pi d\mu v+\varrho gV\thinspace.$$

The unknowns here are v and a, i.e., we have two unknowns but only one equation. From kinematics in physics we know that the acceleration is the time derivative of the velocity: $$a=dv/dt$$. This is our second equation. We can easily eliminate a and get aÂ single differential equation for v:

$$m\frac{dv}{dt}=-mg-3\pi d\mu v+\varrho gV\thinspace.$$

AÂ small rewrite of this equation is handy: We express m as $$\varrho_{b}V$$, where $$\varrho_{b}$$ is the density of the body, and we divide by the mass to get

$$v^{\prime}(t)=-\frac{3\pi d\mu}{\varrho_{b}V}v+g\left(\frac{\varrho}{\varrho_{b}}-1\right)\thinspace.$$
(4.40)

We may introduce the constants

$$a=\frac{3\pi d\mu}{\varrho_{b}V},\quad b=g\left(\frac{\varrho}{\varrho_{b}}-1\right),$$
(4.41)

so that the structure of the differential equation becomes obvious:

$$v^{\prime}(t)=-av(t)+b\thinspace.$$
(4.42)

The corresponding initial condition is $$v(0)=v_{0}$$ for some prescribed starting velocity v0.

This derivation can be repeated with the quadratic drag force $$F_{d}^{(q)}$$, leading to the result

$$v^{\prime}(t)=-\frac{1}{2}C_{D}\frac{\varrho A}{\varrho_{b}V}|v|v+g\left(\frac{\varrho}{\varrho_{b}}-1\right)\thinspace.$$
(4.43)

Defining

$$a=\frac{1}{2}C_{D}\frac{\varrho A}{\varrho_{b}V},$$
(4.44)

and b as above, we can write (Ch4.E43) as

$$v^{\prime}(t)=-a|v|v+b\thinspace.$$
(4.45)

### 4.11.3 Terminal Velocity

An interesting aspect of (Ch4.E42) and (Ch4.E45) is whether v will approach aÂ final constant value, the so-called terminal velocity vT, as $$t\rightarrow\infty$$. AÂ constant v means that $$v^{\prime}(t)\rightarrow 0$$ as $$t\rightarrow\infty$$ and therefore the terminal velocity vT solves

$$0=-av_{T}+b$$

and

$$0=-a|v_{T}|v_{T}+b\thinspace.$$

The former equation implies $$v_{T}=b/a$$, while the latter has solutions $$v_{T}=-\sqrt{|b|/a}$$ for aÂ falling body ($$v_{T}<0$$) and $$v_{T}=\sqrt{b/a}$$ for aÂ rising body ($$v_{T}> 0$$).

### 4.11.4 AÂ Crankâ€“Nicolson Scheme

Both governing equations, the Stokesâ€™ drag model (Ch4.E42) and the quadratic drag model (Ch4.E45), can be readily solved by the Forward Euler scheme. For higher accuracy one can use the Crankâ€“Nicolson method, but aÂ straightforward application of this method gives aÂ nonlinear equation in the new unknown value $$v^{n+1}$$ when applied to (Ch4.E45):

$$\frac{v^{n+1}-v^{n}}{\Delta t}=-a\frac{1}{2}(|v^{n+1}|v^{n+1}+|v^{n}|v^{n})+b\thinspace.$$
(4.46)

The first term on the right-hand side of (Ch4.E46) is the arithmetic average of $$-|v|v$$ evaluated at time levels n and $$n+1$$.

Instead of approximating the term $$-|v|v$$ by an arithmetic average, we can use aÂ geometric mean:

$$(|v|v)^{n+\frac{1}{2}}\approx|v^{n}|v^{n+1}\thinspace.$$
(4.47)

The error is of second order in $$\Delta t$$, just as for the arithmetic average and the centered finite difference approximation in (Ch4.E46). With the geometric mean, the resulting discrete equation

$$\frac{v^{n+1}-v^{n}}{\Delta t}=-a|v^{n}|v^{n+1}+b$$

becomes aÂ linear equation in $$v^{n+1}$$, and we can therefore easily solve for $$v^{n+1}$$:

$$v^{n+1}=\frac{v_{n}+\Delta tb^{n+\frac{1}{2}}}{1+\Delta ta^{n+\frac{1}{2}}|v^{n}|}\thinspace.$$
(4.48)

Using aÂ geometric mean instead of an arithmetic mean in the Crankâ€“Nicolson scheme is an attractive method for avoiding aÂ nonlinear algebraic equation when discretizing aÂ nonlinear ODE.

### 4.11.5 Physical Data

Suitable values of Î¼ are $$1.8\cdot 10^{-5}\,\text{Pa}\,\text{s}$$ for air and $$8.9\cdot 10^{-4}\,\text{Pa}\,\text{s}$$ for water. Densities can be taken as $$1.2\,\mathrm{kg/m}^{3}$$ for air and as $$1.0\cdot 10^{3}\,\mathrm{kg/m}^{3}$$ for water. For considerable vertical displacement in the atmosphere one should take into account that the density of air varies with the altitude, see Sect.Â 4.9. One possible density variation arises from the one-layer model in the mentioned section.

Any density variation makes b time dependent and we need $$b^{n+\frac{1}{2}}$$ in (Ch4.E48). To compute the density that enters $$b^{n+\frac{1}{2}}$$ we must also compute the vertical position $$z(t)$$ of the body. Since $$v=dz/dt$$, we can use aÂ centered difference approximation:

$$\frac{z^{n+\frac{1}{2}}-z^{n-\frac{1}{2}}}{\Delta t}=v^{n}\quad\Rightarrow\quad z^{n+\frac{1}{2}}=z^{n-\frac{1}{2}}+\Delta t\,v^{n}\thinspace.$$

This $$z^{n+\frac{1}{2}}$$ is used in the expression for b to compute $$\varrho(z^{n+\frac{1}{2}})$$ and then $$b^{n+\frac{1}{2}}$$.

The drag coefficient CD depends heavily on the shape of the body. Some values are: 0.45 for aÂ sphere, 0.42 for aÂ semi-sphere, 1.05 for aÂ cube, 0.82 for aÂ long cylinder (when the center axis is in the vertical direction), 0.75 for aÂ rocket, 1.0-1.3 for aÂ man in upright position, 1.3 for aÂ flat plate perpendicular to the flow, and 0.04 for aÂ streamlined, droplet-like body.

### 4.11.6 Verification

To verify the program, one may assume aÂ heavy body in air such that the Fb force can be neglected, and further assume aÂ small velocity such that the air resistance Fd can also be neglected. This can be obtained by setting Î¼ and $$\varrho$$ to zero. The motion then leads to the velocity $$v(t)=v_{0}-gt$$, which is linear in t and therefore should be reproduced to machine precision (say tolerance $$10^{-15}$$) by any implementation based on the Crankâ€“Nicolson or Forward Euler schemes.

Another verification, but not as powerful as the one above, can be based on computing the terminal velocity and comparing with the exact expressions. The advantage of this verification is that we can also test the situation $$\varrho\neq 0$$.

As always, the method of manufactured solutions can be applied to test the implementation of all terms in the governing equation, but then the solution has no physical relevance in general.

### 4.11.7 Scaling

Applying scaling, as described in Sect.Â 4.1, will for the linear case reduce the need to estimate values for seven parameters down to choosing one value of aÂ single dimensionless parameter

$$\beta=\frac{\varrho_{b}gV\left(\frac{\varrho}{\varrho_{b}}-1\right)}{3\pi d\mu I},$$

provided $$I\neq 0$$. If the motion starts from rest, Iâ€‰=â€‰0, the scaled problem reads

$$\bar{u}^{\prime}=1-\bar{u},\quad\bar{u}(0)=0,$$

and there is no need for estimating physical parameters (!). This means that there is aÂ single universal solution to the problem of aÂ falling body starting from rest: $$\bar{u}(t)=1-e^{-\bar{t}}$$. All real physical cases correspond to stretching the $$\bar{t}$$ axis and the $$\bar{u}$$ axis in this dimensionless solution. More precisely, the physical velocity $$u(t)$$ is related to the dimensionless velocity $$\bar{u}(\bar{t})$$ through

$$u=\frac{\varrho_{b}gV\left(\frac{\varrho}{\varrho_{b}}-1\right)}{3\pi d\mu}\bar{u}(t/(g(\varrho/\varrho_{b}-1)))=\frac{\varrho_{b}gV\left(\frac{\varrho}{\varrho_{b}}-1\right)}{3\pi d\mu}(1-e^{t/(g(\varrho/\varrho_{b}-1))})\thinspace.$$

## 4.12 Viscoelastic Materials

When stretching aÂ rod made of aÂ perfectly elastic material, the elongation (change in the rodâ€™s length) is proportional to the magnitude of the applied force. Mathematical models for material behavior under application of external forces use strain $$\varepsilon$$ and stress Ïƒ instead of elongation and forces. Strain is relative change in elongation and stress is force per unit area. An elastic material has aÂ linear relation between stress and strain: $$\sigma=E\varepsilon$$. This is aÂ good model for many materials, but frequently the velocity of the deformation (or more precisely, the strain rate $$\varepsilon^{\prime}$$) also influences the stress. This is particularly the case for materials like organic polymers, rubber, and wood. When the stress depends on both the strain and the strain rate, the material is said to be viscoelastic. AÂ common model relating forces to deformation is the Kelvinâ€“Voigt model:

$$\sigma(t)=E\varepsilon(t)+\eta\varepsilon^{\prime}(t)\thinspace.$$
(4.49)

Compared to aÂ perfectly elastic material, which deforms instantaneously when aÂ force is acting, aÂ Kelvinâ€“Voigt material will spend some time to elongate. For example, when an elastic rod is subject to aÂ constant force $$\boldsymbol{\sigma}$$ at tâ€‰=â€‰0, the strain immediately adjusts to $$\varepsilon=\sigma/E$$. AÂ Kelvinâ€“Voigt material, however, has aÂ response $$\varepsilon(t)=\frac{\sigma}{E}(1-e^{Et/\eta})$$. Removing the force when the strain is $$\varepsilon(t_{1})=I$$ will for an elastic material immediately bring the strain back to zero, while aÂ Kelvinâ€“Voigt material will decay: $$\varepsilon=Ie^{-(t-t_{1})E/\eta)}$$.

Introducing u for $$\varepsilon$$ and treating $$\boldsymbol{\sigma}(t)$$ as aÂ given function, we can write the Kelvinâ€“Voigt model in our standard form

$$u^{\prime}(t)=-au(t)+b(t),$$
(4.50)

with $$a=E/\eta$$ and $$b(t)=\boldsymbol{\sigma}(t)/\eta$$. An initial condition, usually $$u(0)=0$$, is needed.

## 4.13 Decay ODEs from Solving aÂ PDE by Fourier Expansions

Suppose we have aÂ partial differential equation

$$\frac{\partial u}{\partial t}=\alpha\frac{\partial^{2}u}{\partial x^{2}}+f(x,t),$$

with boundary conditions $$u(0,t)=u(L,t)=0$$ and initial condition $$u(x,0)=I(x)$$. One may express the solution as

$$u(x,t)=\sum_{k=1}^{m}A_{k}(t)e^{ikx\pi/L},$$

for appropriate unknown functions Ak, $$k=1,\ldots,m$$. We use the complex exponential $$e^{ikx\pi/L}$$ for easy algebra, but the physical u is taken as the real part of any complex expression. Note that the expansion in terms of $$e^{ikx\pi/L}$$ is compatible with the boundary conditions: all functions $$e^{ikx\pi/L}$$ vanish for xâ€‰=â€‰0 and $$x=L$$. Suppose we can express $$I(x)$$ as

$$I(x)=\sum_{k=1}^{m}I_{k}e^{ikx\pi/L}\thinspace.$$

Such an expansion can be computed by well-known Fourier expansion techniques, but those details are not important here. Also, suppose we can express the given $$f(x,t)$$ as

$$f(x,t)=\sum_{k=1}^{m}b_{k}(t)e^{ikx\pi/L}\thinspace.$$

Inserting the expansions for u and f in the differential equations demands that all terms corresponding to aÂ given k must be equal. The calculations result in the follow system of ODEs:

$$A_{k}^{\prime}(t)=-\alpha\frac{k^{2}\pi^{2}}{L^{2}}+b_{k}(t),\quad k=1,\ldots,m\thinspace.$$

From the initial condition

$$u(x,0)=\sum_{k}A_{k}(0)e^{ikx\pi/L}=I(x)=\sum_{k}I_{k}e^{(ikx\pi/L)},$$

so it follows that $$A_{k}(0)=I_{k}$$, $$k=1,\ldots,m$$. We then have m equations of the form $$A_{k}^{\prime}=-aA_{k}+b$$, $$A_{k}(0)=I_{k}$$, for appropriate definitions of a and b. These ODE problems are independent of each other such that we can solve one problem at aÂ time. The outlined technique is aÂ quite common solution approach to partial differential equations.

### Remark

Since ak depends on k and the stability of the Forward Euler scheme demands $$a_{k}\Delta t\leq 1$$, we get that $$\Delta t\leq\alpha^{-1}L^{2}\pi^{-2}k^{-2}$$ for this scheme. Usually, quite large k values are needed to accurately represent the given functions I and f so that $$\Delta t$$ in the Forward Euler scheme needs to be very small for these large values of k. Therefore, the Crankâ€“Nicolson and Backward Euler schemes, which allow larger $$\Delta t$$ without any growth in the solutions, are more popular choices when creating time-stepping algorithms for partial differential equations of the type considered in this example.

## 4.14 Exercises

### Problem 4.1 (Radioactive decay of Carbon-14)

The Carbon-14 isotope, whose radioactive decay is used extensively in dating organic material that is tens of thousands of years old, has aÂ half-life of 5,730Â years. Determine the age of an organic material that contains 8.4â€¯% of its initial amount of Carbon-14. Use aÂ time unit of 1Â year in the computations. The uncertainty in the half time of Carbon-14 is Â±40Â years. What is the corresponding uncertainty in the estimate of the age?

### Hint 1

Let A be the amount of Carbon-14. The ODE problem is then $$A^{\prime}(t)=-aA(t)$$, $$A(0)=I$$. Introduced the scaled amount $$u=A/I$$. The ODE problem for u is $$u^{\prime}=-au$$, $$u(0)=1$$. Measure time in years. Simulate until the first mesh point tm such that $$u(t_{m})\leq 0.084$$.

### Hint 2

Use simulations with $$5{,}730\pm 40$$â€¯y as input and find the corresponding uncertainty interval for the result.

Filename: carbon14.

### Exercise 4.2 (Derive schemes for Newtonâ€™s law of cooling)

Show in detail how we can apply the ideas of the Forward Euler, Backward Euler, and Crankâ€“Nicolson discretizations to derive explicit computational formulas for new temperature values in Newtonâ€™s law of cooling (see Sect.Â 4.4):

$$\frac{dT}{dt}=-k(T-T_{s}(t)),\quad T(0)=T_{0}\thinspace.$$

Here, T is the temperature of the body, $$T_{s}(t)$$ is the temperature of the surroundings, t is time, k is the heat transfer coefficient, and T0 is the initial temperature of the body. Summarize the discretizations in aÂ Î¸-rule such that you can get the three schemes from aÂ single formula by varying the Î¸ parameter.

Filename: schemes_cooling.

### Exercise 4.3 (Implement schemes for Newtonâ€™s law of cooling)

The goal of this exercise is to implement the schemes from ExerciseÂ 4.2 and investigate several approaches for verifying the implementation.

1. a)

Implement the Î¸-rule from ExerciseÂ 4.2 in aÂ function

where T0 is the initial temperature, k is the heat transfer coefficient, T_s is aÂ function of t for the temperature of the surroundings, t_end is the end time of the simulation, dt is the time step, and theta corresponds to Î¸. The cooling function should return the temperature as an array T of values at the mesh points and the time mesh t.

2. b)

In the case $$\lim_{t\rightarrow\infty}T_{s}(t)=C=\text{const}$$, explain why $$T(t)\rightarrow C$$. Construct an example where you can illustrate this property in aÂ plot. Implement aÂ corresponding test function that checks the correctness of the asymptotic value of the solution.

3. c)

AÂ piecewise constant surrounding temperature,

$$T_{s}(t)=\left\{\begin{array}[]{ll}C_{0},&0\leq t\leq t^{*}\\ C_{1},&t> t^{*},\end{array}\right.$$

corresponds to aÂ sudden change in the environment at $$t=t^{*}$$. Choose $$C_{0}=2T_{0}$$, $$C_{1}=\frac{1}{2}T_{0}$$, and $$t^{*}=3/k$$. Plot the solution $$T(t)$$ and explain why it seems physically reasonable.

4. d)

We know from the ODE $$u^{\prime}=-au$$ that the Crankâ€“Nicolson scheme can give non-physical oscillations for $$\Delta t> 2/a$$. In the present problem, this results indicates that the Crankâ€“Nicolson scheme give undesired oscillations for $$\Delta t> 2/k$$. Discuss if this aÂ potential problem in the physical case from c).

5. e)

Find an expression for the exact solution of $$T^{\prime}=-k(T-T_{s}(t))$$, $$T(0)=T_{0}$$. Construct aÂ test case and compare the numerical and exact solution in aÂ plot.

Find aÂ value of the time step $$\Delta t$$ such that the two solution curves cannot (visually) be distinguished from each other. Many scientists will claim that such aÂ plot provides evidence for aÂ correct implementation, but point out why there still may be errors in the code. Can you introduce bugs in the cooling function and still achieve visually coinciding curves?

### Hint

The exact solution can be derived by multiplying (Ch4.E8) by the integrating factor $$e^{kt}$$.

1. f)

Implement aÂ test function for checking that the solution returned by the cooling function is identical to the exact numerical solution of the problem (to machine precision) when Ts is constant.

### Hint

The exact solution of the discrete equations in the case Ts is aÂ constant can be found by introducing $$u=T-T_{s}$$ to get aÂ problem $$u^{\prime}=-ku$$, $$u(0)=T_{0}-T_{s}$$. The solution of the discrete equations is then of the form $$u^{n}=(T_{0}-T_{s})A^{n}$$ for some amplification factor A. The expression for Tn is then $$T^{n}=T_{s}(t_{n})+u^{n}=T_{s}+(T_{0}-T_{s})A^{n}$$. We find that

$$A=\frac{1-(1-\theta)k\Delta t}{1+\theta k\Delta t}\thinspace.$$

The test function, testing several Î¸ values for aÂ quite coarse mesh, may take the form

Running this function shows that the diff variable is 3.55E-15 as maximum so aÂ tolerance of $$10^{-14}$$ is appropriate. This is aÂ good test that the cooling function works!

Filename: cooling.

### Exercise 4.4 (Find time of murder from body temperature)

AÂ detective measures the temperature of aÂ dead body to be 26.7â€¯$${}^{\circ}$$C at 2â€¯pm. One hour later the temperature is 25.8â€¯$${}^{\circ}$$C. The question is when death occurred.

Assume that Newtonâ€™s law of cooling (Ch4.E8) is an appropriate mathematical model for the evolution of the temperature in the body. First, determine k in (Ch4.E8) by formulating aÂ Forward Euler approximation with one time steep from time 2â€¯am to time 3â€¯am, where knowing the two temperatures allows for finding k. Assume the temperature in the air to be 20â€¯$${}^{\circ}$$C. Thereafter, simulate the temperature evolution from the time of murder, taken as tâ€‰=â€‰0, when $$T=37\,^{\circ}\text{C}$$, until the temperature reaches 25.8â€¯$${}^{\circ}$$C. The corresponding time allows for answering when death occurred.

Filename: detective.

### Exercise 4.5 (Simulate an oscillating cooling process)

The surrounding temperature Ts in Newtonâ€™s law of cooling (Ch4.E8) may vary in time. Assume that the variations are periodic with period P and amplitude a around aÂ constant mean temperature Tm:

$$T_{s}(t)=T_{m}+a\sin\left(\frac{2\pi}{P}t\right)\thinspace.$$
(4.51)

Simulate aÂ process with the following data: $$k=0.05\,\text{min}^{-1}$$, $$T(0)=5\,^{\circ}\text{C}$$, $$T_{m}=25\,^{\circ}\text{C}$$, $$a=2.5\,^{\circ}\text{C}$$, and Pâ€‰=â€‰1â€¯h, Pâ€‰=â€‰10â€¯min, and Pâ€‰=â€‰6â€¯h. Plot the T solutions and Ts in the same plot.

Filename: osc_cooling.

### Exercise 4.6 (Simulate stochastic radioactive decay)

The purpose of this exercise is to implement the stochastic model described in Sect.Â 4.5 and show that its mean behavior approximates the solution of the corresponding ODE model.

The simulation goes on for aÂ time interval $$[0,T]$$ divided into Nt intervals of length $$\Delta t$$. We start with N0 atoms. In some time interval, we have N atoms that have survived. Simulate N Bernoulli trials with probability $$\lambda\Delta t$$ in this interval by drawing N random numbers, each being 0 (survival) or 1 (decay), where the probability of getting 1 is $$\lambda\Delta t$$. We are interested in the number of decays, d, and the number of survived atoms in the next interval is then $$N-d$$. The Bernoulli trials are simulated by drawing N uniformly distributed real numbers on $$[0,1]$$ and saying that 1 corresponds to aÂ value less than $$\lambda\Delta t$$:

Observe that uniform < lambda_*dt is aÂ boolean array whose true and false values become 1 and 0, respectively, when converted to an integer array.

Repeat the simulation over $$[0,T]$$ aÂ large number of times, compute the average value of N in each interval, and compare with the solution of the corresponding ODE model.

Filename: stochastic_decay.

### Problem 4.7 (Radioactive decay of two substances)

Consider two radioactive substances A and B. The nuclei in substance AÂ decay to form nuclei of type B with aÂ half-life $$A_{1/2}$$, while substance B decay to form type A nuclei with aÂ half-life $$B_{1/2}$$. Letting uA and uB be the fractions of the initial amount of material in substance A and B, respectively, the following system of ODEs governs the evolution of $$u_{A}(t)$$ and $$u_{B}(t)$$:

$$\frac{1}{\ln 2}u_{A}^{\prime} =u_{B}/B_{1/2}-u_{A}/A_{1/2},$$
(4.52)
$$\frac{1}{\ln 2}u_{B}^{\prime} =u_{A}/A_{1/2}-u_{B}/B_{1/2},$$
(4.53)

with $$u_{A}(0)=u_{B}(0)=1$$.

1. a)

Make aÂ simulation program that solves for $$u_{A}(t)$$ and $$u_{B}(t)$$.

2. b)

Verify the implementation by computing analytically the limiting values of uA and uB as $$t\rightarrow\infty$$ (assume $$u_{A}^{\prime},u_{B}^{\prime}\rightarrow 0$$) and comparing these with those obtained numerically.

3. c)

Run the program for the case of $$A_{1/2}=10$$ minutes and $$B_{1/2}=50$$ minutes. Use aÂ time unit of 1 minute. Plot uA and uB versus time in the same plot.

### Exercise 4.8 (Simulate aÂ simple chemical reaction)

Consider the simple chemical reaction where aÂ substance A is turned into aÂ substance B according to

\begin{aligned}\displaystyle\frac{d[A]}{dt}&\displaystyle=-k[A],\\ \displaystyle\frac{d[B]}{dt}&\displaystyle=k[A],\end{aligned}

where $$[A]$$ and $$[B]$$ are the concentrations of A and B, respectively. It may be aÂ challenge to find appropriate values of k, but we can avoid this problem by working with aÂ scaled model (as explained in Sect.Â 4.1). Scale the model above, using aÂ time scale $$1/k$$, and use the initial concentration of $$[A]$$ as scale for $$[A]$$ and $$[B]$$. Show that the scaled system reads

\begin{aligned}\displaystyle\frac{du}{dt}&\displaystyle=-u,\\ \displaystyle\frac{dv}{dt}&\displaystyle=u,\end{aligned}

with initial conditions $$u(0)=1$$, and $$v(0)=\alpha$$, where $$\alpha=[B](0)/[A](0)$$ is aÂ dimensionless number, and u and v are the scaled concentrations of $$[A]$$ and $$[B]$$, respectively. Implement aÂ numerical scheme that can be used to find the solutions $$u(t)$$ and $$v(t)$$. Visualize u and v in the same plot.

Filename: chemcial_kinetics_AB.

### Exercise 4.9 (Simulate an n-th order chemical reaction)

An n-order chemical reaction, generalizing the model in ExerciseÂ 4.8, takes the form

\begin{aligned}\displaystyle\frac{d[A]}{dt}&\displaystyle=-k[A]^{n},\\ \displaystyle\frac{d[B]}{dt}&\displaystyle=k[A]^{n},\end{aligned}

where symbols are as defined in ExerciseÂ 4.8. Bring this model on dimensionless form, using aÂ time scale $$[A](0)^{n-1}/k$$, and show that the dimensionless model simplifies to

\begin{aligned}\displaystyle\frac{du}{dt}&\displaystyle=-u^{n},\\ \displaystyle\frac{dv}{dt}&\displaystyle=u^{n},\end{aligned}

with $$u(0)=1$$ and $$v(0)=\alpha=[B](0)/[A](0)$$. Solve numerically for $$u(t)$$ and show aÂ plot with u for nâ€‰=â€‰0.5,1,2,4.

Filename: chemcial_kinetics_ABn.

### Exercise 4.10 (Simulate aÂ biochemical process)

The purpose of this exercise is to simulate the ODE system (Ch4.E23)â€“(Ch4.E26) modeling aÂ simple biochemical process.

1. a)

Scale (Ch4.E23)â€“(Ch4.E26) such that we can work with dimensionless parameters, which are easier to prescribe. Introduce

$$\bar{Q}=\frac{[ES]}{Q_{c}},\quad\bar{P}=\frac{P}{P_{c}},\quad\bar{S}=\frac{S}{S_{0}},\quad\bar{E}=\frac{E}{E_{0}},\quad\bar{t}=\frac{t}{t_{c}},$$

where appropriate scales are

$$Q_{c}=\frac{S_{0}E_{0}}{K},\quad P_{c}=Q_{c},\quad t_{c}=\frac{1}{k_{+}E_{0}},$$

with $$K=(k_{v}+k_{-})/k_{+}$$ (Michaelis constant). Show that the scaled system becomes

$$\frac{d\bar{Q}}{d\bar{t}} =\alpha(\bar{E}\bar{S}-\bar{Q}),$$
(4.54)
$$\frac{d\bar{P}}{d\bar{t}} =\beta\bar{Q},$$
(4.55)
$$\frac{d\bar{S}}{d\bar{t}} =-\bar{E}\bar{S}+(1-\beta\alpha^{-1})\bar{Q},$$
(4.56)
$$\epsilon\frac{d\bar{E}}{d\bar{t}} =-\bar{E}\bar{S}+\bar{Q},$$
(4.57)

where we have three dimensionless parameters

$$\alpha=\frac{K}{E_{0}},\quad\beta=\frac{k_{v}}{k_{+}E_{0}},\quad\epsilon=\frac{E_{0}}{S_{0}}\thinspace.$$

The corresponding initial conditions are $$\bar{Q}=\bar{P}=0$$ and $$\bar{S}=\bar{E}=1$$.

2. b)

Implement aÂ function for solving (Ch4.E54)â€“(Ch4.E57).

3. c)

There are two conservation equations implied by (Ch4.E23)â€“(Ch4.E26):

$$[ES]+[E] =E_{0},$$
(4.58)
$$+[S]+[P] =S_{0}\thinspace.$$
(4.59)

Derive these two equations. Use these properties in the function in b) to do aÂ partial verification of the solution at each time step.

4. d)

Simulate aÂ case with Tâ€‰=â€‰8, $$\alpha=1.5$$ and $$\beta=1$$, and two Ïµ values: 0.9 and 0.1.

Filename: biochem.

### Exercise 4.11 (Simulate spreading of aÂ disease)

The SIR model (Ch4.E27)â€“(Ch4.E29) can be used to simulate spreading of an epidemic disease.

1. a)

Estimating the parameter Î² is difficult so it can be handy to scale the equations. Use $$t_{c}=1/\nu$$ as time scale, and scale S, I, and R by the population size $$N=S(0)+I(0)+R(0)$$. Show that the resulting dimensionless model becomes

$$\frac{d\bar{S}}{d\bar{t}} =-R_{0}\bar{S}\bar{I},$$
(4.60)
$$\frac{d\bar{I}}{d\bar{t}} =R_{0}\bar{S}\bar{I}-\bar{I},$$
(4.61)
$$\frac{d\bar{R}}{d\bar{t}} =I,$$
(4.62)
$$\bar{S}(0) =1-\alpha,$$
(4.63)
$$\bar{I}(0) =\alpha,$$
(4.64)
$$\bar{R}(0) =0,$$
(4.65)

where R0 and Î± are the only parameters in the problem:

$$R_{0}=\frac{N\beta}{\nu},\quad\alpha=\frac{I(0)}{N}\thinspace.$$

AÂ quantity with aÂ bar denotes aÂ dimensionless version of that quantity, e.g, $$\bar{t}$$ is dimensionless time, and $$\bar{t}=\nu t$$.

2. b)

Show that the R0 parameter governs whether the disease will spread or not at tâ€‰=â€‰0.

### Hint

Spreading means $$dI/dt> 0$$.

1. a)

Implement the scaled SIR model. Check at every time step, as aÂ verification, that $$\bar{S}+\bar{I}+\bar{R}=1$$.

2. b)

Simulate the spreading of aÂ disease where $$R_{0}=2,5$$ and 2â€¯% of the population is infected at time tâ€‰=â€‰0.

Filename: SIR.

### Exercise 4.12 (Simulate predator-prey interaction)

SectionÂ 4.8 describes aÂ model for the interaction of predator and prey populations, such as lynx and hares.

1. a)

Scale the equations (Ch4.E30)â€“(Ch4.E31). Use the initial population $$H(0)=H_{0}$$ of H has scale for H and L, and let the time scale be $$1/(bH_{0})$$.

2. b)

Implement the scaled model from a). Run illustrating cases how the two populations develop.

3. c)

The scaling in a) used aÂ scale for H and L based on the initial condition $$H(0)=H_{0}$$. An alternative scaling is to make the ODEs as simple as possible by introducing separate scales Hc and Lc for H and L, respectively. Fit Hc, Lc, and the time scale tc such that there are as few dimensionless parameters as possible in the ODEs. Scale the initial conditions. Compare the number and type of dimensionless parameters with a).

4. d)

Compute with the scaled model from c) and create plots to illustrate the typical solutions.

Filename: predator_prey.

### Exercise 4.13 (Simulate the pressure drop in the atmosphere)

We consider the models for atmospheric pressure in Sect.Â 4.9. Make aÂ program with three functions,

• one computing the pressure $$p(z)$$ using aÂ seven-layer model and varying L,

• one computing $$p(z)$$ using aÂ seven-layer model, but with constant temperature in each layer, and

• one computing $$p(z)$$ based on the one-layer model.

How can these implementations be verified? Should ease of verification impact how you code the functions? Compare the three models in aÂ plot.

Filename: atmospheric_pressure.

### Exercise 4.14 (Make aÂ program for vertical motion in aÂ fluid)

Implement the Stokesâ€™ drag model (Ch4.E40) and the quadratic drag model (Ch4.E43) from Sect.Â 4.11, using the Crankâ€“Nicolson scheme and aÂ geometric mean for $$|v|v$$ as explained, and assume constant fluid density. At each time level, compute the Reynolds number Re and choose the Stokesâ€™ drag model if $$\text{Re}<1$$ and the quadratic drag model otherwise.

The computation of the numerical solution should take place either in aÂ stand-alone function or in aÂ solver class that looks up aÂ problem class for physical data. Create aÂ module and equip it with pytest/nose compatible test functions for automatically verifying the code.

Verification tests can be based on

• the terminal velocity (see Sect.Â 4.11),

• the exact solution when the drag force is neglected (see Sect.Â 4.11),

• the method of manufactured solutions (see Sect.Â 3.1.5) combined with computing convergence rates (see Sect.Â 3.1.6).

Use, e.g., aÂ quadratic polynomial for the velocity in the method of manufactured solutions. The expected error is $$\mathcal{O}(\Delta t^{2})$$ from the centered finite difference approximation and the geometric mean approximation for $$|v|v$$.

AÂ solution that is linear in t will also be an exact solution of the discrete equations in many problems. Show that this is true for linear drag (by adding aÂ source term that depends on t), but not for quadratic drag because of the geometric mean approximation. Use the method of manufactured solutions to add aÂ source term in the discrete equations for quadratic drag such that aÂ linear function of t is aÂ solution. Add aÂ test function for checking that the linear function is reproduced to machine precision in the case of both linear and quadratic drag.

Apply the software to aÂ case where aÂ ball rises in water. The buoyancy force is here the driving force, but the drag will be significant and balance the other forces after aÂ short time. AÂ soccer ball has radius 11 cm and mass 0.43â€¯kg. Start the motion from rest, set the density of water, $$\varrho$$, to $$1000\,\mathrm{kg/m}^{3}$$, set the dynamic viscosity, Î¼, to $$10^{-3}\,\text{Pa\,s}$$, and use aÂ drag coefficient for aÂ sphere: 0.45. Plot the velocity of the rising ball.

Filename: vertical_motion.

### Project 4.15 (Simulate parachuting)

The aim of this project is to develop aÂ general solver for the vertical motion of aÂ body with quadratic air drag, verify the solver, apply the solver to aÂ skydiver in free fall, and finally apply the solver to aÂ complete parachute jump.

All the pieces of software implemented in this project should be realized as Python functions and/or classes and collected in one module.

1. a)

Set up the differential equation problem that governs the velocity of the motion. The parachute jumper is subject to the gravity force and aÂ quadratic drag force. Assume constant density. Add an extra source term to be used for program verification. Identify the input data to the problem.

2. b)

Make aÂ Python module for computing the velocity of the motion. Also equip the module with functionality for plotting the velocity.

### Hint 1

Use the Crankâ€“Nicolson scheme with aÂ geometric mean of $$|v|v$$ in time to linearize the equation of motion with quadratic drag.

### Hint 2

You can either use functions or classes for implementation. If you choose functions, make aÂ function solver that takes all the input data in the problem as arguments and that returns the velocity (as aÂ mesh function) and the time mesh. In case of aÂ class-based implementation, introduce aÂ problem class with the physical data and aÂ solver class with the numerical data and aÂ solve method that stores the velocity and the mesh in the class.

Allow for aÂ time-dependent area and drag coefficient in the formula for the drag force.

1. a)

Show that aÂ linear function of t does not fulfill the discrete equations because of the geometric mean approximation used for the quadratic drag term. Fit aÂ source term, as in the method of manufactured solutions, such that aÂ linear function of t is aÂ solution of the discrete equations. Make aÂ test function to check that this solution is reproduced to machine precision.

2. b)

The expected error in this problem goes like $$\Delta t^{2}$$ because we use aÂ centered finite difference approximation with error $$\mathcal{O}(\Delta t^{2})$$ and aÂ geometric mean approximation with error $$\mathcal{O}(\Delta t^{2})$$. Use the method of manufactured solutions combined with computing convergence rate to verify the code. Make aÂ test function for checking that the convergence rate is correct.

3. c)

Compute the drag force, the gravity force, and the buoyancy force as aÂ function of time. Create aÂ plot with these three forces.

### Hint

You can either make aÂ function forces(v, t, plot=None) that returns the forces (as mesh functions) and t, and shows aÂ plot on the screen and also saves the plot to aÂ file with name stored in plot if plot is not None, or you can extend the solver class with computation of forces and include plotting of forces in the visualization class.

1. f)

Compute the velocity of aÂ skydiver in free fall before the parachute opens.

### Hint

Meade and Struthers [11] provide some data relevant to skydiving. The mass of the human body and equipment can be set to 100â€¯kg. AÂ skydiver in spread-eagle formation has aÂ cross-section of 0.5â€¯$$\text{m}^{2}$$ in the horizontal plane. The density of air decreases with altitude, but can be taken as constant, 1â€¯$$\mathrm{kg/m}^{3}$$, for altitudes relevant to skydiving (0â€“4000â€¯m). The drag coefficient for aÂ man in upright position can be set to 1.2. Start with aÂ zero velocity. AÂ free fall typically has aÂ terminating velocity of 45â€¯m$$/$$s. (This value can be used to tune other parameters.)

1. g)

The next task is to simulate aÂ parachute jumper during free fall and after the parachute opens. At time tp, the parachute opens and the drag coefficient and the cross-sectional area change dramatically. Use the program to simulate aÂ jump from zâ€‰=â€‰3000â€¯m to the ground zâ€‰=â€‰0. What is the maximum acceleration, measured in units of g, experienced by the jumper?

### Hint

Following Meade and Struthers [11], one can set the cross-section area perpendicular to the motion to 44â€¯$$\text{m}^{2}$$ when the parachute is open. Assume that it takes 8â€¯s to increase the area linearly from the original to the final value. The drag coefficient for an open parachute can be taken as 1.8, but tuned using the known value of the typical terminating velocity reached before landing: 5.3â€¯m$$/$$s. One can take the drag coefficient as aÂ piecewise constant function with an abrupt change at tp. The parachute is typically released after $$t_{p}=60$$ s, but larger values of tp can be used to make plots more illustrative.

Filename: parachuting.

### Exercise 4.16 (Formulate vertical motion in the atmosphere)

Vertical motion of aÂ body in the atmosphere needs to take into account aÂ varying air density if the range of altitudes is many kilometers. In this case, $$\varrho$$ varies with the altitude z. The equation of motion for the body is given in Sect.Â 4.11. Let us assume quadratic drag force (otherwise the body has to be very, very small). AÂ differential equation problem for the air density, based on the information for the one-layer atmospheric model in Sect.Â 4.9, can be set up as

$$p^{\prime}(z) =-\frac{Mg}{R^{*}(T_{0}+Lz)}p,$$
(4.66)
$$\varrho =p\frac{M}{R^{*}T}\thinspace.$$
(4.67)

To evaluate $$p(z)$$ we need the altitude z. From the principle that the velocity is the derivative of the position we have that

$$z^{\prime}(t)=v(t),$$
(4.68)

where v is the velocity of the body.

Explain in detail how the governing equations can be discretized by the Forward Euler and the Crankâ€“Nicolson methods. Discuss pros and cons of the two methods.

Filename: falling_in_variable_density.

### Exercise 4.17 (Simulate vertical motion in the atmosphere)

Implement the Forward Euler or the Crankâ€“Nicolson scheme derived in ExerciseÂ 4.16. Demonstrate the effect of air density variation on aÂ falling human, e.g., the famous fall of Felix Baumgartner. The drag coefficient can be set to 1.2.

Filename: falling_in_variable_density.

### Problem 4.18 (Compute $$y=|x|$$ by solving an ODE)

Consider the ODE problem

$$y^{\prime}(x)=\left\{\begin{array}[]{ll}-1,&x<0,\\ 1,&x\geq 0\end{array}\right.\quad x\in(-1,1],\quad y(1-)=1,$$

which has the solution $$y(x)=|x|$$. Using aÂ mesh $$x_{0}=-1$$, $$x_{1}=0$$, and $$x_{2}=1$$, calculate by hand y1 and y2 from the Forward Euler, Backward Euler, Crankâ€“Nicolson, and Leapfrog methods. Use all of the former three methods for computing the y1 value to be used in the Leapfrog calculation of y2. Thereafter, visualize how these schemes perform for aÂ uniformly partitioned mesh with Nâ€‰=â€‰10 and Nâ€‰=â€‰11 points.

Filename: signum.

### Problem 4.19 (Simulate fortune growth with random interest rate)

The goal of this exercise is to compute the value of aÂ fortune subject to inflation and aÂ random interest rate. Suppose that the inflation is constant at i percent per year and that the annual interest rate, p, changes randomly at each time step, starting at some value p0 at tâ€‰=â€‰0. The random change is from aÂ value pn at $$t=t_{n}$$ to $$p_{n}+\Delta p$$ with probability 0.25 and $$p_{n}-\Delta p$$ with probability 0.25. No change occurs with probability 0.5. There is also no change if $$p^{n+1}$$ exceeds 15 or becomes below 1. Use aÂ time step of one month, $$p_{0}=i$$, initial fortune scaled to 1, and simulate 1000 scenarios of length 20 years. Compute the mean evolution of one unit of money and the corresponding standard deviation. Plot the mean curve along with the mean plus one standard deviation and the mean minus one standard deviation. This will illustrate the uncertainty in the mean curve.

### Hint 1

The following code snippet computes $$p^{n+1}$$:

### Hint 2

If $$u_{i}(t)$$ is the value of the fortune in experiment number i, $$i=0,\ldots,N-1$$, the mean evolution of the fortune is

$$\bar{u}(t)=\frac{1}{N}\sum_{i=0}^{N-1}u_{i}(t),$$

and the standard deviation is

$$s(t)=\sqrt{\frac{1}{N-1}\left(-(\bar{u}(t))^{2}+\sum_{i=0}^{N-1}(u_{i}(t))^{2}\right)}\thinspace.$$

Suppose $$u_{i}(t)$$ is stored in an array u. The mean and the standard deviation of the fortune is most efficiently computed by using two accumulation arrays, sum_u and sum_u2, and performing sum_u += u and sum_u2 += u**2 after every experiment. This technique avoids storing all the $$u_{i}(t)$$ time series for computing the statistics.

Filename: random_interest.

### Exercise 4.20 (Simulate aÂ population in aÂ changing environment)

We shall study aÂ population modeled by (Ch4.E3) where the environment, represented by r and f, undergoes changes with time.

1. a)

Assume that there is aÂ sudden drop (increase) in the birth (death) rate at time $$t=t_{r}$$, because of limited nutrition or food supply:

$$r(t)=\left\{\begin{array}[]{ll}\varrho,&t<t_{r},\\ \varrho-A,&t\geq t_{r}.\end{array}\right.$$

This drop in population growth is compensated by aÂ sudden net immigration at time $$t_{f}> t_{r}$$:

$$f(t)=\left\{\begin{array}[]{ll}0,&t<t_{f},\\ f_{0},&t\geq t_{a}.\end{array}\right.$$

Start with $$\varrho$$ and make $$A> \varrho$$. Experiment with these and other parameters to illustrate the interplay of growth and decay in such aÂ problem.

2. b)

Now we assume that the environmental conditions changes periodically with time so that we may take

$$r(t)={\varrho}+A\sin\left(\frac{2\pi}{P}t\right)\thinspace.$$

That is, the combined birth and death rate oscillates around $$\varrho$$ with aÂ maximum change of Â±A repeating over aÂ period of length P in time. Set fâ€‰=â€‰0 and experiment with the other parameters to illustrate typical features of the solution.

Filename: population.py.

### Exercise 4.21 (Simulate logistic growth)

Solve the logistic ODE (Ch4.E4) using aÂ Crankâ€“Nicolson scheme where $$(u^{n+\frac{1}{2}})^{2}$$ is approximated by aÂ geometric mean:

$$(u^{n+\frac{1}{2}})^{2}\approx u^{n+1}u^{n}\thinspace.$$

This trick makes the discrete equation linear in $$u^{n+1}$$.

Filename: logistic_CN.

### Exercise 4.22 (Rederive the equation for continuous compound interest)

The ODE model (Ch4.E7) was derived under the assumption that r was constant. Perform an alternative derivation without this assumption: 1) start with (Ch4.E5); 2) introduce aÂ time step $$\Delta t$$ instead of m: $$\Delta t=1/m$$ if t is measured in years; 3) divide by $$\Delta t$$ and take the limit $$\Delta t\rightarrow 0$$. Simulate aÂ case where the inflation is at aÂ constant level I percent per year and the interest rate oscillates: $$r=-I/2+r_{0}\sin(2\pi t)$$. Compare solutions for $$r_{0}=I,3I/2,2I$$.

Filename: interest_modeling.

### Exercise 4.23 (Simulate the deformation of aÂ viscoelastic material)

Stretching aÂ rod made of polymer will cause deformations that are well described with aÂ Kelvinâ€“Voigt material model (Ch4.E49). At tâ€‰=â€‰0 we apply aÂ constant force $$\sigma=\sigma_{0}$$, but at $$t=t_{1}$$, we remove the force so $$\sigma=0$$. Compute numerically the corresponding strain (elongation divided by the rodâ€™s length) and visualize how it responds in time.

### Hint

To avoid finding proper values of the E and Î· parameters for aÂ polymer, one can scale the problem. AÂ common dimensionless time is $$\bar{t}=tE/\eta$$. Note that $$\varepsilon$$ is already dimensionless by definition, but it takes on small values, say up to 0.1, so we introduce aÂ scaling: $$\bar{u}=10\varepsilon$$ such that $$\bar{u}$$ takes on values up to about unity.

Show that the material model then takes the form $$\bar{u}^{\prime}=-\bar{u}+10\sigma(t)/E$$. Work with the dimensionless force $$F=10\sigma(t)/E$$, and let Fâ€‰=â€‰1 for $$\bar{t}\in(0,\bar{t}_{1})$$ and Fâ€‰=â€‰0 for $$\bar{t}\geq\bar{t}_{1}$$. AÂ possible choice of t1 is the characteristic time $$\eta/E$$, which means that $$\bar{t}_{1}=1$$.

Filename: KelvinVoigt.