The power series \\(1+x+(x^2/2!)+(x^3/3!)+...=\\sum_{n=0}^{\\infty}x^n/n!\\) con
ID: 3545785 • Letter: T
Question
The power series (1+x+(x^2/2!)+(x^3/3!)+...=sum_{n=0}^{infty}x^n/n!) converges to e^x for all values of x. Write a function that uses this series to calculate e^x to six place accuracy by adding terms from the series up to the first term that is less than 10^-6 in absolute value. The prototype of your exponent function should be:
Double exponent(double)
You also need a function to calculate the factorial of a number. Write this function and call it from the exponent function. The prototype of this function is:
long factorial(int)
Using the exponent function, define a new function sinh, that calculates (sinh(x)=(e^x-e^-x)/2)
The prototype of the sinh function should be:
double sinh(double)
Write a program that prints a table of values for the function,sinh, for x=-1 to 1 in increments of 0.1.
The output should be:
x=-1.0 sinh(x)=-1.175201
x=-0.9 sinh(x)=-1.026517
x=-0.8 sinh(x)= -0.888106
.... all the way to x=1 sinh(x)=1.175201
Explanation / Answer
#include <stdio.h>
/* declare the functions */
double exponent(double);
long factorial(int);
double sinh(double);
int main()
{
double x;
for (x = -1; x <= 1; x += 0.1)
{
printf("x=%.1f sinh(x)=%.6f ", x,
sinh(x));
}
return 0;
}
double exponent(double x)
{
double result = 0;
double term = 1;
double xp = 1;
int n = 1;
while (term >= 0.000001 || term <= -0.000001)
{
result += term;
xp *= x;
term = xp / factorial(n);
n++;
}
return result;
}
long factorial(int n)
{
long result = 1;
while (n > 1)
{
result *= n;
n--;
}
return result;
}
double sinh(double x)
{
return (exponent(x) - exponent(-x)) / 2.0;
}
output:
x=-1.0 sinh(x)=-1.175201
x=-0.9 sinh(x)=-1.026517
x=-0.8 sinh(x)=-0.888106
x=-0.7 sinh(x)=-0.758584
x=-0.6 sinh(x)=-0.636654
x=-0.5 sinh(x)=-0.521095
x=-0.4 sinh(x)=-0.410752
x=-0.3 sinh(x)=-0.304520
x=-0.2 sinh(x)=-0.201336
x=-0.1 sinh(x)=-0.100167
x=-0.0 sinh(x)=0.000000
x=0.1 sinh(x)=0.100167
x=0.2 sinh(x)=0.201336
x=0.3 sinh(x)=0.304520
x=0.4 sinh(x)=0.410752
x=0.5 sinh(x)=0.521095
x=0.6 sinh(x)=0.636654
x=0.7 sinh(x)=0.758584
x=0.8 sinh(x)=0.888106
x=0.9 sinh(x)=1.026517
x=1.0 sinh(x)=1.175201
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.