Open Educational Resources

Taylor Series: Examples and Problems

Examples

Example 1

Apply Taylor’s Theorem to the function f:[0,\infty)\rightarrow\mathbb{R} defined as f(x)=\sqrt{x} to estimate the value of f(5). Use a=4. Estimate an upper bound for the error.

Solution

The Taylor approximation of the function f(x) around the point a=4 is given as follows:

    \[ f(x)=f(4)+f'(4) (x-4)+\frac{f''(4)}{2!}(x-4)^2+\frac{f'''(4)}{3!}(x-4)^3+\cdots+\frac{f^{(n)}(4)}{n!}(x-4)^n+\cdots \]

If n+1 terms are used (including f(a)), then the upper bound for the error is:

    \[ |E|\leq \max_{\xi\in[a,x]}\frac{|f^{(n+1)}(\xi)|}{(n+1)!}(x-a)^{n+1} \]

With a=4, an upper bound for the error can be calculated by assuming \xi=4. That’s because the absolute values of the derivatives of f attain their maximum value in the interval [4,x] at 4. The derivatives of f(x) have the following form:

    \[\begin{split} f'(x)&=\frac{1}{2\sqrt{x}}\\ f''(x)&=\frac{-1}{4x^{\frac{3}{2}}}=\frac{-1}{4x\sqrt{x}}\\ f'''(x)&=\frac{3}{8x^{\frac{5}{2}}}=\frac{3}{8x^2\sqrt{x}}\\ f''''(x)&=\frac{-15}{16x^{\frac{7}{2}}}=\frac{-15}{16x^3\sqrt{x}} \end{split} \]

The derivatives of the function evaluated at the point a=4 can be calculated as:

    \[\begin{split} f'(4)&=\frac{1}{4}\\ f''(4)&=\frac{-1}{32}\\ f'''(4)&=\frac{3}{256}\\ f''''(4)&=\frac{-15}{2048} \end{split} \]

If two terms are used, then:

    \[ f(5)\approx f(4)+f'(4)(5-4)=2+\frac{1}{4}=2.25 \]

In this case, the upper bound for the error is:

    \[ |E|\leq \frac{|f''(4)|}{2!}(5-4)^2=\frac{1}{32(2)}=\frac{1}{64}=0.015625 \]

Using Mathematica, the square root of 5 approximated to 4 decimal places is \sqrt{5}=2.2361. Therefore, the error when two terms are used is:

    \[ E=2.2361-2.25=-0.0139 \]

which satisfies that the actual error is less the upper bound:

    \[ |-0.0139|<0.015625 \]

If three terms are used, then:

    \[ f(5)\approx f(4)+f'(4)(5-4)+\frac{f''(4)}{2!}(5-4)^2=2+\frac{1}{4}+\frac{-1}{64}=2.23438 \]

In this case, the upper bound for the error is:

    \[ |E|\leq \frac{|f'''(4)|}{3!}(5-4)^3=\frac{3}{256\times 3\times 2}=0.001953 \]

The actual error in this case is indeed less than the upper bound:

    \[ E=2.2361-2.23438=0.00172<0.001953 \]

The following code provides a user-defined function for the Taylor series having the following inputs: a function f, the value of x, the value of a, and the number of terms n including the constant term.

View Mathematica Code
Clear[x]
Taylor[f_, xi_, a_, n_] := Sum[(D[f, {x, i}] /. x -> a)/i!*(xi - a)^i, {i, 0, n - 1}]
f = (x)^(0.5);
a = Table[{i, Taylor[f, 5, 4, i]}, {i, 2, 21}];
a // MatrixForm
View Python Code
import math
import sympy as sp

x = sp.symbols('x')
def taylor(f,xi,a,n):
  return sum([(f.diff(x, i).subs(x,a))/math.factorial(i)*(xi - a)**i for i in range(n)])
f = x**(0.5)
m = []
for i in range(2,22):
  m.append([i,taylor(f,5,4,i)])
display(sp.Matrix(m))

You can download the MATLAB file below which provides the solution to this question.

Example 2

Apply Taylor’s Theorem to the function f:(-\infty,1]\rightarrow\mathbb{R} defined as f(x)=\sqrt{1-x} to estimate the value of f(0.1) and f(-2). Use a=0. Estimate an upper bound for the error.

Solution

