1 Three point stencil

Let us consider the boundary value problem defined by Equations (1):

p ( x ) y ( x ) + q ( x ) y ( x ) + r ( x ) y ( x ) = s ( x ) ( 0 < x < &ell; ) a 0 y ( 0 ) + b 0 y ( 0 ) = c 0 a 1 y ( &ell; ) + b 1 y ( &ell; ) = c 1 ( 1 )

The first line is the differential equation , and the second and third lines are the boundary conditions which can involve derivatives.

It is our aim to approximate the solution of this problem numerically, and we adopt an approach similar to that seen in HELM booklet  32.

We divide the interval 0 < x < &ell; into a number, J say, of subintervals each of equal width h = &ell; J . Our numerical solution will provide an approximation to y = y ( x ) at each value of x where two subintervals meet (see Figure 1).

Figure 1

No alt text was set. Please request alt text from the person who provided you with this resource.

Key Point 1

A numerical approximation to the boundary value problem

p ( x ) y ( x ) + q ( x ) y ( x ) + r ( x ) y ( x ) = s ( x ) ( 0 < x < &ell; ) a 0 y ( 0 ) + b 0 y ( 0 ) = c 0 a 1 y ( &ell; ) + b 1 y ( &ell; ) = c 1

is a sequence of numbers y 0 , y 1 , y 2 , y 3 , , y J .

Here y j is an approximation to y ( x ) at x = j h , where h = &ell; J and j = 0 , 1 , 2 , , J .

It is useful to give a name to the x -values where we seek an approximation to y = y ( x ) . Hence we will sometimes write x j = j h j = 0 , 1 , 2 , , J .

The functions p , q , r and s will frequently occur evaluated at the values x j so it is convenient to set up the following abbreviations:

p j = p ( x j ) , q j = q ( x j ) , r j = r ( x j ) , s j = s ( x j ) .

A numerical approximation to Equations (1) can be found by approximating the derivatives by finite differences. Here we will approximate y ( x ) and y ( x ) by central differences to obtain

p ( x j ) y ( x j + h ) 2 y ( x j ) + y ( x j h ) h 2 + q ( x j ) y ( x j + h ) y ( x j h ) 2 h + r ( x j ) y ( x j ) s ( x j )

that is, on using the abbreviations established in Key Point 1,

p j y ( x j + h ) 2 y ( x j ) + y ( x j h ) h 2 + q j y ( x j + h ) y ( x j h ) 2 h + r j y ( x j ) s j

which we use as the motivation for the numerical method

p j y j + 1 2 y j + y j 1 h 2 + q j y j + 1 y j 1 2 h + r j y j = s j .

This last equation can be rearranged, gathering together all the like y -terms. It neatens things further to multiply through by h 2 as well, and the result of these manipulations appears in the following Key Point.

Key Point 2

A central difference approximation to

p ( x ) y ( x ) + q ( x ) y ( x ) + r ( x ) y ( x ) = s ( x )

is

y j 1 p j h 2 q j + y j h 2 r j 2 p j + y j + 1 p j + h 2 q j = h 2 s j .

This approximation to the differential equation can be thought of as a three-point stencil linking three of the approximate y -values. The expression

y j 1 p j h 2 q j + y j h 2 r j 2 p j + y j + 1 p j + h 2 q j = h 2 s j .

is centred around x = x j and involves y j 1 , y j and y j + 1 . The general rule when dealing with a numerical stencil like this is to centre the stencil at every point where y is unknown . (This general rule will appear again, for example on page 13.)

Key Point 3

Centre the stencil at every x -point where y is unknown. This will give a set of equations for the unknown y -values, and we are guaranteed exactly as many equations as there are unknowns.

In the following Example, matters are simplified because the functions p , q , r and s are all constant.

Example 1

Let y = y ( x ) be a solution to the boundary value problem

3 y ( x ) + 4 y ( x ) + 5 y ( x ) = 7 0 < x < 1.5

y ( 0 ) = 2 , y ( 1.5 ) = 2 .

