The Taylor series approximation of a cos function

A Taylor series approximation is a mathematical technique for representing a function as an infinite series of terms. In this representation, each term corresponds to a derivative of the function evaluated at a particular point. This mathematical technique is named after the English mathematician Brook Taylor, who introduced the concept in 1715.

The Taylor series for a 1-D function f(x)f(x) centered around a point aa is written as:

In the above equation:

  • f(x)f(x) is the original function we want to approximate.

  • f(a)f(a) is the function value at a specific point aa.

  • (xa)(x-a) represents the deviation of the point xx from point aa.

  • f(a)f'(a), f(a)f''(a),f(a)f'''(a) , andfi(a)f^i(a) represents the first, second, third, and ithi^{th}derivative of the function calculated at point aa.

  • ith!i^{th}! represents the factorial of ii.

The Taylor series is expanding the function at a specific point aa. This expansion represents complex functions as structured polynomials, approximating their behavior near a specific point.

The compact representation of the Taylor series is written as follows:

In the above equation, ii represents the ithi^{th} term. For 2D functions, Taylor series approximation is written as:

The above equation is a second-order Taylor series approximation, and the notations used in the equation represent:

  • f(x,y)f(x,y) is the original 2-D function we want to approximate.

  • aa and bb are points around which we are approximating the function.

  • (xa)11!fx(x,y)+(yb)11!fy(x,y)\frac{(x-a)^1}{1!} f_x(x,y) + \frac{(y-b)^1}{1!}f_y(x,y) represents the first-order derivative wherefxf_xand fyf_yare the first partial derivatives of the function w.r.t xx and yy at point aa and bb.

  • fxxf_{xx}, fyyf_{yy} represent the second partial derivatives and fxyf_{xy}represents the mixed partial derivative of the function.

The accuracy of the Taylor series approximation depends on the order of the series. When one increases the order of the series by including more polynomial terms, the approximation becomes progressively more accurate to the underlying function within a specified range around the specific point. However, be aware that the Taylor series approximation is only useful when xx is close to aa.

The cos function approximation

The following Python code showcases the power of Taylor series approximations by visualizing the cosine function approximation. In the code, a cosine function is approximated, having values ranging from -2*pi to 2*pi with 1000 points at a specific point a.

Please login to launch live app!

Note: We can click the arrow to the upper right corner to view the Python code in Jupyter Notebook in a new tab.

Explanation

  • We start by importing necessary libraries to facilitate our mathematical and visualization tasks. We utilize numpy for numerical operations, sympy for symbolic mathematics, and matplotlib.pyplot for creating informative plots.

  • We introduce the taylor_cos_order(n, a) function to compute the Taylor series approximation. Within this function, the parameters are:

    • The n parameter represents the order of the Taylor series.

    • The x parameter represents the range of values over which we calculate the Taylor series approximations for cos(x).

    • The a parameter represents the point around which the Taylor series is computed.

  • To calculate numerical evaluation, we use the lambdify function of the sympy library, which converts the mathematical sympy expression to numpy function. These functions, in turn, are used to compute the corresponding values for the Taylor series approximations.

  • The code calculates nth order Taylor series approximations, specifically considering three distinct orders: 3rd order, 10th order, and 15th order. Each of these approximations provides varying degrees of precision in representing the cosine function.

  • Finally, we employ the pyplot function of the matplotlib library to visually convey the results.

Output

After running the above code, we’re presented with a graphical representation that offers valuable insights into the Taylor series approximations we’ve computed. This visualization serves as a powerful tool for gaining a deeper understanding of the behavior and convergence of these approximations.

Cosine function approximation at 3rd order, 10th order and 15th order
Cosine function approximation at 3rd order, 10th order and 15th order

As we progressively increase the order of the Taylor series, the cos(x) function approximation becomes notably more precise and accurate.

We can modify the order n parameter to investigate the impact of higher or lower orders on the accuracy of the approximations. Furthermore, by altering the a parameter, which represents the central point for approximation, we can gain insights into how different expansion points affect the quality of the Taylor series approximations.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved