![](/user_photo/_userpic.png)
primer_on_scientific_programming_with_python
.pdf![](/html/91133/112/html_7ILhI2Vg3Y.62s5/htmlconvd-HSZ47J631x1.jpg)
![](/html/91133/112/html_7ILhI2Vg3Y.62s5/htmlconvd-HSZ47J632x1.jpg)
614 |
|
|
|
|
|
|
|
B |
Di erential Equations |
|
|
|
|
|
|||||
A numerical scheme for the logistic equation (B.23) is given by |
|||||||||
|
uk+1 − uk |
= αu |
1 |
− |
uk |
|
, |
|
|
|
R |
|
|||||||
|
Δt |
k |
|
|
|||||
which we can solve with respect to the unknown uk+1: |
|||||||||
uk+1 = uk + Δtαuk 1 − |
uk |
. |
(B.24) |
||||||
R |
This is the form of the equation that is suited for implementation.
B.4 A General Ordinary Di erential Equation
Let us briefly consider a general ordinary3 di erential equations on the form
u (t) = f (u(t)) |
(B.25) |
subject to the initial condition
u(0) = u0
where f (u) is a given function and the initial state u0 is given. Suppose we want to compute an approximate solution of (B.25) for t ranging from t = 0 to t = T where T > 0 is given. As above we start by introducing the time step
Δt = T /n.
where n 1 is a given integer, and we let uk denote an approximation of the exact solution u(tk). Also as above, we replace the derivative of u with a finite di erence and obtain the scheme
uk+1 − uk = f (uk). Δt
The scheme can be rewritten in a form that is more suitable for computations;
3Di erential equations are divided into two groups: ordinary di erential equations and partial di erential equations. Ordinary di erential equations contain derivatives with respect to one variable (usually t in our examples), whereas partial di erential equations contain derivatives with respect to more than one variable, typically with respect to space and time. A typical ordinary di erential equation is
u (t) = u(t),
and a typical partial di erential equation is
∂u ∂2u ∂2u ∂t = ∂x2 + ∂y2 ,
The latter is known as the heat or di usion equation.
![](/html/91133/112/html_7ILhI2Vg3Y.62s5/htmlconvd-HSZ47J633x1.jpg)
B.5 A Simple Pendulum |
615 |
|
|
uk+1 = uk + Δtf (uk). |
(B.26) |
We note that since u0 is known, we can compute u1, u2 and so on. This scheme is commonly referred to as the Explicit Euler4 scheme, or the Forward Euler scheme, or the Forward Euler method. We will later derive the Implicit Euler scheme, also called the Backward Euler scheme (or method). That scheme is sligtly harder to use but it has some nice properties that will be discussed later.
The Explicit Euler scheme is implemented in the function Explicit_Euler in the module Euler. The test block in the module file allows input from the command line: the formula for f (u), the number of time steps n, the final time T , and the initial condition u0.
from numpy import linspace, zeros
def Explicit_Euler(f, u0, T, n): dt = T/float(n)
t = linspace(0, T, n+1) u = zeros(n+1)
u[0] = u0
for k in range(n):
u[k+1] = u[k] + dt*f(u[k]) return u, t
if __name__ == ’__main__’: f_formula = sys.argv[1] n = int(sys.argv[2])
T = eval(sys.argv[3]) u0 = eval(sys.argv[4])
f = StringFunction(f_formula, independent_variables=’u’) u, t = Explicit_Euler(f, u0, T, n)
plot(t, u)
In Figure B.2 we see the results of the program as applied to the
problem
u = eu
with u0 = 0, T = 1. The numerical results are provided for n = 5, 10, 20 and 100. Convergence is not as obvious anymore, so let us also try the program for n = 100, 200, 300 and 400. The results are given in Figure B.3 and we see that the approximations seem to tend to a common limiting function.
B.5 A Simple Pendulum
So far we have considered scalar ordinary di erential equations, i.e., equations with one single function u(t) as unknown. Now we shall deal
4Leonhard Paul Euler, 1707–1783. A pioneering Swiss mathematician and physicist who spent most of his life in Russia and Germany. Euler is one of the greatest scientists of all time, and made important contributions to calculus, mechanics, optics, and astronomy. He also introduced much of the modern terminology and notation in mathematics.
![](/html/91133/112/html_7ILhI2Vg3Y.62s5/htmlconvd-HSZ47J634x1.jpg)
616 |
B Di erential Equations |
|
|
Explicit Euler for the differential equation u’ = eu using different values for n.
|
4 |
n = 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
n = 10 |
|
|
|
|
|
3.5 |
n = 20 |
|
|
|
|
|
n = 100 |
|
|
|
|
|
|
3 |
|
|
|
|
|
|
2.5 |
|
|
|
|
|
u |
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
1.5 |
|
|
|
|
|
|
1 |
|
|
|
|
|
|
0.5 |
|
|
|
|
|
|
0 |
|
|
|
|
|
|
0 |
0.2 |
0.4 |
0.6 |
0.8 |
1 |
|
|
|
|
t |
|
|
Fig. B.2 Explicit Euler for the di erential equation u = eu using di erent values for n.
Explicit Euler for the differential equation u’ = eu using different values for n.
|
6 |
|
|
|
|
n = 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
n = 200 |
|
|
|
|
|
|
n = 300 |
|
5 |
|
|
|
|
n = 400 |
|
|
|
|
|
|
|
|
4 |
|
|
|
|
|
u |
|
|
|
|
|
|
= e |
3 |
|
|
|
|
|
u’ |
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
1 |
|
|
|
|
|
|
0 |
|
|
|
|
|
|
0 |
0.2 |
0.4 |
0.6 |
0.8 |
1 |
|
|
|
|
t |
|
|
Fig. B.3 Explicit Euler for the di erential equation u = eu using di erent values for |
||||||
n. |
|
|
|
|
|
|
with systems of ordinary di erential equations, where in general n unknown functions are coupled in a system of n equations. Our introductory example will be a system of two equations having two unknown functions u(t) and v(t). The example concerns the motion of a pendulum, see Figure B.4. A sphere with mass m is attached to a massless rod of length L and oscillates back and forth due to gravity. Newton’s second law of motion applied to this physical system gives rise the di erential equation
θ (t) + α sin(θ) = 0 |
(B.27) |
where θ = θ(t) is the angle the rod makes with the vertical, measured in radians, and α = g/L (g is the acceleration of gravity). The un-
![](/html/91133/112/html_7ILhI2Vg3Y.62s5/htmlconvd-HSZ47J635x1.jpg)
B.5 A Simple Pendulum |
617 |
|
|
Fig. B.4 A pendulum with m = mass, L = length of massless rod and θ = θ(t) = angle.
known function to solve for is θ, and knowing θ, we can quite easily compute the position of the sphere, its velocity, and its acceleration, as well as the tension force in the rod. Since the highest derivative in (B.27) is of second order, we refer to (B.27) as a second-order differential equations. Our previous examples in this chapter involved only first-order derivatives, and therefore they are known as first-order di erential equations.
Equation (B.27) can be solved by the same numerical method as we use in Appendix C.1.2, because (B.27) is very similar to Equation C.8, which is the topic of Appendix C. The only di erence is that C.8 has extra terms, which can be skipped, while the kS term in C.8 must be extended to α sin(S) to make C.8 identical to (B.27). This extension is easily performed. However, here we shall not solve the second-order equation (B.27) as it stands. We shall instead rewrite it as a system of two first-order equations so that we can use numerical methods for first-order equations to solve it.
To transform a second-order equation to a system of two first-order equations, we introduce a new variable for the first-order derivative (the angular velocity of the sphere): v(t) = θ (t). Using v and θ in
(B.27) yields
v (t) + α sin(θ) = 0 .
In addition, we have the relation
v = θ (t)
between v and θ. This means that (B.27) is equivalent to the following system of two coupled first-order di erential equations:
θ (t) = v(t), |
(B.28) |
||
v (t) = |
− |
α sin(θ) . |
(B.29) |
|
|
|
As for scalar di erential equations, we need initial conditions, now two conditions because we have two unknown functions:
![](/html/91133/112/html_7ILhI2Vg3Y.62s5/htmlconvd-HSZ47J636x1.jpg)
618 B Di erential Equations
θ(0) = θ0, v(0) = v0,
Here we assume the initial angle θ0 and the initial angular velocity v0 to be given.
It is common to group the unknowns and the initial conditions in 2-vectors: (θ(t), v(t)) and (θ0, v0). One may then view (B.28)–(B.29) as a vector equation, whose first component equation is (B.28), and the second component equation is (B.29). In Python software, this vector notation makes solution methods for scalar equations (almost) immediately available for vector equations, i.e., systems of ordinary di erential equations.
In order to derive a numerical method for the system (B.28)–(B.29), we proceed as we did above for one equation with one unknown function. Say we want to compute the solution from t = 0 to t = T where T > 0 is given. Let n 1 be a given integer and define the time step
Δt = T /n.
Furthermore, we let (θk, vk) denote approximations of the exact solution (θ(tk), v(tk)) for k = 0, 1, . . . , n. A Forward Euler type of method will now read
θk+1 − θk |
= vk, |
|
(B.30) |
||
Δt |
|
||||
|
|
|
|
|
|
vk+1 − vk |
= |
− |
α sin(θ |
). |
(B.31) |
Δt |
|
k |
|
|
This scheme can be rewritten in a form more suitable for implementation:
θk+1 |
= θk + Δt vk, |
(B.32) |
vk+1 |
= vk − αΔt sin(θk) . |
(B.33) |
The next program, pendulum.py, implements this method in the function pendulum. The input parameters to the model, θ0, v0,, the final time T , and the number of time-steps n, must be given on the command line.
def pendulum(T, n, theta0, v0, alpha):
"""Return the motion (theta, v, t) of a pendulum.""" dt = T/float(n)
t = linspace(0, T, n+1) v = zeros(n+1)
theta = zeros(n+1) v[0] = v0 theta[0] = theta0 for k in range(n):
theta[k+1] = theta[k] + dt*v[k]
v[k+1] = v[k] - alpha*dt*sin(theta[k+1]) return theta, v, t
![](/html/91133/112/html_7ILhI2Vg3Y.62s5/htmlconvd-HSZ47J637x1.jpg)
![](/html/91133/112/html_7ILhI2Vg3Y.62s5/htmlconvd-HSZ47J638x1.jpg)
![](/html/91133/112/html_7ILhI2Vg3Y.62s5/htmlconvd-HSZ47J639x1.jpg)
![](/html/91133/112/html_7ILhI2Vg3Y.62s5/htmlconvd-HSZ47J640x1.jpg)
622 |
|
|
|
|
|
|
B Di erential Equations |
|
|
|
|
|
|
|
|
|
|
Exercise B.3. Solve an ODE for y(x). |
|
|
|
|
||||
We have given the following ODE problem: |
|
|||||||
|
dy |
1 |
|
√ |
|
|
|
|
|
|
= |
|
, y(0) = 1 + |
|
, x [0, 4], |
(B.34) |
|
|
dx |
2(y − 1) |
where > 0 is a small number. Formulate a Forward Euler method for this ODE problem and compute the solution for varying step size in x: Δx = 1, Δx = 0.25, Δx = 0.01. Plot√the numerical solutions together with the exact solution y(x) = 1 + x + , using 1001 x coordinates for accurate resolution of the latter. Set to 10−3. Study the numerical solution with Δx = 1, and use that insight to explain why this problem
is hard to solve numerically. Name of program file: yx_ODE.py. |
|
Exercise B.4. Experience instability of an ODE. |
|
Consider the ODE problem |
|
u = αu, u(0) = u0, |
|
solved by the Forward Euler method. Show by repeatedly applying the
scheme that
uk = (1 + αΔt)ku0 .
We now turn to the case α < 0. Show that the numerical solution will oscillate if Δt > −1/α. Make a program for computing uk, set α = −1, and demonstrate oscillatory solutions for Δt = 1.1, 1.5, 1.9. Recall that that the exact solution, u(t) = eαt, never oscillates.
What happens if Δt > −2/α? Try it out in the program and explain then mathematically why not uk → 0 as k → ∞. Name of program file: unstable_ODE.py.
Exercise B.5. Solve an ODE for the arc length.
Given a curve y = f (x), the length of the curve from x = x0 to some point x is given by the function s(x), which fulls the problem
ds |
= 1 + [f (x)]2, s(x0) = 0 . |
(B.35) |
dx |
Since s does not enter the right-hand side, (B.35) can immediately be integrated from x0 to x. However, we shall solve (B.35) as an ODE. Use the Forward Euler method and compute the length of a straight line (for verification) and a sine curve: f (x) = 12 x + 1, x [0, 2]; f (x) = sin(πx), x [0, 2]. Name of program file: arclength_ODE.py.
Exercise B.6. Solve an ODE with time-varying growth.
Consider the ODE for exponential growth,
u = αu, u(0) = 1, t |
|
[0, T ] . |
|
|