x=(-1;1): 1/x + x/3! - x^3/3*5! + x^5/5*7! - x7/7*9! ....
find the sum of terms of infinite series with a given accuracy eps by the user
i know i should use 'for' but i just dont get how to do it for infinite series. i also think that what i did for factorial is useless in a way. did i miss somothing? i definitely did but i dont understand what. could you explain please?
(with a strange term 1/x in) isn't going anywhere when x=0.
For the remainder of the series:
x/3! - x^3/3*5! + x^5/5*7! - x7/7*9! ....
Start with term x/6 and set the sum equal to it.
Multiply your term by -(1/3)*x*x/(4 * 5) and add it to the sum.
Multiply your term by -(3/5)*x*x/(6 * 7) and add it to the sum.
Multiply your term by -(5/7)*x*x/(8 * 9) and add it to the sum.
Spot the pattern? Stop when the absolute value of the term is less than eps.
The one thing you should NOT do is work out a factorial for every term, despite the way it looks as if it is defined.
Well, it would go something like this. However, I'd be surprised if that 1/x term was correct, and there's absolutely no reason to restrict it to the interval (-1,1) either.
#include <iostream>
usingnamespace std;
double series( double x, double eps )
{
double term = x / 6.0; // x / 3!
double sum = term;
for ( int n = 3; abs( term ) > eps; n += 2 )
{
term *= -x * x * ( n - 2 ) / ( n * ( n + 1 ) * ( n + 2 ) );
sum += term;
}
return sum;
}
int main()
{
double x, eps;
cout << "Input x and epsilon: "; cin >> x >> eps;
cout << "Sum of series (including 1/x term) is " << 1.0 / x + series( x, eps ) << '\n';
}