I can't figure out what I'm doing wrong. I'm ending up with an answer of 0.84965 while it should be much closer to 0.746824
import numpy as np
import math
def f(x):
return np.e**(-x**2)
def chebyshev(a,b,n):
h=(b-a)/n
s=0
r=(b-a)/2
for i in range(n+1):
l=i*h
if i>n/2:
l=h*(n/2-(i-n/2))
val=np.cos(np.arcsin(l/r))*r
s+=f(val)
return s*h
print(chebyshev(0,1,1000))
If I use the equation from page 11 of these notes for the Chebyshev-Gauss approximation, and do:
it gives 0.7468244140713791, which seems to match your expected solution.
Update: Just to note, the above could all be vectorised with NumPy as: