-2

I am new to c++ and I am trying to implement a function that has a summation between k=1 and infinite:

enter image description here

Do you know how summation to infinite can be implemented efficiently in c++?

CafféSospeso
  • 1,101
  • 3
  • 11
  • 28
  • 1
    If your function tends to zero, you could sum to a large number (INT_MAX), but summing to infinity would take an infinite amount of time. – Phillip Ngan Mar 05 '22 at 05:21
  • @PhillipNgan, this means that it is not possible? INT_MAX approach is the only alternative? – CafféSospeso Mar 05 '22 at 05:25
  • 1
    You won't do an infinite summation. If you code it as a summation, pick an upper limit for `k` based on the term being added being close enough to zero so adding more terms doesn't significantly affect on the result. This works as (for specified values of `t` and `t1`) the magnitude of the term being added decreases with `k`. What is "close enough to zero" depends on what you're using the result for (i.e. you have to decide). You might also try working out if there is a mathematical solution (e.g. if there is some formula that is more easily calculated, that the summation converges to). – Peter Mar 05 '22 at 05:44
  • 1
    The value in `exp()` simplifies to c\*k\*k where c is a constant determined by the other constants in the expression. Does that help? – doug Mar 05 '22 at 05:53
  • 2
    You are probably better off asking on [math.se] how to compute the equation, once you know that the programming is simple – Alan Birtles Mar 05 '22 at 07:53

2 Answers2

3

Mathematically, summing to infinite is perfectly valid (in some contexts). Formally, the sum you've written is the same as taking the limit of the summation for all k from 1 through n as n tends towards infinite.

Computers, however, cannot compute such a summation via brute force; it would take infinite time to iterate over a loop infinitely many times. Other than taking an approximation, you might be able to find a closed form equation for this sum. Unfortunately, it's not just a simple geometric series, so finding a closed form solution may not be possible, and is almost certainly non-trivial.

John
  • 31
  • 3
2

Do you know how summation to infinite can be implemented efficiently in c++?

Nothing of this sort, doing X untill infinity to arrive at any finite result can be done in any language. This is impossible, there is not sugar coated way to say this, but it is how it is for digital computers.

You can likely however make approximations. Example, a sin x is simply an expression that goes on to infinity. But to compute sin x we don't run the loop till infinity, there will be approximations made saying the terms after these are so small they are within the error bounds. And hence ignored.

You have to do a similar approach. Assuming t-t1 is always positive, there will come a k where the term (k.pi/2)^2 is so big that 1/e^(...) will be very small, say below 0.00001 and say you want your result to be in an error range of +- 0.001 and so if the series is convergent (look up converging series) then you can likely ignore these terms. Essentially it's a pen and paper problem first that you can then translate to your code.

themagicalyang
  • 2,493
  • 14
  • 21