Week 5: Exercises#

Note

  • The terms degree and order of polynomials are used synonymously. We will mostly use degree.

  • The terms Taylor polynomial and approximating polynomial are also used synonymously. We will use both terms.

Exercises – Long Day#

1: Approximating Polynomials. By Hand#

Question a#

Find the first- and second-degree Taylor polynomials for each of the following functions with the expansion point \(x_0=0\).

  1. \(f(x)=\operatorname{e}^x,\quad x\in \mathbb{R}\)

  2. \(f(x)=\cos (x), \quad x\in \mathbb{R}\)

  3. \(f(x)=\operatorname{e}^{\sin( x)},\quad x\in \mathbb{R}\)

Question b#

Consider the function \(f: \,\,]0,\infty[\,\, \to \mathbb{R}\) given by:

\[\begin{equation*} f(x)=\frac{1}{x}. \end{equation*}\]

Can we perform a Taylor expansion of the function from the expansion point \(x_0 = 0\)? Determine the first- and second-degree approximating polynomials for \(f\) with the expansion point \(x_0 = 1\).

Question c#

Plot the four functions together with their respective first- and second-degree approximating polynomials.

2: Investigation of Taylor Polynomials#

Use SymPy to find the approximating polynomial of degree 9, \(P_9(x)\), from the expansion point \(x_0 = 0\) for the function \(\sin(x)\). Plot \(\sin(x)\) and \(P_9(x)\) in the same coordinate system. How far to the sides can the approximating polynomial follow the function? (Experiment with the polynomial’s degree).

3: Error Estimation#

A function \(f:\operatorname{dom}(f) \to \mathbb{R}\), \(\operatorname{dom}(f) \subseteq \mathbb{R}\), is given by

\[\begin{equation*} f(x)=\sqrt{2x-1}. \end{equation*}\]

Question a#

State the largest possible domain \(\operatorname{dom}(f)\) of \(f\).

Question b#

Find the approximating polynomial \(P_3(x)\) of degree \(3\) for \(f\) with the expansion point \(x_0=1\).

Question c#

Show that the remainder function \(R_3(x)\) belonging to \(P_3(x)\) can be expressed as

\[\begin{equation*} R_3(x)=-\frac{5}{8} \frac 1{(2\xi-1)^{7/2}} (x-1)^4 \end{equation*}\]

for some \(\xi\) between \(1\) and \(x\). Demonstrate by estimating the remainder function value that the numerical value of the error incurred by using \(P_3(3/2)\) instead of \(f(3/2)\) is less than or equal to \(\displaystyle{\frac 5{2^7}}\).

Note

The point of tasks like this is that you can find an approximation to a function value for a difficult function simply by using a polynomial where the value is easy to compute. At the same time, purely by hand, you can establish an upper bound for the error that is incurred when using the approximation. We are not aiming for the precise size of the error, as it is just as difficult to calculate as the true function value itself!

4: Approximation of Complex Function#

Approximating polynomials for complex functions of a real variable are constructed using the same formula as for real functions of a real variable. In the following, we consider the function \(f: \mathbb{R} \to \mathbb{C}\) given by:

\[\begin{equation*} f(x)=2\cos(x)+i\sin(2x), \quad x\in \mathbb{R}. \end{equation*}\]

Question a#

Determine the approximating polynomial \(P_3\) of degree three for \(f\) with the expansion point \(x_0=0\).

Question b#

Determine, preferably with the help of SymPy’s series command, the approximating polynomial \(Q_3\) of degree at most three for \(f\) with the expansion point \(x_1 = \frac{\pi}{2}\).

Question c#

The number 1 is closer to \(x_1 = \frac{\pi}{2}\) than to \(x_0 = 0\). Why might it still a much better idea to use \(P_3\) rather than \(Q_3\) if you need an approximated value for \(f(1)\)?

Question d#

Determine the smallest degree \(n\) for a Taylor polynomial with the expansion point \(x_0 = 0\), such that the difference between \(f(1)\) and the polynomial’s value at \(x = 1\) is less than one hundredth.

