
# sinFar.py
# Andrew Davison, ad@coe.psu.ac.th, June 2026
'''
Generate multiple Maclaurin series approximations to
sin(x) using an increasing large numbers of terms.

Plot sin(x) and the series over the x interval
[-5 \pi, 5 \pi] to see how well the series deal with 
approximating the function at a long distance from
the origin.

Each of the series is also printed to stdout, so their
coefficients can be examined.

See sinMac.py for a version of this code which deals with
a small number of terms, close to the origin.

'''

import math
import matplotlib.pyplot as plt
import faUtils
import sympy as sp

xs = faUtils.linspace(-5*math.pi, 5*math.pi, 200)

plt.figure(figsize=(10, 5))


for n in [5, 10, 15, 20, 25]:
  print(f"\nUsing {n} terms:")
  fCoeffs = faUtils.seriesCoefs(sp.sin, nTerms=n,
                                   verbose=True)
  ys = [ faUtils.evalPowers(fCoeffs, x) for x in xs]
  plt.plot(xs, ys, label= f"{n} Terms")


mathFn = [math.sin(x) for x in xs]
plt.plot(xs, mathFn, 'k', label='sin(x)', ls='dashed')

plt.grid()
plt.title('Maclaurin Series sin(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()