I'm doing a homework assignment where I need to use the McLaurin Series to evaluate for cosh(x) ans sin(x). Each time the program runs, the user inputs a number from 0-10 to specify how many terms to evaluate the series too. The user then inputs the value of x to evaluate to. Once the user hits enter, 10 increments of the series will be calculated and printed out. In addition, I have to find the exact error as well. To do this, I created several different functions for both sinh and cosh that performed the calculations of a specific term. For example: the code below evaluates the term up to 10!.
void cosFunction10(double power, double value)
{
double cosh_x = 0;
double math_cosh = 0;
double exact_error;
double x;
if (value < 0)
x = -0.1*0;
for (int i = 0; i < 11; ++i)
{
cosh_x = (1.0 + ((x*x)/(2.0*1.0)) + ((x*x*x*x)/(4.0*3.0*2.0*1.0)) + ((x*x*x*x*x*x)/(6.0*5.0*4.0*3.0*2.0*1.0)) + ((x*x*x*x*x*x*x*x)/(8.0*7.0*6.0*5.0*4.0*3.0*2.0*1.0)) + ((x*x*x*x*x*x*x*x*x*x)/(10.0*9.0*8.0*7.0*6.0*5.0*4.0*3.0*2.0*1.0)));
math_cosh = cosh(x);
exact_error = (math_cosh - cosh_x);
cout << setiosflags(ios::scientific) << setprecision(3) << x << "\t";
cout << setiosflags(ios::scientific) << setprecision(5) << cosh_x << "\t";
cout << math_cosh << "\t";
cout << exact_error << endl;
x = x + (0.1*value);
}
}
If I run the program and put in these values: 10 (the nth term) and -6 (value for x).
This is what I should get: (2nd increment)
x Series Exact (using the cmath functions) Exact % Error
-6.000e-001 1.18547e+000 1.18547e+000 0.00000e+000
The next 2 out puts would be the same for the exact error, until I get to the 4th increment
x Series Exact (using the cmath functions) Exact % Error
-1.800e+100 3.10747e+000 3.10747e+000 -7.67243e-005
When I run my code, I do not get the above results: (2nd increment)
x Series Exact (using the cmath functions) Exact % Error
-6.000e-001 1.18547e+000 1.18547e+000 4.55325e-012
So it seems that something is going wrong because my exact error isn't even close to the same as to what I'm given. I understand that there may be slightly different values, which is expected but not to this extent.
Any suggestions would be much appreciated,
Thank you