Taylor Series Notes
Taylor Series Notes
August 7, 2015
An introduction to the concept of a Taylor series and how these are used in
numerical analysis to find numerical approximations and estimate their accuracy.
This is a series of four short videos to accompany the printed notes. You can
download the printed notes and fill parts in as we go along. Alternatively, you
can work through the notes without the videos.
1 Taylor Series
• Many functions can be expressed as Taylor series
• Taylor series are infinite polynomials
6
ex
5 1
1 +x
4 1 + x + x2!2
1 + x + x2!2 + x3!3
3
1 + x + x2!2 + x3!3 + x4!4
2
2
2.0 1.5 1.0 0.5 0.0 0.5 1.0 1.5 2.0
2
For example a sine wave:
1.5
0.5
1.0
1.5
6 4 2 0 2 4 6
The more terms you include, the more accurate it should get.
1.5
square wave
1.0
0.5
0.0
0.5
1.0
1.5
6 4 2 0 2 4 6
4
Functions with discontinuities in some of their derivatives cannot be expressed as
Taylor series:
(
1
/2 (1 + cos x) |x| < π
Eg. cosine bell, f (x) =
0 otherwise
1.5
cosine bell
1−12 x2
1.0 2!
1−12 x2 + 12 x4
2! 4!
1−12 x2 + 12 x4 −1 x6
2! 4! 2 6!
0.5
0.0
0.5
6 4 2 0 2 4 6
6
1.1 Exercises (answers at end)
1. Write down the infinite Taylor series for function f at position x + ∆x about x
2. Write the third order approximation for f (x − ∆x) in terms of f (x), f 0 (x) and f 00 (x). Write
the error term using the O (∆xn ) notation.
3. Write the third order approximation for f (x + ∆x) in terms of f (x − ∆x), f 0 (x − ∆x) and
f 00 (x − ∆x).
2 Numerical Differentiation
Consider a set of points, x0 , x1 , · · · x j , · · · xn where x j = j∆x (the points are distance ∆x apart).
Assume that we know the value of the function f (x) at these points, as shown in figure 1.
f f j−1 fj
f j+1
x0 x1 x2 ∆x x j−1 xj x j+1 x
Figure 1: Values of a function f at points x0 , x1 , · · · , x j , · · · .
3.1 Example
1. Assume that we know f j = f (x j )
f j−1 = f (x j−1 ) = f (x − ∆x)
f j+1 = f (x j+1 ) = f (x + ∆x)
2. and we want to find f j0 .
3. For 3 knowns we wonder if we can get second order accuracy
0 0
4. We do not want to generate too many unknowns. We don’t know f j−1 or f j+1 so no Taylor
series about x j−1 or x j+1 . So let’s try Taylor series for f j+1 and f j−1 about x j
∆x2 00 ∆x3 000
f j+1 = f j + ∆x f j0 + f + f + O(∆x4 )
2! j 3!
∆x2 00 ∆x3 000
f j−1 = f j − ∆x f j0 + f − f + O(∆x4 )
2! j 3!
10
3.2 Exercises (answers at the end)
1. Use the Taylor series to find an approximation for f j0 in terms of f j and f j−1 . What order
accuracy is it?
2. Derive an uncentred, second order difference formula for f j0 that uses f j , f j+1 and f j+2 .
(And show that it is second order accurate)
3. Find an uncentred approximation for f j00 using f j , f j+1 and f j+2 . What order accurate is it?
4. Derive a second order approximation for fb0 from fa , fb and fc at x locations a < b < c
when the grid spacing is not regular. (And show that it is second order accurate)
11
10-2
An Example:
fi
• Function f is known at points x1 and x2
(values f1 and f2 ) f1
• We want to estimate the value of f at point
xi in between x1 and x2 x1 xi x2
x
β∆x (1−β)∆ x
∆x
• Exercise: Use linear interpolation (ie assume that fi lies on a straight line between f1 and
f2 ): to find f at xi
Hint: First write down expressions for ∆x, β and the gradient, f 0 between x1 and x2 . Then
find an expression for f at xi along the straight line between x1 and x2 .
xi − x1 f2 − f1
∆x = x2 − x1 β= f0 = =⇒ fi = (1 − β ) f1 + β f2
x2 − x1 x2 − x1
• If f is known at n points then a polynomial of degree n − 1 can be fit to estimate f
5.1 Finding Finite Difference Formulae for Interpolation using Taylor Series
An Example:
Assume that we know f j = f (x j ) and f j+1 = f (x j+1 ) and we want to find the interpolated
value, f j+1/2 , mid-way between x j and x j+1 .
• Start by writing down Taylor series for f j and f j+1 about f j+1/2
∆x 0 1 ∆x 2 00 1 ∆x 3 000
f j+1 = f j+1/2 + f j+1/ + f j+1/ + f j+1/ + O(∆x)4
2 2 2! 2 2 3! 2 2
2 3
∆x 0 1 ∆x 00 1 ∆x 000 4
f j = f j+1/2 − f j+1/ + f j+ 1/ − f j+ 1/ + O(∆x)
2 2 2! 2 2 3! 2 2
0
• Eliminate the largest unknown, f j+ 1/ by adding the two equations
2
∆x2 00
f j + f j+1 = 2 f j+1/2 + f 1 + O(∆x)4
4 j+ /2
• Rearrange to find f j+1/2 and express the error based on the largest unknown
f j+1/2 = ( f j + f j+1 )/2 + O(∆x)2
• So this is a second-order accurate approximation
14
5.2 Exercises (answers at the end)
1. Derive a centred, second order difference interpolation formula for f j that uses f j−1 and
f j+1 . (And show that it is second order accurate)
0
2. Derive a centred fourth order difference formula for f j+1/ that uses f j−1 , f j , f j+1 and f j+2 .
2
(And show that it is fourth order accurate)
3. Show that the first order forward difference formula for f j0 is exact for linear functions,
f (x) = ax + b.
4. Show that the centred second order difference formula for f j0 is exact for quadratic func-
tions f (x) = ax2 + bx + c.
15
Solutions
Solutions to Exercises 1.1
1. Write down the infinite Taylor series for function f at position x + ∆x about x
∆x2 00 ∆x3 000 ∆x j ( j)
f (x + ∆x) = f (x) + ∆x f 0 (x) + f (x) + f (x) + · · · + f (x) + · · ·
2! 3! j!
2. Write the third order approximation for f (x − ∆x) in terms of f (x), f 0 (x) and f 00 (x). Write
the error term using the O (∆xn ) notation.
∆x2 00
f (x) + O ∆x3
f (x − ∆x) = f (x) − ∆x f 0 (x) +
2!
3. Write the third order approximation for f (x + ∆x) in terms of f (x − ∆x), f 0 (x − ∆x) and
f 00 (x − ∆x).
4∆x3 000
f (x + ∆x) = f (x − ∆x) + 2∆x f 0 (x − ∆x) + 2∆x2 f 00 (x − ∆x) + f (x − ∆x) + O ∆x3
3
Solutions to Exercises 3.2
1. Use the Taylor series to find an approximation for f j0 in terms of f j and f j−1 . What order
accuracy is it?
Write the Taylor series for f j−1 in terms of f j :
f j−1 = f j − ∆x f j0 + O(∆x2 )
Rearrange to find f j0 :
f j0 = ( f j − f j−1 )/∆x + O(∆x)
Note dividing O(∆x2 ) by ∆x gives O(∆x) so the approximation is first order accurate
2. Derive an uncentred, second order difference formula for f j0 that uses f j , f j+1 and f j+2 .
(And show that it is second order accurate)
16
Taylor approximations for f j+1 and f j+2 about f j :
∆x2 00 ∆x3 000 ∆x4 0000
f j+1 = f j + ∆x f j0 + f + f + f + O(∆x5 )
2! j 3! j 4! j
4∆x3 000 2∆x4 0000
f j+2 = f j + 2∆x f j0 + 2∆x2 f j00 + f + f + O(∆x5 )
3 j 3 j
Eliminate the largest unknown, f j00 by calculating f j+2 − 4 f j+1 :
f j+2 − 4 f j+1 = −3 f j − 2∆x f j0 + O(∆x3 )
Rearrange to find f j0 :
f j0 = − f j+2 + 4 f j+1 − 3 f j /(2∆x) + O(∆x2 )
3. Find an uncentred approximation for f j00 using f j , f j+1 and f j+2 . What order accurate is it?
Taylor approximations for f j+1 and f j+2 about f j :
∆x2 00 ∆x3 000 ∆x4 0000
f j+1 = f j + ∆x f j +
0
fj + fj + f j + O(∆x5 )
2! 3! 4!
2 00 4∆x3 000 2∆x4 0000
0
f j+2 = f j + 2∆x f j + 2∆x f j + fj + f j + O(∆x5 )
3 3
Eliminate the largest unknown, f j0 by calculating f j+2 − 2 f j+1 :
f j+2 − 2 f j+1 = − f j − ∆x2 f j00 + O(∆x3 )
Rearrange to find f j00 :
f j00 = − f j+2 + 2 f j+1 − f j /∆x2 + O(∆x)
4. Derive a second order approximation for fb0 from fa , fb and fc at x locations a < b < c
when the grid spacing is not regular. (And show that it is second order accurate)
Define ∆x1 = b − a and ∆x2 = c − b and ∆x = max (∆x1 , ∆x2 )
Taylor approximations for fa and fc about fb :
∆x2 ∆x3 ∆x4
fa = fb − ∆x1 fb0 + 1 fb00 − 1 fb000 + 1 fb0000 + O(∆x15 )
2! 3! 4!
17
19