
# expFar.py
# Andrew Davison, ad@coe.psu.ac.th, June 2026
# inputs: 5, 10, 15, 20, 25
'''
Generate successive Maclaurin series approximations to
exp(-x) using increasing numbers of terms.

'''

import math
import matplotlib.pyplot as plt
import faUtils
import sympy as sp

xs = faUtils.linspace(-1, 20, 200)
fn = lambda x: sp.exp(-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.exp(-x) for x in xs]
plt.plot(xs, mathFn, 'k', label='exp(-x)', ls='dashed')

plt.grid()
plt.title('Maclaurin Series exp(-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()