Week 5: Exercises#
The exercises are intended to be done by hand unless otherwise stated (such as when you are asked to plot a graph or run a script). If you are being asked to find the numerical value of, e.g., a square root or an exponential function, then you may of course use Python/Sympy as a calculator.
Note
The terms degree and order of polynomials are sometimes used synonymously. We will use degree.
The terms Taylor polynomial and approximating polynomial are also used synonymously. We will use both.
Exercises – Long Day#
1: Approximating Polynomials#
Question a#
Find the first- and second-degree Taylor polynomials for each of the following functions with the expansion point \(x_0=0\).
\(f(x)=\operatorname{e}^x,\quad x\in \mathbb{R}\)
\(f(x)=\cos (x), \quad x\in \mathbb{R}\)
\(f(x)=\operatorname{e}^{\sin( x)},\quad x\in \mathbb{R}\)
Answer
\(P_1(x)=1+x\) and \(P_2(x)=1+x+\frac 12x^2\)
\(P_1(x)=1\) and \(P_2(x)=1-\frac{1}{2}x^2\)
\(P_1(x)=1+x\) and \(P_2(x)=1+x+\frac{1}{2}x^2\)
Question b#
Consider the function \(f: \,\,]0,\infty[ \,\,\to \mathbb{R}\) given by:
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\).
Answer
No, a Taylor expansion from \(x_0=0\) is not possible since \(f\) is not defined at \(x=0\). We have no way of “repairing” this missing definition since \(f\) (and \(f'\), \(f''\)) has no limit value for \(x \to 0\).
With expansion point \(x_0=1\) we find: \(P_1(x)=-x+2\) and \(P_2(x)=x^2-3x+3\).
Question c#
Plot in Python/Sympy the four functions together with their respective first- and second-degree approximating polynomials.
2: Investigation of Taylor Polynomials (Python)#
Use Python/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).
Hint
See how to determine approximating polynomials using the series command in this week’s SymPy Demo05.
Answer
The higher the degree chosen for the approximating polynomial, the larger is the interval around \(x_0 = 0\) where there is agreement between \(\sin(x)\) and the approximating polynomial \(P_K(x)\). The approximating polynomial \(P_K(x)\) can be made to follow the function \(\sin(x)\) arbitrarily far to the sides in this case. For \(n = 17\), it matches \(\sin(x)\) well in the interval \(\left[ -2\pi , 2\pi \right]\), and for \(n = 25\), the approximation is even better.
3: Error Estimation#
A function \(f:\operatorname{dom}(f) \to \mathbb{R}\), \(\operatorname{dom}(f) \subseteq \mathbb{R}\), is given by
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
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:
Question a#
Determine the approximating polynomial \(P_3\) of degree three for \(f\) with the expansion point \(x_0=0\).
Answer
\(P_3(x)=2+2\cdot i \cdot x-x^2-\frac 43 \cdot i\cdot x^3\).
Question b#
Determine the approximating polynomial \(Q_3\) of degree at most three for \(f\) with the expansion point \(x_1 = \frac{\pi}{2}\).
Question c#
Calculate \(f(1), P_3(1)\), and \(Q_3(1)\). The number \(1\) is closer to \(x_1=\frac{\pi}{2}\) than to \(x_0=0\). Why is still a better idea to use \(P_3\) than \(Q_3\) to approximate a value of \(f(1)?\)
Answer
Taylor polynomials are local. It can be difficult to know, which expansion point that is better. Here we see that \(Q_3(1)\) is a significantly better approximation of \(f(1)\) than \(P_3(1)\), but as it is rather difficult to work with powers of pi, we might still choose to work with \(P_3(x)\) depending on the use.
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.
Answer
\(P_7\) will do the job.
5: Approximation of Function with 3 Variables#
Given function:
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)\).
Answer
Question b#
Use \(P_2(x,y,z)\) to calculate an approximated value of \(f(0.1,0.2,0.3)\).
Answer
Question c#
Calculate the absolute error that was incurred with the use of your approximation in Question b. Here you may use a calculator or Python/Sympy to determine an approximate value of \(f(0.1,0.2,0.3)\).
Hint
The absolute error is given by \(|P_2(x,y,z)-f(x,y,z)|\).
Answer
6: A Second-Degree Taylor Polynomial is a Quadratic Form#
Let us 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, express the matrix \(A\), the column vector \(\pmb{b}\), and the constant \(c\) in terms of the vector \(\pmb{x}_{0}\) and \(f\) along with its (partial) derivatives of order 1 and 2 (in matrix form as gradient vector and Hessian matrix, respectively), evaluated at the point \(\pmb{x}_{0}\).
Hint
Begin by expanding all inner products in the definition of \(P_{2,f,\pmb{x}_{0}}(\pmb{x})\) using the linearity of the inner product. Gather all constant terms (those that do not depend on \(\pmb{x}\)) and set them equal to \(c\). Then continue with the linear terms in \(\pmb{x}\), and lastly the quadratic terms.
7: An Implementation of Taylor Approximation (Python)#
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
Answer
def taylor(f,K,x0):
Pk = 0
for k in range(K+1):
# Calculate the k'th derivative of f at x0
fk = diff(f,x,k).subs({x:x0})
# Add the k'th term to the approximation
Pk += fk/factorial(k)*(x-x0)**k
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\).
Answer
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.
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
Answer
def taylorN(f,x,x0):
N = len(x)
# Contant term
const = evaluateFunction(f,x,x0)
# First-degree term
J = Matrix([f]).jacobian(x)
J0 = evaluateFunction(J,x,x0)
first = J0*(x-x0)
# Second-degree term
H = hessian(f,x)
H0 = evaluateFunction(H,x,x0)
second = 1/2*(x-x0).T*H0*(x-x0)
# Result
Pk = simplify(Matrix([const]) + first + second)[0]
return Pk
Note
The implementation of evaluateFunction is not a particularly “elegant” use of Python. In larger projects you should rather use return f.subs(list(zip(vars, args))) in place of a for loop over the index.
Question e#
Given the function:
Determine the second-degree approximating polynomial using your implementation in the previous question with the expansion point \(\pmb{x}_0 = (3, 0, -\pi/2)\).
Answer
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:
Hint
The absolute error incurred is given by \(|P_2(x_1,x_2,x_3)-f(x_1,x_2,x_3)|\).
Hint
the relative error incurred is given by \(|P_2(x_1,x_2,x_3)-f(x_1,x_2,x_3)|/|f(x_1,x_2,x_3)|\).
Answer
For \(\pmb{y}_1\), the absolute error is \(0.0589\) and the relative error is 3.55%, and for \(\pmb{y}_2\), the absolute error is \(0.0401\) and the relative error is 5.28%.
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:
Answer
\(f(x)=2-4x-x^2+x^2\cdot \varepsilon(x)\).
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\).
Hint
Use the first two pieces of information to write down \(P_2(x)\). There is actually only one unknown. Use the last piece of information to determine it.
Answer
\(P_2(x)=-1+x+(x-2)^2\).
2: Taylor’s Limit Formula#
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
Hint
Insert the expression for Taylor’s limit formula for \(\ln(1+x)\) of order 1, and then see if the limit can be determined. In this question, we do not need to use Taylor’s limit formula for the denominator. Why?
Hint
After simplifying with \(x\), read off the limit value.
Hint
Now try with order 2 and order 3.
Answer
It is sufficient to expand to order 2.
Answer
Question c#
Now compute, using Taylor’s limit formula, the following limit value:
Hint
The denominator is of degree 3, so we only need to expand the numerator to order 3.
Answer
3: Trick Exercise#
A function \(f \in C^{\infty}(\mathbb{R}^2)\) satisfies the equations
Find the approximating polynomial of degree 2 for the function \(f\) with \((x_0,y_0)=(0,0)\) as the expansion point.
Hint
At first glance, it seems like we don’t have enough information. But try to write down the expansion formula with the information you have.
Hint
Hint
Find the missing information!
Answer
\(P_2(x,y)=1+x+\frac{1}{2}x^2+y^2\).
4: Taylor’s Formulas and Approximations#
We are given the function \(f: \mathbb{R}^2 \to \mathbb{R}\) by:
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(\frac34, \frac12\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(\frac34, \frac12\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 two 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)\). You may use Python/Sympy for these calculations.
Answer
\(P_2(x,y)=1+x-2y-xy+\frac{1}{2}x^2+2y^2\) \(Q_2(x,y)=2x-y-(x-1)(y-1)+2(x-1)^2+\frac{1}{2}(y-1)^2\)
Answer
The deviation when using \(P_2\) is \(2.04\%\), while when using \(Q_2\) it is \(0.719\%\). So in this case, it was probably worth the effort to develop \(Q_2 \ldots\)
5: Application of Approximating Polynomial#
A function \(f:\mathbb{R}^2 \to \mathbb{R}\) is given by
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)\).
Hint
See in the definition in the textbook a formula for the approximating 2nd-degree polynomial.
Answer
\(\displaystyle{P_2(x,y)=5+\frac{3}{5}(x-3)+\frac{4}{5}(y-4)+\frac{8}{125}(x-3)^2-\frac{12}{125}(x-3)(y-4)+\frac{9}{250}(y-4)^2.}\)
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 the previous question the length of the diagonal in a rectangle with side lengths 2.9 and 4.2.
Hint
The distance from the origin to an arbitrary point \((x,y)\) is a function of two variables. In fact, it is \(f(x,y)=\sqrt{x^2+y^2}\).
Hint
Imagine the rectangle placed in the \((x, y)\) coordinate system such that the diagonal connects the points \((0, 0)\) and \((2.9, 4.2)\).
Answer
The length can be approximated using \(P_2\) which we found above with the expansion point \((3,4)\). In short, \(P_{2}(2.9,4.2)=5.10400\).
Question c#
Compare with a Python/Sympy value of the diagonal length.
Answer
The computer value would be about \(5.10392\), so the approximation is not that bad.
Question d#
Discuss whether the difference is significant.
Answer
The difference is approximately \(8 \cdot 10^{-5}\), which is not much compared to the length of the diagonal. The difference \(8 \cdot 10^{-5}\) is called the absolute error; the absolute error relative to (i.e., divided by) the length of the diagonal is called the relative error.