Using a mesh width of h = 0.5 , obtain a central difference approximation to the differential equation and hence find y j y ( j h ) , j = 1 , 2 .

Solution

In general, the central difference approximation to

p ( x ) y ( x ) + q ( x ) y ( x ) + r ( x ) y ( x ) = s ( x )

is

y j 1 p j h 2 q j + y j h 2 r j 2 p j + y j + 1 p j + h 2 q j = h 2 s j .

In this case the coefficients are

p j h 2 q j = 2 , h 2 r j 2 p j = 4.75 , p j + h 2 q j = 4 , h 2 s j = 1 . 75 .

These values will be the same for all x because p , q , r and s are constants in this Example. Hence the general stencil is

2 y j 1 4.75 y j + 4 y j + 1 = 1.75

In this case h = 0.5 , and our numerical solution consists of the values

y 0 = y ( 0 ) = 2 from the boundary condition y 1 y ( 0.5 ) y 2 y ( 1 ) y 3 = y ( 1.5 ) = 2 from the boundary condition

So there are two unknowns, y 1 and y 2 . We centre the stencil at each of the corresponding x values. Putting j = 1 in the numerical stencil gives

2 y 0 4.75 y 1 + 4 y 2 = 1.75

Moving the stencil one place to the right, we put j = 2 so that

2 y 1 4.75 y 2 + 4 y 3 = 1.75

In these two equations y 0 and y 3 are known from the boundary conditions and we move terms involving them to the right-hand side. This leads to the system of equations

4.75 4 2 4.75 y 1 y 2 = 2.25 6.25

Solving this pair of simultaneous equations we find that

y 1 = 2.45 , y 2 = 2.35

to 2 decimal places.

This approximation is shown in Figure 2 in which the numerical approximations to point values of y are shown as circles.

Figure 2

No alt text was set. Please request alt text from the person who provided you with this resource.

The question remains how close to the exact solution these approximations are. (Of course for Example 1 above it is possible to find the analytic solution fairly easily, but this will not usually be the case.)

A pragmatic way to deal with this question is to recompute the results with a smaller value of h . We know from HELM booklet  31 that the central difference approximations get closer and closer to the derivatives which they approximate as h decreases. In Figure 3 the results for h = 0.5 are given again as circles, and a computer has been used to find more accurate approximations to y using h = 1.5 7 (shown as squares) and yet more accurate results (shown as dots) from using h = 1.5 10 . (This involves solving larger systems of equations than are manageable by hand. The methods seen in HELM booklet  30 can be used to deal with these larger systems.)

Figure 3

No alt text was set. Please request alt text from the person who provided you with this resource.

We can now have some confidence that the results we calculated using h = 0.5 tended to overestimate the true values of y .

Example 2

Let y = y ( x ) be a solution to the boundary value problem

y ( x ) + 2 y ( x ) 2 y ( x ) = 3 0 < x < 2

y ( 0 ) = 1 , y ( 2 ) = 2 .

Using a mesh width of h = 0.5 obtain a central difference approximation to the differential equation and hence find simultaneous equations satisfied by the unknowns y 1 , y 2 and y 3 .

Solution

In this case the coefficients are

p j h 2 q j = 0.5 , h 2 r j 2 p j = 2.5 , p j + h 2 q j = 1.5 , h 2 s j = 0 . 75 .

These values will be the same for all x because p , q , r and s are constants in this Example.

Hence the general stencil is

0.5 y j 1 2.5 y j + 1.5 y j + 1 = 3

Here we have h = 0.5 and our numerical solution consists of the values

y 0 = y ( 0 ) = 1 from the boundary condition y 1 y ( 0.5 ) y 2 y ( 1 ) y 3 y ( 1.5 ) y 4 = y ( 2 ) = 2 from the boundary condition

So there are three unknowns, y 1 , y 2 and y 3 . We centre the stencil at each of the corresponding x values. Putting j = 1 in the numerical stencil gives

0.5 y 0 2.5 y 1 + 1.5 y 2 = 0 . 75 .

