How to Write a Taylor Series in Python

By Michael Carroll

A Taylor series is a representation of a function using an infinite sum. Computers often make approximations of the values of a trigonometric, exponential or other transcendental function by summing a finite number of the terms of its Taylor series, and you can recreate this process in Python. The terms of the sum are based on successive derivatives of the function, so you'll need to identify a pattern in the values of those derivatives to write a formula for each term of the series. Then, use a loop to accumulate the sum, controlling the accuracy of your approximation with the number of iterations of the loop.

Step 1

Consult the definition of the Taylor series to understand how each term may be computed. Each term of the series is indexed, typically by "n," and its value is related to the nth derivative of the function being represented. For simplicity's sake, use 0 for the value of "a" on your first attempt. This special version of the Taylor series is called the Maclaurin series. Try the sine function, since its successive derivatives are easy to determine.

Step 2

Write down several values of the nth derivative of the sine function evaluated at 0. If n is 0, the value is 0. If n is 1, the value is 1. If n is 2, the value is 0. If n is 3, the value is -1. From here, the pattern repeats, so disregard every even-indexed term of the Taylor series since it's multiplied by 0. A formula for each term of the resulting series is:(-1)^n/(2n+1)!*x^(2n+1)"2n+1" is used in place of "n" to re-index the series, effectively discarding the even-indexed terms without changing the index itself. The (-1)^n factor accounts for the alternation between positive and negative of successive terms. This preliminary math work might seem extraneous, but the Python code will be far easier to write and reuse on other Taylor series if the index always starts at 0 and counts upward in increments of 1.

Step 3

Open the Python interpreter. Start by typing the following commands to define several variables:sum = 0x = .5236The "sum" variable will be used to accumulate the sum of the Taylor series as each term is computed. The variable "x" is the angle (in radians) for which you want to approximate the sine function. Set it to whatever you like.

Step 4

Import the "math" module with the following command so you have access to the "pow" and "factorial" functions:import math

Step 5

Initiate a "for" loop, setting the number of iterations with the "range" function:for n in range(4):This will cause the index variable, n, to start at zero and count up to 4. Even this small number of iterations will yield a surprisingly accurate result. The loop does not execute immediately and will not begin until you've specified the whole block of code to iterate over.

Step 6

Type the following command to add the value of each successive term to "sum:" sum += math.pow(-1,n)/math.factorial(2*n+1)*math.pow(x,2*n+1) Notice that the command is indented with a tab, which indicates to Python that it's part of the "for" loop. Also note how "pow" and "factorial" are used in place of the "^" and "!" notation. The formula to the right of the "+=" assignment operator is identical to the one in Step 2, but written in Python syntax.

Step 7

Press "Enter" to add a blank line. To Python, this indicates termination of the "for" loop, so the calculation is executed. Type the command "sum" to reveal the result. If you used the value of x given in Step 3, the result is very close to .5, the sine of pi/6. Try the process again for different values of x and for different numbers of iterations of the loop, checking your results against the "math.sin(x)" function. You've implemented in Python the very process many computers use to compute values for sine and other transcendental functions.

Tips & Warnings

  • Indent and type the command "sum" on the second line of the "for" loop to get a running total of the sum as the code executes. This reveals how each successive term of the series brings the sum closer and closer to the actual value of the function.