The exponential function, exp(x), can be approximate by the following sum: exp(x
ID: 3582449 • Letter: T
Question
The exponential function, exp(x), can be approximate by the following sum: exp(x) = sigma_n=0^infinity x^n/n! The accuracy of the approximation increases with more terms. Obviously we don't need to keep going forever but only until a certain condition is met. write a type double C function that takes doubles; "x" and "delta" as parameters. The function should return an approximation of exp(x) by calculating the above sum. The number of terms in the sum should be determined as follows: You need to stop adding to the sum when the last term added to the sum is smaller than "delta". You can assume that you have a factorial function 'factorial" available (takes an integer as input argument and returns an integer as result). You can also use the pow library function as well (The C library function double pow (double x, double y) returns x raised to the power of y i.e.x^yExplanation / Answer
#include <stdio.h>
#include<math.h>
/* function declaration expsum */
float expsum( double x,double delta);
int main( )
{
/* initialization of variables */
double x,delta;
printf("Enter the 'x' value:");
scanf("%d",&x);
printf(" Enter the 'delta' value:");
scanf("%d",&delta);
/* function calling expsum */
sumprevious=expsum(x,delta);
printf(" Sum of the series:%f",sumprevious);
}
expsum( double x,double delta)
{
int fact,i,j,n;
float sumprevious=0;
printf(" Enter the 'n' value:");
scanf("%d",&n);
/* calculation of factorial */
for(i=1;i<=n;i++)
{
fact=1;
for( j=i;j>=1;j--)
fact=fact*j;
/* calculation of sum */
sumprevious=sumprevious+(pow(x,i)/fact);
/* checking condition */
if(sumprevious<delta)
break;
else
return sumprevious;
}
}
Output:
Enter the 'x' value:3
Enter the 'delta' value:4
Enter the 'n' value:2
Sum of the series:4.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.