First, we will calculate the numerical solution for the f(0.1) and f(-2):

    \[ f(0.1)=\sqrt{1-0.1}=\sqrt{0.9}=0.948683 \qquad f(-2)=\sqrt{1+2}=\sqrt{3}=1.73205 \]

The Taylor approximation around a=0 is given as:

    \[ f(x)=f(0)+f'(0) (x-0)+\frac{f''(0)}{2!}(x-0)^2+\frac{f'''(0)}{3!}(x-0)^3+\cdots+\frac{f^{(n)}(0)}{n!}(x-0)^n+\cdots \]

If n+1 terms are used (including f(0)) and if x>a, then the upper bound of the error is:

    \[ |E|\leq \max_{\xi\in[a,x]}\frac{|f^{(n+1)}(\xi)|}{(n+1)!}(x-a)^{n+1} \]

If x<a, then, the upper bound of the error is:

    \[ |E|\leq \max_{\xi\in[x,a]}\frac{|f^{(n+1)}(\xi)|}{(n+1)!}(a-x)^{n+1} \]

The derivatives of the function are given by:

    \[ \begin{split} f(x)&=(1-x)^{0.5}\\ f'(x)&=-\frac{0.5}{(1-x)^{0.5}}\\ f''(x)&=-\frac{0.25}{(1-x)^{1.5}}\\ f'''(x)&=-\frac{0.375}{(1-x)^{2.5}}\\ f''''(x)&=-\frac{0.9375}{(1-x)^{3.5}}\\ f^{(5)}(x)&=-\frac{3.28125}{(1-x)^{4.5}}\\ f^{(6)}(x)&=-\frac{14.7656}{(1-x)^{5.5}}\\ f^{(7)}(x)&=-\frac{81.2109}{(1-x)^{6.5}}\\ f^{(8)}(x)&=-\frac{527.871}{(1-x)^{7.5}} \end{split} \]

when evaluated at a=0 these have the values:

    \[\begin{split} f(0)&=1\\ f'(0)&=-0.5\\ f''(0)&=-0.25\\ f'''(0)&=-0.375\\ f''''(0)&=-0.9375\\ f^{(5)}(0)&=-3.28125\\ f^{(6)}(0)&=-14.7656\\ f^{(7)}(0)&=-81.2109\\ f^{(8)}(0)&=-527.871 \end{split} \]

For x=0.1, and using two terms:

    \[ f(0.1)\approx f(0)+f'(0)(0.1)=1-0.5\times 0.1 = 0.95 \]

Using three terms:

    \[ f(0.1)\approx f(0)+f'(0)(0.1)+\frac{f''(0)}{2!}(0.1)^2=1-0.5\times 0.1-\frac{0.25}{2}0.1^2 = 0.94875 \]

Using four terms:

    \[ \begin{split} f(0.1)&\approx f(0)+f'(0)(0.1)+\frac{f''(0)}{2!}(0.1)^2+\frac{f'''(0)}{3!}(0.1)^3\\ &=1-0.5\times 0.1-\frac{0.25}{2}0.1^2-\frac{0.375}{3\times 2}0.1^3 = 0.9486875 \end{split} \]

The terms are getting closer to each other and four terms provide a good approximation. The error term in the theorem gives an upper bound for |E| as follows:

    \[ |E|\leq \max_{\xi\in[0,0.1]}\frac{|f^{(4)}(\xi)|}{4\times 3 \times 2\times 1}0.1^4=\frac{\frac{0.9375}{(1-\xi)^{3.5}}}{4\times 3 \times 2\times 1}0.1^4=\frac{3.9063(10)^{-6}}{(1-\xi)^{3.5}} \]

The maximum value would be obtained for \xi=0.1. Therefore:

    \[ |E|\leq\frac{3.9063(10)^{-6}}{(1-0.1)^{3.5}}=5.64822(10)^{-6} \]

Indeed, the actual value of the error is less than the upper bound:

    \[ |E|=|0.948683-0.948688|=4.5(10)^{-6}<5.64822(10)^{-6} \]