Moving the stencil one place to the right, we put j = 2 so that

0.5 y 1 2.5 y 2 + 1.5 y 3 = 0.75

and finally we let j = 3 so that

0.5 y 2 2.5 y 3 + 1.5 y 4 = 0.75

In these three equations y 0 and y 4 are known from the boundary conditions and we move terms involving them to the right-hand side. This leads to the system of equations

2.5 1.5 0 0.5 2.5 1.5 0 0.5 2.5 y 1 y 2 y 3 = 1.25 0.75 2.25

We can find (using methods from HELM booklet  30, for example) that the solution to the system of equations in Example 2 is

y 1 = 0.39 y 2 = 0.18 y 3 = 0.94 to 2 decimal places.

Task!

Let y = y ( x ) be a solution to the boundary value problem

y ( x ) + 4 y ( x ) = 4 0 < x < 1

y ( 0 ) = 2 , y ( 1 ) = 3 .

Using a mesh width of h = 0.25 obtain a central difference approximation to the differential equation and hence find a system of equations satisfied by y j y ( j h ) , j = 1 , 2 , 3 .

In general, the central difference approximation to

p ( x ) y ( x ) + q ( x ) y ( x ) + r ( x ) y ( x ) = s ( x )

is

y j 1 p j h 2 q j + y j h 2 r j 2 p j + y j + 1 p j + h 2 q j = h 2 s j .

In this case the coefficients are

p j h 2 q j = 0.5 , h 2 r j 2 p j = 2 , p j + h 2 q j = 1.5 , h 2 s j = 0 . 25 .

These values will be the same for all x because p , q , r and s are constants in this Example.

In this case h = 0.25 and our numerical solution consists of the values

y 0 = y ( 0 ) = 2 from the boundary condition y 1 y ( 0.25 ) y 2 y ( 0.5 ) y 3 y ( 0.75 ) y 4 = y ( 1 ) = 3 from the boundary condition

So there are three unknowns, y 1 , y 2 and y 3 . We centre the stencil at each of the corresponding x values. Putting j = 1 in the numerical stencil gives

0.5 y 0 2 y 1 + 1.5 = 0.25

Moving the stencil one place to the right, we put j = 2 so that

0.5 y 1 2 y 2 + 1.5 = 0.25

and finally we let j = 3 so that

0.5 y 2 2 y 3 + 1.5 = 0.25

In these three equations y 0 and y 4 are known from the boundary conditions and we move terms involving them to the right-hand side. This leads to the system of equations

2 1.5 0 0.5 2 1.5 0 0.5 2 y 1 y 2 y 3 = 1.25 0.25 4.25

(And you might like to check that the solution to this system of equations is y 1 = 0.95 , y 2 = 2.1 and y 3 = 2.65 .)

Example 3

The temperature y of an electrically heated wire of length &ell; is affected by local air currents. This situation may be modelled by

d 2 y d x 2 = a b ( Y y ) , ( 0 < x < &ell; ) .

Consider the case where &ell; = 3 , a = 50 , b = 0.1 and Y = 2 0 &compfn; C and suppose that the ends of the wire are known (to 1 decimal place) to be at temperatures y ( 0 ) = 15 . 0 &compfn; C and y ( 3 ) = 25 . 0 &compfn; C.

Using a central difference to approximate the derivative and using 3 subintervals obtain approximations to the temperature 1 3 and 2 3 of the length along the wire.

Solution

This Example falls into the general case given at the beginning of this Section if we choose p = 1 , q = 0 , r = 0.1 and s = 52 . In this case h = 1 and our numerical solution consists of the values

y 0 = y ( 0 ) = 15 from the boundary condition y 1 y ( 1 ) y 2 y ( 2 ) y 3 = y ( 3 ) = 25 from the boundary condition

So there are two unknowns, y 1 and y 2 . We centre the stencil at each of the corresponding x values. Putting j = 1 in the numerical stencil gives

y 0 2.1 y 1 + y 2 = 52

Moving the stencil one place to the right, we put j = 2 so that

