A calculator shows sin 30° as 0.5 in an instant. It contains no table of all possible answers. Instead, it adds up a few terms of a series. The same idea can be programmed with a simple loop.

Infinite Series and Approximation

Definition: Taylor series. An expression of a function as an infinite sum of terms, each computed from powers of x, whose partial sums approximate the value of the function.

A computer cannot add infinitely many terms. It adds the first few, and the result is an approximation. In general, the more terms that are added, the closer the sum comes to the true value.

Series for sin(x) and cos(x)

For an angle x measured in radians:

  • sin(x) = x − x³/3! + x⁵/5! − x⁷/7! + …
  • cos(x) = 1 − x²/2! + x⁴/4! − x⁶/6! + …

Here n! (n factorial) is the product 1 × 2 × … × n. The signs alternate, the powers rise by two from term to term, and the series for sin(x) uses odd powers while that for cos(x) uses even powers. An angle in degrees must first be converted: radians = degrees × π / 180.

Series for π

The value of π can be approximated by the Leibniz series:

  • π/4 = 1 − 1/3 + 1/5 − 1/7 + …

The sum of the series is multiplied by 4 to obtain π. This series approaches π slowly, as Table 2.8 shows; thousands of terms are needed for a few correct decimal places.

Table 2.8: Approximations of π by the Leibniz series

Number of terms Approximation of π
1 4.000000
10 3.041840
100 3.131593
1000 3.140593

Computing Each Term from the Previous One

Calculating x⁷ and 7! afresh for every term wastes effort, and factorials quickly become too large for an integer variable. A better method obtains each term from the one before it. In the series for sin(x), the term after x³/3! is x⁵/5!. The ratio of the two is x² / (4 × 5). In general:

  • next term = − (current term) × x² / ((2k) × (2k + 1)), for k = 1, 2, 3, …

Accuracy

The accuracy of the result depends on the number of terms and on the size of x. For small x the terms shrink very quickly, and four or five terms give several correct decimal places. For large x more terms are needed. A program should therefore state how many terms it used.

Example 2.8: Approximating sin(x)

Problem: Approximate sin(0.5), with x in radians, using the first 4 terms of the series, and compare the result with the library function.

Solution:

  1. The first term is x itself.
  2. Each later term is obtained from the previous one by multiplying by −x² / ((2k)(2k + 1)).
  3. The terms are added in a loop.
#include <stdio.h>
#include <math.h>

int main(void)
{
    double x = 0.5, term, sum;
    int k, n = 4;

    term = x;
    sum = x;
    for (k = 1; k < n; k++)
    {
        term = -term * x * x / ((2 * k) * (2 * k + 1));
        sum = sum + term;
    }
    printf("Series value  = %.6f\n", sum);
    printf("Library value = %.6f\n", sin(x));
    return 0;
}

Output:

Series value  = 0.479426
Library value = 0.479426

Result: With only four terms the series agrees with the library value to six decimal places, because x is small and the terms shrink rapidly.

Common mistakes

  • Supplying the angle in degrees when the series requires radians.
  • Forgetting that the signs of the terms alternate.
  • Computing large factorials in an int variable, which overflows; computing each term from the previous one avoids this.

Key takeaways

  • A Taylor series approximates a function by a sum of terms.
  • The series for sin(x) uses odd powers of x; the series for cos(x) uses even powers.
  • π can be approximated by 4 × (1 − 1/3 + 1/5 − 1/7 + …).
  • Each term is best computed from the previous term, and x must be in radians.

Practice questions

  1. Write the first three terms of the series for cos(x).
  2. Convert 90 degrees to radians.
  3. Why is a series value called an approximation?

Answers to the practice questions

  1. 1 − x²/2! + x⁴/4!
  2. 90 × π / 180 = π/2, about 1.5708 radians.
  3. Only a finite number of terms of the infinite series are added, so the sum is close to, but not exactly, the true value.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit II