Beruflich Dokumente
Kultur Dokumente
and
Finite-difference Equations
Outline
This topic discusses numerical solutions to BVPs:
Divided-difference approximations of the 1st and 2nd derivatives
Boundary-value Problems (BVPs)
Approximations of linear ordinary differential equations (ODEs)
using finite-difference equations
Numerical approximations to BVPs
Examples
Laplaces equation
u (1) x lim
where
x, x h
u x h u x
h
1
u (2) h
2
we get
u (1) x
u x h u x 1 (2)
u h
h
2
u x h u x
1
u (2) h
h
2
x, x h
10
u x h u x
1
M
u (2) h
h
h
2
2
11
dx
x 0.8
x 2 3 0.5 x e 0.5 x
x 0.8
1.115412557
x e
0.4 x
2.02
If h = 0.1, it follows
thatu 0.9 u 0.8
(1)
u
0.1
1.216270590
Our approximation
13
14
u (1) x
u 0.85 u 0.8
1.165850114
0.05
1 (2)
M
1
u h
h 2.02 0.05 0.0505
2
2
2
15
16
% h =
% h =
% h =
% h =
% h =
% h =
Subtractive Cancellation
The issue here is subtractive cancellation
The correct answer:
1.1154125566033037964387363874459825837
19
Subtractive Cancellation
Now, double uses 53 bits of precision:
53
15.95
log 2 (10)
0.8
0.3432038635702473
0.3432038635702473
0.0000000000000000
Subtractive Cancellation
But what happens in the binary world?
Consider the function u1(x) = x2 ex and let us approximate the
derivative at x = 1
u1(1) x
u1 x h u1 x 1 (2)
u1 h
h
2
We will use Matlab and print out the double-precision floatingpoint numbers in their binary representation
We will use h = 2n for n = 0, 1, 2, ...
The bits that are correct (taking into account rounding) are marked
in blue
The zero bits resulting from subtractive cancellation are marked in
red
21
Subtractive Cancellation
n
0
1
2
3
4
5
6
7
8
9
10
11
12
13
22
Subtractive Cancellation
n
27
28
29
30
31
32
33
34
10
0%
rel
ati
ve
err
or
35
36
37
38
39
40
23
Subtractive Cancellation
Consequence:
Unlike calculus, we cannot make h arbitrarily small
Possible solutions:
Find a better formulas
Use a completely different approach
24
Better Approximations
Idea: find the line that interpolates the two points
(x, u(x)) and (x + h, u(x + h))
25
Better Approximations
The slope of this interpolating line is our approximation
u x h u x
of the derivative:
h
26
Better Approximations
What happens if we find the interpolating quadratic going
through the three points
(x h, u(x h)) (x, u(x)) (x + h, u(x + h))
?
27
Better Approximations
The interpolating quadratic is clearly a local
approximation
28
Better Approximations
The slope of the interpolating quadratic is easy to find:
29
Better Approximations
The slope of the interpolating quadratic is also closer to
the slope of the original function at x
30
Better Approximations
Without going through the process, finding the
interpolating quadratic function gives us a similar formula
u (1) x
u x h u x h
2h
31
Better Approximations
Additionally, we can approximate the concavity (2nd
derivative) at the point x by finding the concavity of the
interpolating quadratic polynomial
u (2) x
u x h 2u x u x h
h2
32
Better Approximations
For those interested, this Maple code finds these
formulas
33
Better Approximations
Question: how much better are these two
approximations?
u x h u x h
(1)
u x
2h
u x h 2u x u x h
u (2) x
h2
34
Better Approximations
Using Taylor series, we have approximations for both
u(x + h) and u(x h):
1
1
u x h u x u (1) x h u (2) x h 2 u (3) h 3
2
6
1
1
u x h u x u (1) x h u (2) x h 2 u (3) h 3
2
6
Here, x, x h and x, x h
35
Better Approximations
Subtracting the second approximation from the first, we
get
1
1
u x h u x u (1) x h u (2) x h 2 u (3) h 3
2
6
1
1
u x h u x u (1) x h u (2) x h 2 u (3) h 3
2
6
u x h u x h
2u (1) x h
2u (1) x h
1
1
u (3) h 3 u (3) h3
6
6
1 (3)
u u (3) h3
Better Approximations
Solving the equation
u x h u x h 2u (1) x h
1 (3)
u u (3) h 3
u x h u x h 2
Mh 2
2h
3
Better Approximations
The critical term is the h2
u x h u x h 2
u (1) x
Mh 2
2h
This says
If we halve h, the error goes down by a factor of 4
If we divide h by 10, the error goes down by a factor of 100
Better Approximations
Adding the two approximations
1
1
1
u x h u x u (1) x h u (2) x h 2 u (3) x h 3 u (4) h 4
2
6
24
1
1
1
u x h u x u (1) x h u (2) x h 2 u (3) x h 3 u (4) h 4
2
6
24
u x h u x h 2u x
u (2) x h 2
2u x u (2) x h 2
1 (4)
1
u h 4 u (4) h 4
24
24
1 (4)
u u (4) h 4
24
Better Approximations
Solving the equation
u x h u x h 2u x u (2) x h 2
1 (4)
u u (4) h 4
24
h2
24
u x h 2u x u x h 1
2
Mh
h2
12
u (2) x
Better Approximations
Again, the term in the error is h2
u x h 2u x u x h
u (2) x
2
h
1
Mh 2
12
Example
We will demonstrate this by finding the approximation of
both the derivative and 2nd-derivative of u(x) = x3 e0.5x at
x = 0.8
Using Maple, the correct values to 17 decimal digits are:
u(1)(0.8) = 1.1154125566033037
u(2)(0.8) = 2.0163226984752030
Example
u x h u x
h
h
Approximation
Error
u x h 2u x u x h
h2
u x h u x h
2h
Approximation
Error
Approximation
Error
1.115614538794 2.020e-04
2.0131210165303
3.2017e-3
1.115414523411 1.9668e-6
2.016290701661
3.1997e-5
1.115412576266 1.9663e-8
2.016322378395
3.2008e-7
1.115412556800 1.9340e-10
2.016322686593
1.1882e-8
1.115412556604 9.9676e-13
2.016322109277
5.8920e-7
1.115412556651 4.8181e-11
2.016276035022
4.6663e-5
1.115412555929 6.7346e-10
2.0150547896945
1.2679e-3
1.115412559538 2.9348e-9
0.555111512313
1.4612
1.115412512353 4.4250e-8
-55.511151231258
57.5275
u(1)(0.8) = 1.1154125566033037
u(2)(0.8) = 2.0163226984752030
Better Approximations
To give names to these formulas:
First Derivative
u x h u x
h
u x h u x h
2h
Second Derivative
u x h 2u x u x h
h2
44
u (1) x
u x h u x h
2h
h2
c2
u x h u x h
c3u x g x
2h
46
The equation
2c1 hc2 u x h 2h 2c3 4c1 u x 2c1 hc2 u x h 2h 2 g x
is called the finite-difference equation approximating the
2nd-order linear ODE
47
d 2h 2 c3 4c1
d 2c1 hc2
48
Boundary-value Problems
The final step in our problem is defining and solving
boundary value problems:
2
1
c1u x c2u x c3u x g x
We could use more general equations, but we will
restrict ourselves to linear ODEs
49
Boundary-value Problems
The final step in our problem is defining and solving
boundary value problems:
2
1
c1u x c2u x c3u x g x
We note that there are two derivatives
Finding the solution requires two integrations
This requires two constraints
50
Boundary-value Problems
The final step in our problem is defining and solving
boundary value problems:
c1u 2 x c2u 1 x c3u x g x
One approach is to constrain both the value of the
function and the derivative at a single point:
u x1 u10
u 1 x1 u11
51
Boundary-value Problems
The final step in our problem is defining and solving
boundary value problems:
2
1
c1u x c2u x c3u x g x
An alternative system of constraints are two boundary
values:
u a ua
u b ub
52
Boundary-value Problems
Given these, two constrains, we are looking for a
function u(x) which equals both specified boundary
values
53
Boundary-value Problems
Not only must it satisfy the boundary values, but it must
also satisfy the ODE
54
Boundary-value Problems
Given a point x and the value u(x)
55
Boundary-value Problems
Given a point x and the value u(x), the derivative at x
56
Boundary-value Problems
Given a point x and the value u(x), the derivative at x and
the concavity at x, the linear combination must equal the
forcing function
57
Boundary-value Problems
Assuming we are looking for a numerical solution, we
cannot find the value at every point a < x < b
58
Boundary-value Problems
Instead, we will divide the interval into n equally spaced
points
59
Boundary-value Problems
Instead, we will divide the interval into n equally spaced
points
ba
h
n 1
xk a k 1 h
60
Boundary-value Problems
We will find values that approximate u(xk) at each of
these points xk
61
Boundary-value Problems
We will call the approximations u xk uk
Here u1 = ua and un = ub
62
Boundary-value Problems
The problem: solve for the values for u2 through un 1
63
Recall that:
d 2c1 hc2
d 2h 2 c3 4c1
d 2c1 hc2
64
Now, xk h = xk 1 and xk + h = xk + 1
d u xk 1 du xk d u xk 1 2h 2 g xk
65
may be approximated by
d uk 1 duk d uk 1 2h 2 g xk
66
d u1
For each interior point x2, ..., xn 1, write out the linear
equation:
du2
d u3
d u2
du3
d u3
d u4
du4
d u4
d u5
du5
d u5
d u6
du6
O
d u7
O
d un 3
dun 2
d un 2
2 h 2 g x2
2h 2 g x3
2 h 2 g x4
2h 2 g x5
2h 2 g x6
M
M
d un 1
dun 1
d un
2 h 2 g xn 2
2h 2 g xn 1
du2
d u3
d u2
du3
d u3
d u4
du4
d u4
d u5
du5
d u5
d u6
du6
O
d u7
O
d un 3
dun 2
d un 2
2 h 2 g x2
2h 2 g x3
2 h 2 g x4
2h 2 g x5
2h 2 g x6
M
M
d un 1
dun 1
d un
2 h 2 g xn 2
2h 2 g xn 1
du2
d u3
d u2
du3
d u3
d u4
du4
d u4
d u5
du5
d u5
d u6
du6
O
d u7
O
d un 3
dun 2
d un 2
2 h 2 g x2
2h 2 g x3
2 h 2 g x4
2h 2 g x5
2h 2 g x6
M
M
d un 1
dun 1
d un
2 h 2 g xn 2
2h 2 g xn 1
d u1
du2
d u3
d u2
du3
d u3
d u4
du4
d u4
d u5
du5
d u5
d u6
du6
O
d u7
O
d un 3
dun 2
d un 2
2 h 2 g x2
2h 2 g x3
2 h 2 g x4
2h 2 g x5
2h 2 g x6
M
M
d un 1
dun 1
d un
2 h 2 g xn 2
2h 2 g xn 1
du2
d u3
d u2
du3
d u3
d u4
du4
d u4
d u5
du5
d u5
d u6
du6
O
d u7
O
d un 3
dun 2
d un 2
2 h 2 g x2
2h 2 g x3
2 h 2 g x4
2h 2 g x5
2h 2 g x6
M
M
d un 1
dun 1
d un
2 h 2 g xn 2
2h 2 g xn 1
du2
d u2
d u3
du3
d u3
d u4
du4
O
d u5
O
d un 3
O
dun 2
d un 2
d un 1
dun 1
d un
2 h 2 g x2
2h 2 g x3
2 h 2 g x4
2h 2 g xn 2
2h 2 g xn 1
d ua
d ub
du2
d u2
d u3
du3
d u3
d u4
du4
O
d u5
O
d un 3
O
dun 2
d un 2
d un 1
dun 1
d un
2h 2 g x2
2h 2 g x3
2 h 2 g x4
2h 2 g xn 2
2h 2 g xn 1
d ua
d ub
2
h
g
x
d
d
d
2h g x4
d
d
d
2
h
g
x
d
d
d
u
5
intr
2
2h g x
d d
d
6
d
d
2h 2 g xn 2
2
2h g xn 1 d ub
where
uintr
u2
u3
u4
u5
u6
M
un 2
un 1
The Problem
Implement a function
function [x_out, u_out] = bvp( c, g, x_bndry,
u_bndry, n )
Here:
c = [c1 c2 c3]
g is a function handle for g(x)
x_bndry = [a, b]
u_bndry = [u_a, u_b]
x_out is a column vector of n x-values
u_out is a column vector of n u-values
76
0
0
0
3.2000
0
0
0
50
0
3.2000
0
0
0
3.2000
0
0
0
0
0
0
3.5000
4.0000
0.5000
81
Saved as g.m
1.3499
1.2000
1.6000
0.9098
0.3958
1.3499
1.2000
1.6000
0.9098
0.3958
84
85
4.2000
>> [0 u(2:end - 1) 0]
ans =
0
3.4000
3.8000
0
4.2000
4.6000
4.6000
4.2000
4.6000
87
88
>> sqr_int( [1 2 3] )
vector
??? Error using ==> sqr_int at 3
the argument 'x' is not an integer
% passing a
% passing a
Matlab Example
Consider the boundary-value problem
u 2 x 3u 1 x 2u x 0
u 0 4
u 1 5
We would call
bvp( [1 3 2], @g1, [0, 1], [4, 5], 9 )
This has the solution
u2 x
e x 5e2 4 e 2 x 4e 5e 2
e 1
Matlab Example
The matrices and vectors involved are:
x =
0
0.1250
0.2500
0.3750
0.5000
0.6250
0.7500
0.8750
1.0000
M =
-3.9375
1.6250
0
0
0
0
0
2.3750
-3.9375
1.6250
0
0
0
0
0
2.3750
-3.9375
1.6250
0
0
0
u_intr = M \ b;
u_intr =
5.1205
5.7524
6.0334
6.0668
5.9301
5.6805
5.3602
0
0
2.3750
-3.9375
1.6250
0
0
u =
0
0
0
2.3750
-3.9375
1.6250
0
4.0000
5.1205
5.7524
6.0334
6.0668
5.9301
5.6805
5.3602
5.0000
0
0
0
0
2.3750
-3.9375
1.6250
u 0 4
u 1 5
0
0
0
0
0
2.3750
-3.9375
b =
-6.5000
0
0
0
0
0
-11.8750
Matlab Example
Creating the plot, we have
>>
>>
>>
>>
plot( x, u, 'o' );
hold on
xs = linspace( 0, 1, 100 );
plot( xs, u2(xs), 'r' );
92
Matlab Example
Consider the same boundary-value problem but with a
forcing function
u 2 x 3u 1 x 2u x 4sin(2 x)
u 0 4
u 1 5
We would call
bvp( [1 3 2], @g2, [0, 1], [4, 5], 9 )
This has the solution (courtesy of Maple):
Matlab Example
The matrices and vectors involved are:
x =
0
0.1250
0.2500
0.3750
0.5000
0.6250
0.7500
0.8750
1.0000
M =
-3.9375
1.6250
0
0
0
0
0
2.3750
-3.9375
1.6250
0
0
0
0
0
2.3750
-3.9375
1.6250
0
0
0
u_intr = M \ b;
u_intr =
5.1652
5.7893
6.0113
5.9678
5.7810
5.5382
5.2790
0
0
2.3750
-3.9375
1.6250
0
0
u =
0
0
0
2.3750
-3.9375
1.6250
0
4.0000
5.1652
5.7893
6.0113
5.9678
5.7810
5.5382
5.2790
5.0000
0
0
0
0
2.3750
-3.9375
1.6250
u 0 4
u 1 5
0
0
0
0
0
2.3750
-3.9375
b =
-0.0884
-0.1250
-0.0884
-0.0000
0.0884
0.1250
0.0884
Matlab Example
Creating the plot, we have
>>
>>
>>
>>
plot( x, u, 'o' );
hold on
xs = linspace( 0, 1, 100 );
plot( xs, u3(xs), 'r' );
95
Laplaces Equation
One special case is Laplaces equation in one
dimension:
2
u x 0
u 0 4
u 1 5
This has the rather trivial solution:
A straight line connecting the boundary values
u4 x 4 x
96
Laplaces Equation
One special case is Laplaces equation in one
dimension:
2
u x 0
The important point is the finite difference equation:
or
uk 1 uk 1
2
Summary
We have looked at using finite-difference equations for
approximating boundary-value problems
1st- and 2nd-order divided-difference approximations of the
derivative
2nd-order approximation of the 2nd derivative
Boundary-value problems
Finite-difference equations approximating ODEs
The approximation of linear ODEs by a system of equations
The implementation in Matlab
Laplaces equation in one dimension
98
References
[1]
[2]
99