Taylor Series: Examples and Problems
Examples
Example 1
Apply Taylor’s Theorem to the function defined as to estimate the value of . Use . Estimate an upper bound for the error.
Solution
The Taylor approximation of the function around the point is given as follows:
If terms are used (including ), then the upper bound for the error is:
With , an upper bound for the error can be calculated by assuming . That’s because the absolute values of the derivatives of attain their maximum value in the interval at 4. The derivatives of have the following form:
The derivatives of the function evaluated at the point can be calculated as:
If two terms are used, then:
In this case, the upper bound for the error is:
Using Mathematica, the square root of approximated to 4 decimal places is . Therefore, the error when two terms are used is:
which satisfies that the actual error is less the upper bound:
If three terms are used, then:
In this case, the upper bound for the error is:
The actual error in this case is indeed less than the upper bound:
The following code provides a user-defined function for the Taylor series having the following inputs: a function , the value of , the value of , and the number of terms including the constant term.
View Mathematica CodeClear[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
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 defined as to estimate the value of and . Use . Estimate an upper bound for the error.
Solution
First, we will calculate the numerical solution for the and :
The Taylor approximation around is given as:
If terms are used (including ) and if , then the upper bound of the error is:
If , then, the upper bound of the error is:
The derivatives of the function are given by:
when evaluated at these have the values:
For , and using two terms:
Using three terms:
Using four terms:
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 as follows:
The maximum value would be obtained for . Therefore:
Indeed, the actual value of the error is less than the upper bound:
For , the Taylor series for this function around doesn’t give a very good approximation as will be shown here but rather keeps oscillating. First, using two terms:
Using three terms:
Using four terms:
Using five terms:
Using six terms:
In fact, the following table gives the values up to 21 terms. It is clear that the Taylor series is diverging.
The error term in the theorem gives an upper bound for when six terms are used as follows:
The maximum value will be obtained when :
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 and is as small as possible. For some functions, like , , and , the Taylor series always converges. However, for functions with square roots, the Taylor series converges when is relatively close to . There are some analytical conditions that would indicate the radius of convergence 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 , the value of , the value of , and the number of terms including the constant term.
View Mathematica CodeClear[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
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 defined as at . Use . Calculate the error associated with each expansion.
Solution
The true value of the function at is given by:
The zero order Taylor series expansion around has the following form:
The error in this case is given by:
The first order Taylor series expansion around has the following form:
The error in this case is given by:
The second order Taylor series expansion around has the following form:
The error in this case is given by:
The third order Taylor series expansion around has the following form:
The error in this case is given by:
The fourth order Taylor series expansion around has the following form:
The error in this case is given by:
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
- Use the Taylor series for the function defined as to estimate the value of . Use once and 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 should not give a proper approximation for because 10 and 4 are far from each other)
- Using the Taylor series and setting , derive the polynomial forms of the functions listed in the MacLaurin series section.
- Use Taylor’s Theorem to find an estimate for at with . Employ the zero-, first-,
second-, and third-order versions and compute the truncation error for each case. - Using the MacLaurin series expansion for , find an approximation for as a function of the number of terms up to 5 terms. For each case, find the relative error and the relative approximate error .
- Use zero- through third-order Taylor series expansions to predict for
assuming that . Compute for each approximation.
- Derive the MacLaurin series for the following functions:
Note: The resulting series are only convergent for
- Derive the MacLaurin series for the following functions:
Note: The resulting series are only convergent for
This is a comment
how to activate first example’s code in matlab. it gives error everytime. please help
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🙏