
# chebTest.py
# Andrew Davison, ad@coe.psu.ac.th, June 2026
'''
Use faUtils' chebFit(), chebEval(), and printCoefs()
to test an 8-term Chebyshev approximation for f(x).
'''


import math
import faUtils

def f(x):
  return x*x * (x*x - 2.0) * math.sin(x)


nTerms = 8   # try 6, 8, 10
a = -math.pi/2; b = math.pi/2
print(f"Evaluating f(x) using {nTerms} terms in [{a},{b}]:")
coefs = faUtils.chebFit(a, b, nTerms, f)
faUtils.printCoefs(coefs)

print(f"    x            actual       chebyshev     abs error")
for i in range(-4, 5):
  x = i * math.pi/2 / 10
  chebx = faUtils.chebEval(a, b, coefs, nTerms, x)
  print(f"{x:12.6f} {f(x):12.6f} {chebx:12.6f}  {abs(f(x) - chebx):12.6f}")