Homework 9 Solutions

CS 3513 - Numerical Analysis
Homework #9 - 2006.11.08
Due Date - 2006.11.17
Solutions
Show that the following finite-difference formulas are all O(h4 ).
1. 12hf ′ (x) ≈ f (x − 2h) − 8f (x − h) + 8f (x + h) − f (x + 2h)
2. 12hf ′ (x) ≈ −3f (x − h) − 10f (x) + 18f (x + h) − 6f (x + 2h) + f (x + 3h)
3. 12hf ′ (x) ≈ −25f (x) + 48f (x + h) − 36f (x + 2h) + 16f (x + 3h) − 3f (x + 4h)
4. 12hf ′ (x) ≈ 3f (x + h) + 10f (x) − 18f (x − h) + 6f (x − 2h) − f (x − 3h)
5. 12hf ′ (x) ≈ 25f (x) − 48f (x − h) + 36f (x − 2h) − 16f (x − 3h) + 3f (x − 4h)
To answer all of the above problems, consider the following expansions:
9h2
27h3
81h4
− f ′′′ (x)
+ f iv (x)
+ ...
2
6
24
4h2
8h3
16h4
f (x − 2h) = f (x) − 2f ′ (x)h + f ′′ (x)
− f ′′′ (x)
+ f iv (x)
+ ...
2
6
24
2
3
4
h
h
h
f (x − h) = f (x) − f ′ (x)h + f ′′ (x) − f ′′′ (x)
+ f iv (x)
+ ...
2
6
24
f (x) = f (x)
f (x − 3h) = f (x) − 3f ′ (x)h + f ′′ (x)
h2
h3
h4
+ f ′′′ (x)
+ f iv (x)
+ ...
2
6
24
4h2
8h3
16h4
f (x + 2h) = f (x) + 2f ′ (x)h + f ′′ (x)
+ f ′′′ (x)
+ f iv (x)
+ ...
2
6
24
9h2
27h3
81h4
f (x + 3h) = f (x) + 3f ′ (x)h + f ′′ (x)
+ f ′′′ (x)
+ f iv (x)
+ ...
2
6
24
f (x + h) = f (x) + f ′ (x)h + f ′′ (x)
By using the above formulas with the prescribed values from the problems, it is easy to show that the formulas
are indeed order
1
Use the above finite-difference formulas to construct a first derivative program in Matlab. Given a function on
an interval [a, b], your program should compute the derivative using the formula from problem 1 on all points except
those near the endpoints. Near the endpoints you will have to use the formulas from the other problems. Compare
the derivative computed by finite-difference with the actual derivative on the specified interval and plot the difference.
6. f (x) = 4x3 − 12x2 − x + 1 on [0, 1] with h = 0.01
Actual Derivative (red)
Finite Difference (blue)
0
−2
−4
f’(x)
−6
−8
−10
−12
−14
0
0.2
0.4
0.6
0.8
1
0.8
1
x
−13
1.8
Error in Finite Difference Approximation
x 10
1.6
1.4
|f’−fd|
1.2
1
0.8
0.6
0.4
0.2
0
0
0.2
0.4
0.6
x
−14
4.5
x 10
Relative Error in Finite Difference Approximation
0
0.2
4
3.5
|f’−fd|/f’
3
2.5
2
1.5
1
0.5
0
0.4
0.6
x
2
0.8
1
7. f (x) = sin(x) on [0, 2π] with h = 0.02
Actual Derivative (red)
Finite Difference (blue)
1
0.8
0.6
0.4
f’(x)
0.2
0
−0.2
−0.4
−0.6
−0.8
−1
0
1
2
3
4
5
6
5
6
x
−8
3.5
Error in Finite Difference Approximation
x 10
3
2.5
|f’−fd|
2
1.5
1
0.5
0
0
1
2
3
4
x
−8
4
Relative Error in Finite Difference Approximation
x 10
3.5
|f’−fd |/f’
3
2.5
2
1.5
1
0.5
0
1
2
3
4
x
3
5
6
8. f (x) = ex on [0, 1] with h = 0.001
Actual Derivative (red)
Finite Difference (blue)
2.8
2.6
2.4
2.2
f’(x)
2
1.8
1.6
1.4
1.2
1
0.8
0
0.2
0.4
0.6
0.8
1
x
−13
7
x 10
Error in the Finite Difference Approximation
6
5
|f’−fd|
4
3
2
1
0
0
0.2
0.4
0.6
0.8
1
x
−13
2.5
x 10
Relative Error in Finite Difference Approximation
0
0.2
2
|f’−fd|/f’
1.5
1
0.5
0
0.4
0.6
x
4
0.8
1
9. f (x) = xex on [0, 2] with h = 0.01
Actual Derivative (red)
Finite Difference (blue)
25
20
f’(x)
15
10
5
0
0
0.5
−7
1.4
1
x
1.5
x 10
Error in Finite Difference Approximation
0
0.5
2
1.2
1
|f’−fd|
0.8
0.6
0.4
0.2
0
−8
1.2
x 10
1
x
1.5
2
Relative Error in Finite Difference Approximation
1
|f’−fd|/f’
0.8
0.6
0.4
0.2
0
0
0.5
1
x
5
1.5
2