Taylor Expansion

Taylor expansion approximates a function as a truncated polynomial series about a point x=a based on the function’s derivatives at a.

The Taylor expansion about a up to degree n is:

f(x) ≈ f(a) + f’(a)(x-a) + f’’(a)(x-a)^2/2! + … + f^n(a)(x-a)^n/n!

Where f^(n)(a) is the nth derivative of f evaluated at a.

Java example - e^x approximation:

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

  return result;
}

C++ example - sin(x) approximation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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;
    sign *= -1;
    term *= x * x / ((2*i) * (2*i + 1));
  }

  return result;
}

Python example - log(1+x) approximation:

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

Taylor expansion provides a polynomial form for approximating functions, useful for numerical analysis.

Taylor expansion is a way to represent a function as an infinite sum of terms that are calculated from the values of the function’s derivatives at a single point. It provides a polynomial approximation for functions that are differentiable. The idea is to expand the function around a point, ( a ), using its derivatives:

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

Each term involves a derivative of the function ( f(x) ) evaluated at the point ( a ). Taylor expansion is widely used in numerical methods, simulations, and calculations involving functions that are complex to deal with directly.

Java Code for Taylor Expansion

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

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

In Java, the code calculates the Taylor expansion of ( e^x ) using a for loop. The Math.pow() method is used for exponentiation, and the factorial is calculated with a separate factorial() function.

C++ Code for Taylor Expansion

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

double computeExp(double x, int terms) {
    double result = 0.0;
    for (int i = 0; i < terms; ++i) {
        result += pow(x, i) / factorial(i);
    }
    return result;
}

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

In C++, the code for Taylor expansion is quite similar to the Java code. The pow() function from <cmath> is used for exponentiation.

Python Code for Taylor Expansion

1
2
3
4
5
6
7
import math

def compute_exp(x, terms):
    result = 0
    for i in range(terms):
        result += math.pow(x, i) / math.factorial(i)
    return result

In Python, the math.pow() and math.factorial() functions are used for exponentiation and calculating the factorial, respectively.

Taylor expansion is a fundamental concept in calculus and has numerous applications in engineering, physics, and computer science. It serves as a basis for approximating functions and is essential for various computational techniques.