4

I am trying to calculate Chebyshev nodes to approximate the cos function in the interval [-1,1] with a polynomial of degree 4. In Python3, I used chebyfit from mpmath and chebyshev from numpy.

While I am getting accurate approximations,

nprint(cos(0.6), 12)
nprint(polyval(poly, 0.6), 12)
nprint(np.polynomial.chebyshev.chebval(0.6, c), 12)

0.82533561491
0.825331777049
0.825331777049078

I am not sure why the coefficients differ.

poly, err = chebyfit(cos, [-1, 1], 5, error=True)
nprint(poly)
[0.0399612, 2.11758e-23, -0.499576, -2.64698e-23, 1.0]

c = np.polynomial.chebyshev.chebinterpolate(np.cos, 4)
nprint(c)
[ 7.65197687e-01 -6.51999524e-18 -2.29807158e-01 -3.40959333e-18
  4.99515460e-03]

I am expecting the coefficients to match as the approximation is calculated for the same interval and order.

Devharsh Trivedi
  • 561
  • 8
  • 23
  • The results of `chebinterpolate` must not be simple polynomial coefficients. If you evaluate that polynomial for x=0.6, you do NOT get 0.825331... Just looking at it, for x=0, the answer has to be 0.7651976, but if you run `chebval`, the result is 1 as expected. – Tim Roberts Mar 09 '23 at 01:39
  • Evaluating at `x=0.6`, `1.0 + (-2.64698e-23 * x) + (-0.499576 * (x**2)) + (2.11758e-23 * (x**3)) + (0.0399612 * (x**4))` I get `0.82533161152` and for `7.65197687e-01 + (-6.51999524e-18 * x) + (-2.29807158e-01 * (x**2)) + (-3.40959333e-18 * (x**3)) + (4.99515460e-03 * (x**4))` the result is `0.6831144821561601` – Devharsh Trivedi Mar 09 '23 at 01:47
  • 3
    `chebyfit` doesn't return the Chebyshev coefficients (`chebinterpolate` does) it returns a "normal" polynomial (like using `chebinterpolate` and `cheb2poly`) – harold Mar 09 '23 at 01:47
  • @harold yes that makes sense, as per the documents "(The option to return the coefficients in Chebyshev form should be made available in the future.)" Ref.: https://mpmath.org/doc/current/calculus/approximation.html?highlight=chebyfit#mpmath.chebyfit – Devharsh Trivedi Mar 09 '23 at 01:53

0 Answers0