
# logCat.py
# Andrew Davison, ad@coe.psu.ac.th, June 2026
'''
Plot f(x) = log(1+x) and several version of its Maclaurin 
series approximation using an increasing number of terms
'''

import math
import matplotlib.pyplot as plt
import faUtils
import sympy as sp

xs = faUtils.linspace(-0.7, 5, 200)
fn = lambda x: sp.log(1+x)

plt.figure(figsize=(10, 5))


for n in [5, 10, 15, 20, 25]:
  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 = [math.log(1+x) for x in xs]
plt.plot(xs, mathFn, 'k', label='log(1+x)', ls='dashed')

plt.grid()
plt.title('Maclaurin Series log(1+x) Approx')
plt.xlabel('x')
plt.ylabel('y')
plt.axhline(y=0, color='black')
plt.axvline(x=0, color='black')
plt.ylim(-3, 3)
plt.legend()
plt.show()