r
(
x
)
.
So we end up with the special case of our original equation where
q
(
x
) = 0. Actually, this special
case can be solved by simply integrating twice and adjusting the constants of integration to ensure
f
(0) =
A
and
f
(1) =
B
. For example, when
r
(
x
) =
r
is constant and
A
=
B
= 1, the solution
is
f
(
x
) = 1

rx/
2 +
rx
2
/
2. We can use this exact solution to compare against the approximate
solution that we will compute.
I.3.2. Discretization
In the finite difference approach to solving differential equations approximately, we want to ap
proximate a function by a vector containing a finite number of sample values. Pick equally spaced
points
x
k
=
k/N
,
k
= 0
, . . . , N
between 0 and 1. We will represent a function
f
(
x
) by its values
f
k
=
f
(
x
k
) at these points. Let
F
=
f
0
f
1
.
.
.
f
N
.
x
f(x)
x
x
x
x
x
x
x
x
x
0
1
2
3
4
5
6
7
8
f
f
f
f
f
f
f
f
f
0
1
2
3
4
5
6
7
8
51
At this point we throw away all the other information about the function, keeping only the values
at the sampled points.
x
f(x)
x
x
x
x
x
x
x
x
x
0
1
2
3
4
5
6
7
8
f
f
f
f
f
f
f
f
f
0
1
2
3
4
5
6
7
8
If this is all we have to work with, what should we use as an approximation to
f
0
(
x
)? It seems
reasonable to use the slopes of the line segments joining our sampled points.
x
f(x)
x
x
x
x
x
x
x
x
x
0
1
2
3
4
5
6
7
8
f
f
f
f
f
f
f
f
f
0
1
2
3
4
5
6
7
8
Notice, though, that there is one slope for every interval (
x
i
, x
i
+1
) so the vector containing the
slopes has one fewer entry than the vector
F
. The formula for the slope in the interval (
x
i
, x
i
+1
)
is (
f
i
+1

f
i
)
/
Δ
x
where the distance Δ
x
=
x
i
+1

x
i
(in this case Δ
x
= 1
/N
). Thus the vector
containing the slopes is
F
0
= (Δ
x
)

1
f
1

f
0
f
2

f
1
f
3

f
2
.
.
.
f
N

f
N

1
= (Δ
x
)

1

1
1
0
0
· · ·
0
0

1
1
0
· · ·
0
0
0

1
1
· · ·
0
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
0
0
0
0
· · ·
1
f
0
f
1
f
2
f
3
.
.
.
f
N
= (Δ
x
)

1
D
N
F
where
D
N
is the
N
×
(
N
+ 1) finite difference matrix in the formula above. The vector
F
0
is our
approximation to the first derivative function
f
0
(
x
).
52
To approximate the second derivative
f
00
(
x
), we repeat this process to define the vector
F
00
. There
will be one entry in this vector for each adjacent pair of slopes, that is, each adjacent pair of entries
of
F
0
. These are naturally labelled by the interior points
x
1
, x
2
, . . . , x
n

1
. Thus we obtain
F
00
= (Δ
x
)

2
D
N

1
D
N
F
= (Δ
x
)

2
1

2
1
0
· · ·
0
0
0
0
1

2
1
· · ·
0
0
0
0
0
1

2
· · ·
0
0
0
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
0
0
0
0
· · ·
1

2
1
f
0
f
1
f
2
f
3
.
.
.
f
N
.
Let
r
k
=
r
(
x
k
) be the sampled points for the load function
r
(
x
) and define the vector approxi
mation for
r
at the interior points
r
=
r
1
.