I am trying to write a piece of code in java that needs to calculate the sum of values in following manner for the range [-∞, +∞]
K= +∞
∑ [ f( (2K+1)*x ) - f( (2K-1)*x ) ]
K= -∞
There are two confusions that I am facing with the calculation to produce a computationally feasible and efficient solution:
what value must be assumed for ∞ to approximate the calculation as above?
what minimum value must be used to increment K within loop, assuming that K holds fractional values too.
First of all, in order to get an approximation you need the value to decrease dramatically. For example, -1/x
. If x
increases much the answer will be near zero.
∞
to approximate the calculation as above?for ( float k = 0 ; ; k++ )
{
sum += (((2 * ( k) + 1) * x) - ((2 * ( k) - 1) * x)); //adding from 1 to infinity
sum += (((2 * (-k) + 1) * x) - ((2 * (-k) - 1) * x)); //adding from -1 to infinity
}
but this will result in an infinite loop. You can replace x
with 1/x
and increase x
each time in the loop. The code becomes
if ( (sum - lastsum) < 0.001 )
{
break;
}
lastsum = sum;
}
K
within [the] loop, assuming that K
holds fractional values too?This is according to the type of K
. If it is double
, it could have up to 15 decimal places.
In addition, in the summation, you should move by one, not a fraction. See https://upload.wikimedia.org/math/d/f/2/df26e1cf51b67fbedd01ce9c68cbbef5.png https://en.wikipedia.org/wiki/Summation