y 1 2.1 y 2 + y 3 = 52

In these two equations y 0 and y 3 are known from the boundary conditions and we move terms involving them to the right-hand side. This leads to the system of equations

2.1 1 1 2.1 y 1 y 2 = 67 77

Solving this pair of simultaneous equations we find that

y 1 = 63.8 , y 2 = 67.1

to 1 decimal place.

We conclude that the temperature 1 3 of the wire’s length from the cooler end is approximately 63 . 8 &compfn; C and the temperature the same distance from the hotter end is approximately 67 . 1 &compfn; C, where we have rounded these numbers to the same number of places as the given boundary conditions.

The Examples and Task above were such that p , q , r and s were each equal to a constant for all values of x . More realistic engineering applications may involve coefficients that vary, and the next Example is of this type.

Example 4

Let y = y ( x ) be a solution to the boundary value problem

ln ( 2 + x ) y ( x ) + x y ( x ) + ( x + 1 ) 2 y ( x ) = cos ( x ) 0 < x < 1.2

y ( 0 ) = 0 , y ( 1.2 ) = 2 .

Using a mesh width of h = 0.4 obtain a central difference approximation to the differential equation and hence find y j y ( j h ) , j = 1 , 2 .

Solution

In general, the central difference approximation to

p ( x ) y ( x ) + q ( x ) y ( x ) + r ( x ) y ( x ) = s ( x )

is

y j 1 p j h 2 q j + y j h 2 r j 2 p j + y j + 1 p j + h 2 q j = h 2 s j .

The coefficients will vary with j in this Example because the functions p , q , r and s are not all constants. In this case h = 0.4 and our numerical solution consists of the values

y 0 = y ( 0 ) = 0 from the boundary condition y 1 y ( 0.4 ) y 2 y ( 0.8 ) y 3 = y ( 1.2 ) = 2 from the boundary condition

So there are two unknowns, y 1 and y 2 . We centre the stencil at each of the corresponding x values. Putting j = 1 in the numerical stencil gives

0.795469 y 0 1.437337 y 1 + 0.955469 y 2 = 0.147370

Moving the stencil one place to the right, we put j = 2 so that

0.869619 y 1 1.540839 y 2 + 1.189619 y 3 = 0.111473

In these two equations y 0 and y 3 are known from the boundary conditions and we move terms involving them to the right-hand side. This gives the pair of equations

1.437337 0.955469 0.869619 1.540839 y 1 y 2 = 0.147370 2.267766

Solving this pair of simultaneous equations we find that

y 1 = 1.40 , y 2 = 2.26

to 2 decimal places.

Once again we can monitor the accuracy of the results obtained in the Example above by recomputing for a smaller value of h . In Figure 4 the values calculated are shown as circles and a computer has been used to obtain the more accurate results (shown as dots) obtained from choosing h = 1.2 20 .

Figure 4

No alt text was set. Please request alt text from the person who provided you with this resource.

In the next Example we see that the derivative y appears in the boundary condition at x = 0 . This means that y is not given at x = 0 and we use the general rule given earlier in Key Point 3:

centre the stencil at every x -value where y is unknown.

So this implies that we must centre the stencil at x = 0 and this will cause the value y 1 to appear. This is a fictitious value that plays no part in the solution we seek and we use the derivative boundary condition to get y 1 in terms of y 1 . This is done with the central difference

y ( 0 ) y 1 y 1 2 h .

The following Example implements this idea.

Example 5

Let y = y ( x ) be a solution to the boundary value problem

ln ( 2 + x ) y ( x ) + x y ( x ) + 2 y ( x ) = cos ( x ) 0 < x < 1.2

y ( 0 ) = 1 , y ( 1.2 ) = 2

(Note the derivative boundary condition at x = 0 .)

Using a mesh width of h = 0.4 obtain a central difference approximation to the differential equation and hence find the system of equations satisfied by y j y ( j h ) , j = 0 , 1 , 2 .

Solution

In general, the central difference approximation to

