a3dsfcv
a3dsfcv

Reputation: 1274

Calculate the sum of a series

I need to calculate the sum of an infinite series using mixed and recursive methods. What is the difference between these two methods?

My code, below, shows how I am doing it. Which method am I using?

For example, to compute the series

Sum = -X -(X^2/2) -(X^3/3) -(X^4/4)....etc

I would use this code

sum := -x;
numerator:= x;
n := 2;
current := -x; 

repeat
  numerator := numerator * x;
  previous := current;
  current := numerator/n;  
  n := n + 1;
  sum := sum - current;
until ( abs(previous-current) < eps )

Upvotes: 0

Views: 2959

Answers (3)

The seris that you have presented

-X -(X^2/2) -(X^3/3) -(X^4/4)...

Can be writen as

-(X^1/1) - (X^2/2) - (X^3/3) ... -(X^n/n)

This provide us to

Repeate -(X^i/i) until n where n is abs(previous-current) < eps increasing i each time

This give us:

series

I hope that bellow code meet your expectation.

i := 1;
sum := 0;
current := x;

repeat
  previous := current;
  current  := - exp(x, i) / i; {Here you call a function exp that realise x^i}
  sum      := sum + current;
  i        := i + 1;
until ( abs(previous-current) < eps )

Upvotes: 0

Bert te Velde
Bert te Velde

Reputation: 853

Your problem/question is too vague / too general. I cannot offer more, therefore, than a few general remarks:

For starters, a general method to sum "any" infinite series does not exist. For each series individually you will have to determine how to sum THAT PARTICULAR ONE and this requires, first of all, a study of its convergence-characteristics: a series may converge, diverge, or conditionally converge. Simply adding terms until a term gets smaller than some limit, or until the difference between successive terms becomes smaller than some limit is not a guarantee that you're close to the limiting sum. In fact, it doesn't even guarantee that the sum is finite at all (consider the series 1 + 1/2 + 1/3 + 1/4 ... for example).

Now, let's view your example: -sum( x^n/n; n=1..inf ). This particular series doesn't have a finite sum for any x>=1 and neither for x<-1: it doesn't converge unless -1<=x<1, the terms get larger and larger... (however, read on!).

For abs(x)<1 a 'straightforward' approach of adding successive terms will 'in the end' give you the correct answer, but it will take a long while before you get close to the limiting sum, unless x is very small, and assessing HOW close you are with any finite sub-sum is far from trivial. Moreover, there are better (=faster-converging) methods to sum such types of series.

In this specific case, you may note that it is log(1-x), expressed in a Maclaurin series expansion, so there's not need to set up a tedious summation at all, because the result of the infinite summation is already known.

Now, consider at the one hand that we can easily see that the terms will get bigger and bigger for higher 'n' whenever abs(x) is greater than 1, so that any simple summing-procedure is bound to fail. At the other hand we have this Maclaurin expansion for {log(1-x); -1<=x<1} and we may ponder how it all fits in with the knowledge that surely log(1-x) also exists and is finite for x=-4: could we maybe 'define' the limit of the summation also for x<-1 by this logarithm?! Enter the wonderful world of analytic continuation. I won't go into this, it would take far too much space here.

All in all, summing an infinite series is an art, not something to throw into a standard-summing-machine. Consequently, without specifying which series you wish to sum, you cannot a priori say what method one should apply.

Finally, I do not know what you mean by a "mixed method", so I cannot comment on that, or on its comparison against a recursive method. A recursive method might come in whenever you can write your series in a form that is very similar to the original, but just 'slightly simpler'. An example, not from an infinite series, but from a finite series: the Fibonacci number F(n) can be defined as the finite sum F(N-1)+F(n-2). That is recursion and you 'only' have to know some base-value(s) - i.c: F(0)=F(1)=1 - and there you have your recurrence set-up. Rewriting a series in a recursive form may help to find an analytic solution, or to split off a part that has an analytic solution leaving a 'more convenient' series that lends itself to a fast-converging numerical approach.

Maybe "mixed method" is intended to indicate a mixture of an analytical summation - as with your series: log(1-x) - and some (smart or brute-force) numerical approximation (where, as others pointed out, 'recursive' might be meant to be 'iterative').

To conclude: (a) clarify what you mean by "mixed" and "recursive" methods; (b) be specific about what type of series you need to sum, lest there's no sensible answer possible.

Upvotes: 2

High Performance Mark
High Performance Mark

Reputation: 78316

Some parts of an answer to your question:

I don't know what you mean by a 'mixed' method, though if you had 2 methods and made a new one out of bits of both of them then I guess I could see that you would then have a mixed method. But as a generally-used, off-the-shelf term, it's meaningless to me. Since you contrast it with 'recursive' and since I've already decided that you are not a native English speaker I wonder if you meant to write 'iterative' ? It is very common to find 'iterative' and 'recursive' methods compared and contrasted.

What you have shown us is an 'iterative' method; a simple view is that an iterative method is one which relies on a loop (or loops) as your code does.

Incidentally, I think you could make your code simpler if you recognised that the first term in your series has the same form as all the other terms, you have simplified (X^1)/1 to X which is mathematically correct, but computationally it is often more straightforward to operate on a sequence of identical terms rather than a sequence where the first term differs in form from all the rest.

A recursive method is one which calls itself. Since I suspect that I am helping you with homework I'm not going to write a recursive method for you, but you should be looking for a function which has the approximate form:

sum([]) = 0
sum([a b c d ...]) = a + sum([b c d ...])

Note that the 'function' sum (which is defined in 2 'clauses') appears on both the left-hand side and righ-hand side of the 2nd clause. Note also that on the right it is applied to a sub-set of the input arguments (on the left) which offers the possibility that at some stage the function will terminate.

Upvotes: 1

Related Questions