Part 2: Taylor Series Approximation#
Taylor series are a powerful tool to approximate functions. Throughout this week’s chapter in the text book, you have seen how Taylor series can be used to derive numerical approximations for derivatives and estimate the errors of numerical integration and differentiation techniques.
In the following exercises, we will have a closer look at how this approximation works. The visualizations you create will hopefully help you to build some intuition for the meaning of a Taylor series.
We will approximate the natural logarithm by a number of Taylor polynomials of increasing order.
Analytical Expression#
Task 2.1
This is a pen-and-paper exercise. Write down the terms first four terms of the Taylor series for \(f(x) = \ln(x)\) around \(x_0=1\).
Plotting the Taylor Polynomials#
Task 2.2
Before continuing with the Taylor series approximations, plot the expression \(f(x)=\ln(x)\) in the interval \([0, 3]\). This will be used as benchmark to assess your approximations. We will want to produce plots that will include each successive term of the Taylor approximation to see how the approximation improves as we include more terms in the Taylor series.
import math
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(### YOUR CODE HERE ###, ### YOUR CODE HERE ###, num=200)
def f(x):
    return ### YOUR CODE HERE ###
plt.plot(x, f(x))
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title("Plot of $f(x) = \\ln(x)$");
Task 2.3
Implement a Python function that evaluates the nth derivative of \(\ln(x)\) (at least up to \(n=4\)), so we can use it to compute the Taylor polynomial. Complete the function template in the code cell below.
Tip: to compute the factorial, you can use the function math.factorial from the Python standard library.
def fn(x, n):
    """Compute the nth derivative of ln(x) at point x"""
    ### YOUR CODE HERE ###
    return ### YOUR CODE HERE ###
Task 2.4
Define the expansion point \(x_0\) in Python and provide Python expressions for the Taylor polynomials of first, second, third and fourth order.
x0 = ### YOUR CODE HERE ###
taylor_1 = ### YOUR CODE HERE ###
taylor_2 = ### YOUR CODE HERE ###
taylor_3 = ### YOUR CODE HERE ###
taylor_4 = ### YOUR CODE HERE ###
Task 2.5
Fill in the code below to plot the function \(f(x)\) along with the Taylor polynomials of increasing order.
# Plot f(x) and the Taylor polynomials
plt.plot(x, f(x), label="$f(x) = \\ln(x)$", color="k")
plt.plot(x, ### YOUR CODE HERE ###, label="First Order", color="C0")
plt.plot(x, ### YOUR CODE HERE ###, label="Second Order", color="C1")
plt.plot(x, ### YOUR CODE HERE ###, label="Third Order", color="C2")
plt.plot(x, ### YOUR CODE HERE ###, label="Fourth Order", color="C3")
# Plot the expansion point x0 as a dot
plt.scatter(
    ### YOUR CODE HERE ###,
    ### YOUR CODE HERE ###,
    color="k",
    marker="o",
    label=f"Expansion Point ($x = {x0:0.2f}$)"
)
# Add labels to the plot
plt.xlabel("x")
plt.ylabel("$f(x)$")
plt.title("Taylor polynomials of $f(x) = \\ln(x)$")
plt.legend()
Task 2.6
Look at the plot you just created. Which Taylor polynomial approximates the function \(f(x)\) the best? How does depend on the distance from the expansion point \(x_0\)? You can use your findings to help answer question 2.3 in the report.
Absolute error#
To further analyze the error introduced by truncating the Taylor series, we can evaluate the absolute difference between the function \(f(x)\) and the Taylor polynomials:
where \(T_n\) refers to the Taylor polynomial of \(n\)th order.
Task 2.7
Use your Taylor series approximations and the analytic expression for \(f(x)\) to determine the absolute error. Plot the error against \(x\) and vary the \(x\)- and \(y\)-limits. Are the larger order Taylor polynomials always more accurate? You can use your findings to help answer question 2.3 in the report.
# Compute the absolute error
error_1 = ### YOUR CODE HERE ###
error_2 = ### YOUR CODE HERE ###
error_3 = ### YOUR CODE HERE ###
error_4 = ### YOUR CODE HERE ###
# Plot the absolute error against x
plt.plot(x, ### YOUR CODE HERE ###, label="First Order", color="C0")
plt.plot(x, ### YOUR CODE HERE ###, label="Second Order", color="C1")
plt.plot(x, ### YOUR CODE HERE ###, label="Third Order", color="C2")
plt.plot(x, ### YOUR CODE HERE ###, label="Fourth Order", color="C3")
# Add labels
plt.xlabel("x")
plt.ylabel("Absolute Error: $f(x)-\\mathrm{Taylor~polynomial}$")
plt.title("Absolute Error of Taylor Series Approximations")
plt.legend()
By Anna Störiko, Ronald Brinkgreve, Justin Pittman, Jaime Arriaga Garcia, Robert Lanzafame, Delft University of Technology. CC BY 4.0, more info on the Credits page of Workbook