p ( x ) y ( x ) + q ( x ) y ( x ) + r ( x ) y ( x ) = s ( x )

is

y j 1 p j h 2 q j + y j h 2 r j 2 p j + y j + 1 p j + h 2 q j = h 2 s j .

The coefficients vary with j in this Example because the functions p , q , r and s are not all constants. In this case h = 0.4 and our numerical solution consists of the values

y 0 y ( 0 ) which is not given by the boundary condition y 1 y ( 0.4 ) y 2 y ( 0.8 ) y 3 = y ( 1.2 ) = 2 from the boundary condition

So there are three unknowns, y 0 , y 1 and y 2 . We centre the stencil at each of the corresponding x values. Putting j = 0 in the numerical stencil gives

0.693147 y 1 1.066294 y 0 + 0.6931472 y 1 = 0.16

which introduces the fictitious quantity y 1 . We attach a meaning to y 1 on using the boundary condition at x = 0 . Approximating the derivative in the boundary condition by a central difference gives

y 1 y 1 2 h = 1 y 1 = y 1 + 0.8

and we use this to remove y 1 from the equation where it first appeared. Hence

1.066294 y 0 + 1.386294 y 1 = 0.7145177

The remaining steps are similar to previous Examples. Putting j = 1 in the stencil gives

0.795469 y 0 1.430937 y 1 + 0.955469 y 2 = 0 . 147370 .

Moving the stencil one place to the right, we put j = 2 so that

0.869619 y 1 1.739239 y 2 + 1.189619 y 3 = 0.111473

In the last equation y 3 is known from the boundary conditions and we move the term involving it to the right-hand side. This leaves us with the system of equations

1.066294 1.386294 0 0.795469 1.430937 0.955469 0 0.869619 1.739239 y 0 y 1 y 2 = 0.714518 0.147370 2.267766

where the components are given to 6 decimal places.

