find the sum of infinite series

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?

(i know it's not finished)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include<iostream>
#include<cmath>
using namespace std;

int Fact(int n)
	{
		if (n == 0)
			return 0;
		if (n == 1)
			return 1;

		return n * Fact(n - 1);
	}

void main()
{
	setlocale(LC_ALL, "Russian");	
		double x, eps, sum ; 
		sum = 0;
	    cout << "Введите число, которое принадлежит интервалу (-1;1) "; // enter a number (-1;1)
		cin >> x;

		cout << "Ведите точность eps "; // 'enter the accuracy of eps'?
		cin >> eps;

		if (- 1 < x && x < 1)
		{
			for (sum = 0; sum < eps;)
			{

			}
		}


}
Last edited on
You need to revisit your question.

That series
x=(-1;1): 1/x + x/3! - x^3/3*5! + x^5/5*7! - x7/7*9! ....

(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.


Last edited on
thank you!
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace 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';
}


Input x and epsilon: 0.9 1e-10
Sum of series (including 1/x term) is 1.25911
@lastchance thank you lots! yeah, the first term is definitely 1/x, that's why im so lost. but thank you for helping!
Last edited on
Topic archived. No new replies allowed.