A power series expansion may be used to approximate the value of the exponential
ID: 3673545 • Letter: A
Question
A power series expansion may be used to approximate the value of the exponential function e^x at some value x. The more terms are taken in the above series, the more accurate the answer becomes. Write a script which evaluates the value of e^2 (i.e., e^x w hen x = 2) by summing terms until the value of the current term is less than some value stop_va 1. When this occurs, add this term to the sum and exit the loop. Adjusting the value of stop_val will allow you to control how accurate the approximation is (i.e., how many terms are summed).Explanation / Answer
//This function will return the approximate value of e^x using sum of first n terms of the given problem.
float exponentialcalculate(int n, float x)
{
float sum = 1.0f; // In order to initialize the sum of the series
for (int i = n - 1; i > 0; --i )
sum = 1 + x * sum / i;
return sum;
}
// This is the final driver program to test above function
int main()
{
int n = 20;
float x = 1.0f;
printf("e^x = %f", exponentialcalculate(n, x));
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.