When the exponential function (e x ) is expressed in this format:
e x = 1 + x/1 + x 2 /2! + x 3 /3! +….+ n terms
This guide illustrates various methods to create a program that can calculate the output of a function based on the provided values of x and n:
Inputs: x and n
For instance, when provided with x = 4 and n = 2, the task involves computing the result obtained from expanding the mathematical expression of e raised to the power of 4 up to 2 terms.
e 4 = 1 + 4/1 + 4 2 /2!
Notice that each expression consists of two functions:
- raised to the power of n
- factorial of 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
Generalization:
The Taylor series function Taylor(x, n) calculates the power of x to the nth degree, divided by the factorial of n, and adds it to the result of the Taylor series function with a parameter of n-1.
#include<stdio.h>
float fact(int n)
{
if(n==1 || n==0)
{
return 1;
}
return n*fact(n-1);
}
float power(float m, int n)
{
if(n==0)
{
return 1;
}
if(n==1)
{
return m;
}
return m*power(m, n-1);
}
float taylor(float x, int n)
{
if(n==0)
{
return 1;
}
float temp = power(x, n)/fact(n);
return temp + taylor(x, n-1);
}
int main()
{
int n;
float x;
printf("Enter the value of x: ");
scanf("%f", &x);
printf("Enter the value of n: ");
scanf("%d", &n);
float res = taylor(x, n);
printf("e to the power %0.1f till %d terms: %f", x, n, res);
}
Output:
Enter the value of x: 3
Enter the value of n: 2
e to the power 3.0 till 2 terms: 8.500000
There are an excessive number of recursive functions; let's streamline this:
Approach 2: Only one function:
We require a single function that combines the operations of exponentiation and factorial to compute the values within the Taylor series:
Let's take an example:
x = 3, n = 2
For the value of n equal to zero, the result of raising 3 to the power of 0 is 1, the factorial of 0 is 1, and the Taylor series expansion for 3 raised to the power of 0 is also 1.
For a value of n equal to 1, the result of raising 3 to the power of 1 is 3, the factorial of 1 is 1, and the first term in the Taylor series expansion for 3 is also 3.
For n = 2 -> pow(3, 2) = 9, fact(2) = 2, Taylor(3, 2) = 8.5
Note that within each recursive invocation, the expressions pow(m, n) = mpow(n-1) and fact(n) = nfact(n-1) should be considered as variables, rather than distinct functions.
The key point to remember is that the data must remain constant throughout the recursive invocations of the Taylor function, necessitating the utilization of Static Variables.
#include<stdio.h>
float taylors(float x, int n)
{
static float power = 1, fact = 1;
if(n==0)
{
return 1;
}
float temp = taylors(x, n-1);
power = power*x;
fact = fact*n;
return temp + power/fact;
}
int main()
{
int n;
float x;
printf("Enter the value of x: ");
scanf("%f", &x);
printf("Enter the value of n: ");
scanf("%d", &n);
float res = taylors(x, n);
printf("e to the power %0.1f till %d terms: %f", x, n, res);
}
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 must carry out multiplication operations for both exponentiation and factorial calculations during each recursive iteration. It is crucial to reduce the total count of multiplication operations executed.
Approach 3 (Horner's rule):
Horner's algorithm offers a more efficient method for evaluating polynomial expressions by minimizing the number of multiplications required, thereby improving program performance. By factoring out the variable and identifying recurring patterns, it enables us to streamline calculations within loops or recursive functions.
Approach:
e x = 1 + x/1 + x 2 /2! + x 3 /3! + …. + n terms
Let's assume that n = 4:
e x = 1 + x/1 + x 2 /2! + x 3 /3! + x 4 /4!
= 1 + x/1(1 + x/2 + x 2 /23 + x 3 /23*4)
= 1 + x/1(1 + x/2(1 + x/3 + x 2 /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
#include<stdio.h>
float besttaylors(float x, int n)
{
static float sum = 1;
if(n==0)
{
return sum;
}
sum = 1 + x/n*sum;
return besttaylors(x, n-1);
}
int main()
{
int n;
float x;
printf("Enter the value of x: ");
scanf("%f", &x);
printf("Enter the value of n: ");
scanf("%d", &n);
float res = besttaylors(x, n);
printf("e to the power %0.1f till %d terms: %f", x, n, res);
}
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:
#include<stdio.h>
float iterbesttaylors(float x, int n)
{
float sum = 1;
while(n>0)
{
sum = 1 + x/n*sum;
n--;
}
return sum;
}
int main()
{
int n;
float x;
printf("Enter the value of x: ");
scanf("%f", &x);
printf("Enter the value of n: ");
scanf("%d", &n);
float res = iterbesttaylors(x, n);
printf("e to the power %0.1f till %d terms: %f", x, n, res);
}
Output:
Enter the value of x: 3
Enter the value of n: 2
e to the power 3.0 till 2 terms: 8.500000