Solve Taylor Series for Exponential function(ex) using C

Taylor Series is a mathematical representation of different functions introduced by Brook Taylor in the 18th century. Every function, when represented using Taylor series, is extended as an infinite sum of terms. General form:

f(x) = f(a) + f'(a)(x - a) + f''(a)(x - a)^2/2! + f'''(a)(x - a)^3/3! + ...n terms

When the exponential function (ex) is represented in this form:

ex = 1 + x/1 + x2/2! + x3/3! +….+ n terms

This tutorial explains different ways using which we can write a program that can give the value of the function given the values of x and n:

Inputs: x and n

For example, given x = 4 and n = 2, we need to return the value after calculating the expansion on e4 till 2 terms.

e4 = 1 + 4/1 + 42/2!

Observe that there are two functions in every term:

  1. m power n
  2. factorial(n)

Approach 1: Implementing two functions for both m power n and factorial:

Approach:

For x = 3 and n = 2:

power(3, 2)/fact(2) + taylor(3, 1)

9/2 + power(3, 1)/fact(1) + taylor(0)

4.5 + 3 + 1

8.5

Generalization:

Taylor(x, n) = power(x, n)/factorial(n) + Taylor(x, n -1)

Output:

Enter the value of x: 3
Enter the value of n: 2
e to the power 3.0 till 2 terms: 8.500000

Too many recursive functions; let us simplify this:

Approach 2: Only one function:

We need the functionalities of power and factorial in just one function that calculates the value of the Taylor series:

Let's take an example:

x = 3, n = 2

For n = 0 -> pow(3, 0) = 1, fact(0) = 1, Taylor(3, 0) = 1

For n = 1 -> pow(3, 1) = 3, fact(1) = 1, Taylor(3, 1) = 3

For n = 2 -> pow(3, 2) = 9, fact(2) = 2, Taylor(3, 2) = 8.5

Observe that in every recursive call, pow(m, n) = m*pow(n-1) and fact(n) = n*fact(n-1) should be variables instead of separate functions.

The catch here is that the values need to be persisted amid recursive calls of the Taylor function, for which we need to use the Static Variables.

Output:

Enter the value of x: 3
Enter the value of n: 2
e to the power 3.0 till 2 terms: 8.500000

We need to multiply for power and factorial in every recursive call. The number of multiplications performed has to be decreased.

Approach 3 (Horner's rule):

Horner's rule can be used to solve polynomial expressions without too many multiplications, making the program faster. We keep taking the variable common and find the pattern that we can use in loops or in recursive calls.

Approach:

ex = 1 + x/1 + x2/2! + x3/3! + …. + n terms

Let's assume that n = 4:

ex = 1 + x/1 + x2/2! + x3/3! + x4/4!

= 1 + x/1(1 + x/2 + x2/2*3 + x3/2*3*4)

= 1 + x/1(1 + x/2(1 + x/3 + x2/3*4)

= 1 + x/1(1 + x/2(1 + x/3(1 + x/4)))

Generalization:

For n = 4:

Assume sum = 1

Calculate 1 + x/4

Multiply sum = 1 + (x/4) * sum

Which means:

Initially, sum = 1

sum = 1 + (x/n) * sum

Output:

Enter the value of x: 3
Enter the value of n: 2
e to the power 3.0 till 2 terms: 8.500000

Iterative approach:

Output:

Enter the value of x: 3
Enter the value of n: 2
e to the power 3.0 till 2 terms: 8.500000