For x=-2, the Taylor series for this function around a=0 doesn’t give a very good approximation as will be shown here but rather keeps oscillating. First, using two terms:

    \[ f(-2)\approx f(0)+f'(0)(-2)=1-0.5\times (-2)=1+1=2 \]

Using three terms:

    \[ f(-2)\approx f(0)+f'(0)(-2)+\frac{f''(0)}{2!}(-2)^2=1-0.5\times (-2)-\frac{0.25}{2}(-2)^2=1.5 \]

Using four terms:

    \[\begin{split} f(-2)&\approx f(0)+f'(0)(-2)+\frac{f''(0)}{2!}(-2)^2+\frac{f'''(0)}{3!}(-2)^3\\ &=1-0.5\times (-2)-\frac{0.25}{2}(-2)^2-\frac{0.375}{3\times 2}(-2)^3 \\ &= 2 \end{split} \]

Using five terms:

    \[\begin{split} f(-2)&\approx f(0)+f'(0)(-2)+\frac{f''(0)}{2!}(-2)^2+\frac{f'''(0)}{3!}(-2)^3+\frac{f''''(0)}{4!}(-2)^4\\ &=1-0.5\times (-2)-\frac{0.25}{2}(-2)^2-\frac{0.375}{3\times 2}(-2)^3 -\frac{0.9375}{4\times 3\times 2}(-2)^4\\ &= 1.375 \end{split} \]

Using six terms:

    \[\begin{split} f(-2)&\approx f(0)+f'(0)(-2)+\frac{f''(0)}{2!}(-2)^2+\frac{f'''(0)}{3!}(-2)^3+\frac{f''''(0)}{4!}(-2)^4+\frac{f'''''(0)}{5!}(-2)^5\\ &=1-0.5\times (-2)-\frac{0.25}{2}(-2)^2-\frac{0.375}{3\times 2}(-2)^3 -\frac{0.9375}{4\times 3\times 2}(-2)^4-\frac{-3.28125}{5\times 4\times 3\times 2}(-2)^5\\ &=2.25 \end{split} \]

In fact, the following table gives the values up to 21 terms. It is clear that the Taylor series is diverging.
Taylor1

The error term in the theorem gives an upper bound for |E| when six terms are used as follows:

    \[ |E|\leq\max_{\xi\in[-2,0]}\frac{|f^{(6)}(\xi)|}{6\times 5\times 4\times 3 \times 2\times 1}(2)^6=\frac{\frac{14.7656}{(1-\xi)^{5.5}}}{6\times 5\times 4\times 3 \times 2\times 1}(2)^6=\frac{1.3125}{(1-\xi)^{5.5}} \]

The maximum value will be obtained when \xi=0:

    \[ |E|\leq \frac{1.3125}{(1-\xi)^{5.5}}=1.3125 \]

This is a large upper bound and indicates that using six terms is not giving a good approximation. In general, the Taylor series works best if the distance between x and a is as small as possible. For some functions, like \sin(x), \cos(x), and e^x, the Taylor series always converges. However, for functions with square roots, the Taylor series converges when x is relatively close to a. There are some analytical conditions that would indicate the radius of convergence (x-a) of a Taylor series; however, this is beyond the scope of this course!

The following code provides a user-defined function for the Taylor series having the following inputs: a function f, the value of x, the value of a, and the number of terms n including the constant term.

View Mathematica Code
Clear[x]
Taylor[f_, xi_, a_, n_] := Sum[(D[f, {x, i}] /. x -> a)/i!*(xi - a)^i, {i, 0, n-1}]
f = (1 - x)^(0.5);
a = Table[{i, Taylor[f, -2, 0, i]}, {i, 2, 21}];
a // MatrixForm
a = Table[{i, Taylor[f, 0.1, 0, i]}, {i, 2, 21}];
a // MatrixForm
View Python Code
import math
import sympy as sp

x = sp.symbols('x')
def taylor(f,xi,a,n):
  return sum([(f.diff(x, i).subs(x,a))/math.factorial(i)*(xi - a)**i for i in range(n)])
f = (1 - x)**(0.5)
m1 = []; m2 = []
for i in range(2,22):
  m1.append([i,taylor(f, -2, 0, i)])
  m2.append([i,taylor(f, 0.1, 0, i)])
display(sp.Matrix(m1))
display(sp.Matrix(m2))

Example 3

Use zero through fourth order Taylor’s series expansion to approximate the value of the function f:\mathbb{R}\rightarrow\mathbb{R} defined as f(x)=-0.1x^4-0.15x^3-0.5x^2-0.25x+1.2 at x=1. Use a=0. Calculate the error associated with each expansion.

Solution

The true value of the function at x=1 is given by:

    \[ f_{true}(1)=-0.1-0.15-0.5-0.25+1.2=0.2 \]

The zero order Taylor series expansion around a=0 has the following form:

    \[ f(1)\approx f(0)=1.2 \]

