
# movedCenter.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 the function itself
is well-defined on all of [-5, 5].

In limitRadius.py, the four versions of the Maclaurin series
for f(x) have increasing numbers of terms but the plots show 
that the series cannot reach beyond its radius of convergence.

This version of the code utilizes a Taylor series
expanded around x_0 = 2. This extends the radius of convergence
to sqrt(5) which is visible in the wider span of the plots.
"""

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, x0=2, nTerms=n,  # CHANGED
                                   verbose=True)
  ys = [ faUtils.evalPowers(fCoeffs, x, x0=2) for x in xs]
  plt.plot(xs, ys, label= f"{n} Terms at x0=2")

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

plt.grid()
plt.title(r"Taylor 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()