Exercises
  1. Let y = y ( x ) be a solution to the boundary value problem

    y ( x ) 2 y ( x ) + 3 y ( x ) = 6 0 < x < 0.75

    y ( 0 ) = 2 , y ( 0.75 ) = 1

    Using a mesh width of h = 0.25 obtain a central difference approximation to the differential equation and hence find y 1 y ( 0.25 ) and y 2 y ( 0.5 ) .

  2. Let y = y ( x ) be a solution to the boundary value problem

    2 y ( x ) + 3 y ( x ) = 5 0 < x < 1.2

    y ( 0 ) = 2 , y ( 1.2 ) = 3

    Using a mesh width of h = 0.3 obtain a central difference approximation to the differential equation and hence find a system of equations satisfied by y 1 y ( 0.3 ) , y 2 y ( 0.6 ) and y 3 y ( 0.9 ) .

  3. Let y = y ( x ) be a solution to the boundary value problem

    y ( x ) + x 2 y ( x ) + 3 y ( x ) = x 0 < x < 1.5

    y ( 0 ) = 2 , y ( 1.5 ) = 1 . (Note the derivative boundary condition at x = 0 .)

    Using a mesh width of h = 0.5 obtain a central difference approximation to the differential equation and hence find the system of equations satisfied by y j y ( j h ) , j = 0 , 1 , 2 .

  1. In general, the central difference approximation to

    p ( x ) y ( x ) + q ( x ) y ( x ) + r ( x ) y ( x ) = s ( x )

    is

    y j 1 p j h 2 q j + y j h 2 r j 2 p j + y j + 1 p j + h 2 q j = h 2 s j .

    In this case the coefficients are

    p j h 2 q j = 1.25 , h 2 r j 2 p j = 1.8125 , p j + h 2 q j = 0.75 , h 2 s j = 0 . 375 .

    These values will be the same for all x because p , q , r and s are constants in this Example.

    In this case h = 0.25 and our numerical solution consists of the values

    y 0 = y ( 0 ) = 2 from the boundary condition y 1 y ( 0.25 ) y 2 y ( 0.5 ) y 3 = y ( 0.75 ) = 1 from the boundary condition

    So there are two unknowns, y 1 and y 2 . We centre the stencil at each of the corresponding x values. Putting j = 1 in the numerical stencil gives

    1.25 y 0 1.8125 y 1 + 0.75 y 2 = 0.375

    Moving the stencil one place to the right, we put j = 2 so that

    1.25 y 1 1.8125 y 2 + 0.75 y 3 = 0.375

    In these two equations y 0 and y 3 are known from the boundary conditions and we move terms involving them to the right-hand side. This leads to the system of equations

    1.8125 0.75 1.25 1.8125 y 1 y 2 = 2.125 0.375

    with solution

    y 1 = 1.76 , y 2 = 1.42

    to 2 decimal places.

  2. In general, the central difference approximation to

    p ( x ) y ( x ) + q ( x ) y ( x ) + r ( x ) y ( x ) = s ( x )

    is

    y j 1 p j h 2 q j + y j h 2 r j 2 p j + y j + 1 p j + h 2 q j = h 2 s j .

    In this case the coefficients are

    p j h 2 q j = 1.55 , h 2 r j 2 p j = 4 , p j + h 2 q j = 2.45 , h 2 s j = 0 . 45 .

    These values will be the same for all x because p , q , r and s are constants in this Example.

    In this case h = 0.3 and our numerical solution consists of the values

    y 0 = y ( 0 ) = 2 from the boundary condition y 1 y ( 0.3 ) y 2 y ( 0.6 ) y 3 y ( 0.9 ) y 4 = y ( 1.2 ) = 3 from the boundary condition

    So there are three unknowns, y 1 , y 2 and y 3 . We centre the stencil at each of the corresponding x values. Putting j = 1 in the numerical stencil gives

    1.55 y 0 4 y 1 + 2.45 = 0.45

    Moving the stencil one place to the right, we put j = 2 so that

    1.55 y 1 4 y 2 + 2.45 = 0.45

    and finally we let j = 3 so that

    1.55 y 2 4 y 3 + 2.45 = 0.45

    In these three equations y 0 and y 4 are known from the boundary conditions and we move terms involving them to the right-hand side. This leads to the system of equations

    4 2.45 0 1.55 4 2.45 0 1.55 4 y 1 y 2 y 3 = 3.55 0.45 6.9

  3. In general, the central difference approximation to

    p ( x ) y ( x ) + q ( x ) y ( x ) + r ( x ) y ( x ) = s ( x )

    is

    y j 1 p j h 2 q j + y j h 2 r j 2 p j + y j + 1 p j + h 2 q j = h 2 s j .

    The coefficients vary with j in this exercise because the functions p , q , r and s are not all constants. In this case h = 0.5 and our numerical solution consists of the values

    y 0 y ( 0 ) which is not given by the boundary condition y 1 y ( 0.5 ) y 2 y ( 1 ) y 3 = y ( 1.5 ) = 1 from the boundary condition

    So there are three unknowns, y 0 , y 1 and y 2 . We centre the stencil at each of the corresponding x values. Putting j = 0 in the numerical stencil gives

    y 1 1.25 y 0 + y 1 = 0

    which introduces the fictitious quantity y 1 . We attach a meaning to y 1 on using the boundary condition at x = 0 . Approximating the derivative in the boundary condition by a central difference gives

    y 1 y 1 2 h = 2 y 1 = y 1 2

    and we use this to remove y 1 from the equation where it first appeared. Hence

    1.25 y 0 + 2 y 1 = 2

    The remaining steps are similar to previous exercises. Putting j = 1 in the stencil gives

    0.9375 y 0 1.25 y 1 + 1.0625 y 2 = 0.125

    Moving the stencil one place to the right, we put j = 2 so that

    0.75 y 1 1.25 y 2 + 1.25 y 3 = 0.25

    In the last equation y 3 is known from the boundary conditions and we move the term involving it to the right-hand side. This leads to the system of equations

    1.25 2 0 0.9375 1.25 1.0625 0 0.75 1.25 y 0 y 1 y 2 = 2 0.125 1