The error in this case is given by:

    \[ E=0.2-1.2=-1 \]

The first order Taylor series expansion around a=0 has the following form:

    \[ f(1)\approx f(0)+(1-0)f'(0) = 1.2+(1-0)(-0.25)=0.95 \]

The error in this case is given by:

    \[ E=0.2-0.95=-0.75 \]

The second order Taylor series expansion around a=0 has the following form:

    \[ f(1) \approx f(0)+(1-0)f'(0)+\frac{(1-0)^2}{2!}f''(0) = 1.2+(1-0)(-0.25)+\frac{1}{2}(-1)=0.45 \]

The error in this case is given by:

    \[ E=0.2-0.45=-0.25 \]

The third order Taylor series expansion around a=0 has the following form:

    \[\begin{split} f(1)& \approx f(0)+(1-0)f'(0)+\frac{(1-0)^2}{2!}f''(0)+\frac{(1-0)^3}{3!}f'''(0)\\ & = 1.2+(1-0)(-0.25)+\frac{1}{2}(-1)+\frac{1}{6}(-0.9)\\ &=0.3 \end{split} \]

The error in this case is given by:

    \[ E=0.2-0.3=-0.1 \]

The fourth order Taylor series expansion around a=0 has the following form:

    \[\begin{split} f(1)&\approx f(0)+(1-0)f'(0)+\frac{(1-0)^2}{2!}f''(0)+\frac{(1-0)^3}{3!}f'''(0)+\frac{(1-0)^4}{4!}f''''(0)\\ & = 1.2+(1-0)(-0.25)+\frac{1}{2}(-1)+\frac{1}{6}(-0.9)+\frac{1}{24}(-2.4)\\ & =0.2 \end{split} \]

The error in this case is given by:

    \[ E=0.2-0.2=0 \]

It is important to note that the error reduces to zero when using the fourth order Taylor series approximation. That is because the fourth order Taylor series approximation of a fourth order polynomial function is identical to the function itself. You can think of this as follows, the zero order Taylor approximation provides a “constant” function approximation. The second order Taylor approximation provides a parabolic function approximation while the third order provides a cubic function approximation. The nth Taylor series approximation of a polynomial of degree “n” is identical to the function being approximated!

You can download the MATLAB file below which provides the solution to this question.

Problems

  1. Use the Taylor series for the function f:[0,\infty)\rightarrow\mathbb{R} defined as f(x)=\sqrt{x} to estimate the value of f(10). Use a=4 once and a=9 for another time. Estimate an upper bound for the error for each approximation. Comment on the behaviour of the Taylor series of this function. (hint: For this particular function using a Taylor expansion around a=4 should not give a proper approximation for f(10) because 10 and 4 are far from each other)
  2. Using the Taylor series and setting a=0, derive the polynomial forms of the functions listed in the MacLaurin series section.
  3. Use Taylor’s Theorem to find an estimate for f(x)=e^{-x} at x=1 with a=0. Employ the zero-, first-,
    second-, and third-order versions and compute the truncation error for each case.
  4. Using the MacLaurin series expansion for \cos{x}, find an approximation for \cos{\frac{\pi}{4}} as a function of the number of terms up to 5 terms. For each case, find the relative error E_r and the relative approximate error \varepsilon_r.
  5. Use zero- through third-order Taylor series expansions to predict f(3) for

        \[ f(x) = 25x^3-6x^2+7x-88 \]

    assuming that a = 1. Compute E_r for each approximation.

  6. Derive the MacLaurin series for the following functions:

        \[ f_1(x)=\frac{1}{1+x}\quad f_2(x)=\frac{1}{1-x}\quad f_3(x)=\frac{1}{1+x^2} \]

    Note: The resulting series are only convergent for x\in (-1,1)

  7. Derive the MacLaurin series for the following functions:

        \[ f_1(x)=\ln{(1+x)}\quad \quad f_2(x)=\tan^{-1}{x} \]

    Note: The resulting series are only convergent for x\in (-1,1)

Page Comments

  1. Samer Adeeb says:

    This is a comment

  2. maras says:

    how to activate first example’s code in matlab. it gives error everytime. please help

  3. Richard A says:

    Many thanks for sharing these Numerical Methods OpenCourse resources to the learning community! Dr. Lindsey Westover’s videos are very well presented and narrated – she’s a natural teacher🙏

Leave a Reply to Samer Adeeb Cancel reply

Your email address will not be published.