Convergence of a Geometric Series

A Geometric Series is a series summing the terms of an infinite geometric sequence in which the ratio of consecutive terms is constant.

Thus, the geometric sequence is an infinite sequence that starts at an initial term and each following term is the result of multiplying the previous one by a fixed ratio. For example, if the initial term is \(1.0\) and the ratio is \(2.0\), the resulting geometric sequence is \(1.0, 2.0, 4.0, 8.0, 16.0, ...\) and the corresponding geometric series is the sum of all these terms is infinite.

When the ratio belongs to the \((-1.0, 1.0)\) interval, the terms converge to \(0.0\) and the geometric series is convergence, i.e. there is a value \(v\) after summing the infinitely many terms. For example, if the initial term is \(1.0\) and the ratio is \(1/2\), the resulting geometric sequence is \(1.0, 1/2, 1/4, 1/8, ...\) and the corresponding geometric series is the sum of all these terms that converges to 2.0.

Our purpose is to compute the approximated convergence value of a given geometric series by summing all the terms that are not neglectible according to a given tolerance value. A term is to be considered neglectible if its absolute value is smaller than the given tolerance.

To that goal please implement the following Python function in the gseries_conv module (gseries_conv.py file):

gseries_conv(t0, r, nd)

such that

given

  • t0, float such that t0 != 0.0

  • r, float such that -1.0 < r < 1.0

  • nd, int such that nd > 0

returns

  • a float, the convergence value (rounded to \(4\) decimals) of the geometric series starting at t0 with ratio r and \(1/10^d\) tolerance.

  • an int, the number of summed terms.

For example:


>>> s, i = gseries_conv(1.0, 1/2, 5)
>>> (s, i) == (2, 17)
True

>>> s, i = gseries_conv(1.0, 1/4, 5)
>>> (s, i) == (1.3333, 9)
True

>>> s, i = gseries_conv(1.0, -1/2, 6)
>>> (s, i) == (0.6667, 20)
True

They are available at the gseries_conv.test file.

Note

  • Observe that the tolerance is specified but giving the number of decimals.

  • Using the while statement is recommended.

  • The given doctests are not exhaustive.