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
In the above equation:
is the original function we want to approximate. is the function value at a specific point . represents the deviation of the point from point . , , , and represents the first, second, third, and derivative of the function calculated at point . represents the factorial of .
The Taylor series is expanding the function at a specific point
The compact representation of the Taylor series is written as follows:
In the above equation,
The above equation is a second-order Taylor series approximation, and the notations used in the equation represent:
is the original 2-D function we want to approximate. and are points around which we are approximating the function. represents the first-order derivative where and are the first partial derivatives of the function w.r.t and at point and . , represent the second partial derivatives and 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
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.
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
numpyfor numerical operations,sympyfor symbolic mathematics, andmatplotlib.pyplotfor 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
nparameter represents the order of the Taylor series.The
xparameter represents the range of values over which we calculate the Taylor series approximations forcos(x).The
aparameter represents the point around which the Taylor series is computed.
To calculate numerical evaluation, we use the
lambdifyfunction of thesympylibrary, which converts the mathematicalsympyexpression tonumpyfunction. 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
pyplotfunction of thematplotliblibrary 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.
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