5: Approximation of Function with 3 Variables#

Given function:

\[\begin{equation*} f(x,y,z)=\operatorname{e}^{(x+1)yz}. \end{equation*}\]

Question a#

Determine the approximating polynomial of degree two, \(P_2(x,y,z)\), for the function \(f(x,y,z)\) at \((x_0,y_0,z_0)=(0,0,0)\).

Question b#

Use \(P_2(x,y,z)\) to calculate an approximated value of \(f(0.1,0.2,0.3)\).

Question c#

Calculate the absolute error that was incurred with the use of your approximation in Question b.

6: An Implementation of Taylor Approximation#

We will now take a look at an implementation of Taylor polynomials using Sympy/Python.

Question a#

Implement Taylor polynomials for arbitrary \(K> 0\) – see this equation in the textbook – using the following code structure.

def taylor(f,K,x0):
    Pk = 0
    for k in range(K+1):
        # Calculate the k'th derivative of f at x0
        fk = # ???
        # Add the k'th term to the approximation
        Pk += # ???
    return Pk

Question b#

Use your Python function from the previous question to calculate the approximating polynomial of degree 4, \(P_4(x)\), for the function \(f(x)=\sin(x)\) at \(x_0=2\pi/3\).

Question c#

Plot \(f(x)\) and \(P_4(x)\) from question b in the same window with Sympy/Python.

Question d#

Now we would like to implement the second-order Taylor approximation for functions of multiple variables, according to the definition in the textbook. For evaluating matrices and functions in SymPy/Python, you can use the following simple implementation (or built-in methods). Consider why this works.

def evaluateFunction(f,x,x0):
    for i in range(len(x)):
        f = f.subs({x[i]:x0[i]})
    return f

Then use the following code structure to implement the approximation.

def taylorN(f,x,x0):
    N = len(x)
    # Constant term
    const = # Use evaluateFunction to evalute f at x0
    # First-degree term
    J = # Find the Jacobian matrix
    J0 = # Use evaluateFunction to evaluate J at x0
    first = # Calculate the entire second term
    # Second-degree term
    H = # Find the Hessian matrix
    H0 = # Use evaluateFunction to evaluate H at x0
    second = # Calculate the entire last term
    # Result
    Pk = simplify(Matrix([const]) + first + second)[0]
    return Pk

Question e#

Given the function:

\[\begin{equation*} f(\pmb{x})=\exp(x_1x_2)+x_2\sin(x_3). \end{equation*}\]

Determine the second-degree approximating polynomial using your implementation in question f with the expansion point \(\pmb{x}_0 = (3, 0, -\pi/2)\).

Question f#

Now determine both the absolute error (the numerical error) and the relative error (the error as a percentage) incurred from using the approximation from question e to find function values at the points:

\[\begin{equation*} \pmb{y}_1 = (3.1,0.2,-\pi/2),\quad \pmb{y}_2 = (2.9,-0.2,-\pi/2). \end{equation*}\]

7: A Second-Degree Taylor Polynomial is a Quadratic Form#

We consider a function \(f: \mathbb{R}^n \to \mathbb{R}\) whose first- and second-order partial derivatives exist at the point \(\pmb{x}_0\). Show that the second-degree Taylor polynomial \(P_{2,f,\pmb{x}_0}(\pmb{x})\), defined in the textbook, is a quadratic form \(q\).

In the quadratic form, you should express the matrix \(A\), the column vector \(\pmb{b}\), and the constant \(c\) in terms of the vector \(\pmb{x}_0\) as well as of \(f\) and its partial derivatives of the first and second order (in matrix form, so as the gradient vector and Hessian matrix) evaluated at the point \(\pmb{x}_0\).


Exercises – Short Day#

Note

The name Taylor’s limit formula is used for “Taylor’s formula” (see the textbook) where \(f(x)\) is isolated on the left-hand side and the approximating polynomial and remainder term are collected and expressed using an epsilon function on the right-hand side.

1: Function of One Variable#

Question a#

Determine with the expansion point \(x_0=0\) Taylor’s limit formula of order 2 for the function:

