Taylor Series

The Taylor series represents a function as an infinite sum of terms calculated from the function’s derivatives at a point. It can approximate functions as polynomials.

The Taylor series expansion about x=a is:

f(x) = f(a) + f’(a)(x-a) + f’’(a)(x-a)^2/2! + f’’’(a)(x-a)^3/3! + …

Where f’(a) is the first derivative of f evaluated at a, f’’(a) is the second derivative, etc.

Java example - exp(x) approximation:

1
2
3
4
5
6
7
8
9
double expTaylor(double x, int nterms) {
  double result = 1;
  double term = 1;
  for (int i = 1; i <= nterms; i++) {
    term *= x / i;
    result += term;
  }
  return result;
}

C++ example - sin(x) approximation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
double sinTaylor(double x, int nterms) {
  double result = 0;
  double term = x;
  int sign = 1;
  for (int i = 1; i <= nterms; i++) {
    result += sign * term;
    term *= x * x / ((2*i)*(2*i + 1));
    sign *= -1; 
  }
  return result;
}

Python example - log(1+x) approximation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def logTaylor(x, nterms):
  result = 0
  term = 1
  sign = 1
  for i in range(1, nterms+1):
    result += sign * term / i
    sign *= -1
    term *= x

  return result

Taylor series provide polynomial approximations for functions using their derivatives. Useful for numerical analysis.

The Taylor series is a representation of a function as an infinite sum of terms. It approximates the function around a specific point, known as the expansion point or center. The Taylor series of a function ( f(x) ) around the point ( a ) is given by:

[ f(x) = f(a) + f’(a)(x - a) + \frac{f’’(a)(x - a)^2}{2!} + \frac{f’’’(a)(x - a)^3}{3!} + \cdots ]

Here, ( f’(a), f’’(a), ) etc., denote the derivatives of ( f(x) ) evaluated at ( x = a ).

Java Code for Taylor Series

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class TaylorSeries {
    public static double computeSin(double x, int terms) {
        double result = 0.0;
        for (int i = 0; i < terms; i++) {
            double term = Math.pow(-1, i) * Math.pow(x, 2 * i + 1) / factorial(2 * i + 1);
            result += term;
        }
        return result;
    }

    public static long factorial(int n) {
        long result = 1;
        for (int i = 1; i <= n; i++) {
            result *= i;
        }
        return result;
    }
}

In Java, we implement the Taylor series approximation for ( \sin(x) ) using a for loop. We use Math.pow() for exponentiation and a separate function factorial() to calculate the factorial of numbers.

C++ Code for Taylor Series

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <cmath>

double computeSin(double x, int terms) {
    double result = 0.0;
    for (int i = 0; i < terms; ++i) {
        double term = pow(-1, i) * pow(x, 2 * i + 1) / factorial(2 * i + 1);
        result += term;
    }
    return result;
}

long long factorial(int n) {
    long long result = 1;
    for (int i = 1; i <= n; ++i) {
        result *= i;
    }
    return result;
}

In C++, we use the pow() function from the <cmath> header for exponentiation. The factorial function is implemented similarly to the Java version.

Python Code for Taylor Series

1
2
3
4
5
6
7
8
import math

def compute_sin(x, terms):
    result = 0
    for i in range(terms):
        term = math.pow(-1, i) * math.pow(x, 2 * i + 1) / math.factorial(2 * i + 1)
        result += term
    return result

In Python, the code uses math.pow() for exponentiation and math.factorial() to calculate factorials directly.

Understanding the Taylor series helps in numerous applications including numerical methods, engineering, and computer graphics. It provides a way to approximate complex functions using simpler polynomial terms.