
# limitRadius.py
# Andrew Davison, ad@coe.psu.ac.th, June 2026
'''
f(x) = 1/(1 + x^2) has poles in the complex plane
at x = +/-i, giving it a radius of convergence of 1. 
This means that its Maclaurin series diverges for 
|x| > 1, even though f(x) is well-defined on all of [-5, 5].

Plot f(x) and four versions of the Maclaurin series
with increasing numbers of terms. The plots show that
the series cannot reach beyond its radius of convergence
irrespective of the number of terms it uses.

See movedCenter.py for a version using a Taylor series 
expanded around x_0 = 2.
'''

import math
import matplotlib.pyplot as plt
import faUtils


xs = faUtils.linspace(-5, 5, 200)
fn = lambda x: 1/(1 + x*x)


plt.figure(figsize=(7, 5))

for n in [4, 5, 8, 9]:   # four versions of the series
  print(f"\nUsing {n} terms:")
  fCoeffs = faUtils.seriesCoefs(fn, nTerms=n,
                                   verbose=True)
  ys = [ faUtils.evalPowers(fCoeffs, x) for x in xs]
  plt.plot(xs, ys, label= f"{n} Terms")

mathFn = [fn(x) for x in xs]
plt.plot(xs, mathFn, 'k', label='1/(1+x^2)', ls='dashed')

plt.grid()
plt.title(r"Maclaurin Series $\frac{{1}}{{1 + x^2}}$ Approx")
plt.xlabel("x")
plt.ylabel("y")
plt.axhline(y=0, color='black')
plt.axvline(x=0, color='black')
plt.ylim(-5, 5)
plt.legend()
plt.show()