\[\begin{equation*} f(x)=2\cos(x)-2\sin(2x), \quad x\in \mathbb{R}. \end{equation*}\]

Question b#

A twice-differentiable function \(f\) of one variable satisfies that \(f(2) = 1\), \(f'(2) = 1\), and \(P_2(1) = 1\). Determine the second-degree approximating polynomial \(P_2(x)\) for \(f\) with the expansion point \(x_0 = 2\).

2: Taylor’s Limit Formula. By Hand#

This exercise provides a method to calculate the limit of a fraction where both the numerator and the denominator approach zero.

Question a#

Write down Taylor’s limit formula for the function \(\ln(1+x)\) with the expansion point \(x_0 = 0\) for order 1, as well as for orders 2 and 3.

Question b#

Which of the three results in question a cannot be used to find the limit value

\[\begin{equation*} \lim_{x\to 0}\frac{\ln(1+x)-x}{x^2}? \end{equation*}\]

Question c#

Now compute, using Taylor’s limit formula, the following limit value:

\[\begin{equation*} \lim_{x\to 0}\frac{x(\operatorname{e}^x+1)-2(\operatorname{e}^x-1)}{x^3}. \end{equation*}\]

3: Trick Exercise#

A function \(f \in C^{\infty}(\mathbb{R}^2)\) satisfies the equations

\[\begin{equation*} f(x,0)=\operatorname{e}^x\quad\text{and}\quad f'_y(x,y)=2y\cdot f(x,y). \end{equation*}\]

Find the approximating polynomial of degree 2 for the function \(f\) with \((x_0,y_0)=(0,0)\) as the expansion point.

4: Taylor’s Formulas and Approximations#

We are given the function \(f: \mathbb{R}^2 \to \mathbb{R}\) by:

\[\begin{equation*} f(x,y)=\operatorname{e}^{x+xy-2y}. \end{equation*}\]

Question a#

Write the second-degree Taylor polynomial for \(f\) with the expansion point \((x_0, y_0) = (0, 0)\) in its fully expanded form (its scalar form without vectors and matrices).

Question b#

Determine the gradient \(\nabla f(0,0)\) and the Hessian matrix \(\pmb{H}_f(0,0)\), and write down the second-degree Taylor polynomial for \(f\) with the expansion point \((x_0, y_0) = (0, 0)\) in matrix form.

Question c#

We now want an approximated value for \(f\left(\frac{3}{4}, \frac{1}{2}\right)\) based on a second-degree approximating polynomial for \(f\). It is of course easy to just use the second-degree approximating polynomial with the expansion point \((0, 0)\), which we have from the first question. On the other hand, \(\left(\frac{3}{4}, \frac{1}{2}\right)\) is a bit closer to \((1, 1)\), where it is also relatively convenient to expand from. So perhaps it would be better to use \((1, 1)\) as the expansion point? What difference would that make?

Determine the second-degree approximating polynomials, \(P_2(x, y)\) and \(Q_2(x, y)\), for \(f\) with the expansion points \((0, 0)\) and \((1, 1)\), respectively. Determine their values at the point \(\left(\frac{3}{4}, \frac{1}{2}\right)\) and compare with a computer value of \(f\left(\frac{3}{4}, \frac{1}{2}\right)\).

5: Application of Approximating Polynomial#

A function \(f:\mathbb{R}^2 \to \mathbb{R}\) is given by

\[\begin{equation*} f(x,y)=\sqrt{x^2+y^2}. \end{equation*}\]

Question a#

Determine the approximating polynomial \(P_2(x,y)\) of degree 2 for \(f\) from the expansion point \((x_0,y_0)=(3,4)\).

Question b#

In this question, we will illustrate the error that we incur if we use the approximating second-degree polynomial instead of the exact value.

Determine, using the result from question a, the length of the diagonal in a rectangle with side lengths 2.9 and 4.2 (feel free to use SymPy for the calculation).

Question c#

Compare with a computer value (e.g., from Sympy) of the diagonal length.

Question d#

Is the difference significant?