Created
March 15, 2019 16:19
-
-
Save madmann91/3c4c978620e9abb4be39fc2a531b56c8 to your computer and use it in GitHub Desktop.
Chebyshev approximation of arbitrary functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Math.Polynomial | |
import Math.Polynomial.Chebyshev | |
coeff f n j = (2 / nf) * (sum [f(xk) * yk | (xk, yk) <- zip x y]) | |
where | |
x = map ((\x -> cos (pi * (x + 0.5) / nf)) . fromIntegral) [0..n-1] | |
y = map ((\x -> cos (jf * pi * (x + 0.5) / nf)) . fromIntegral) [0..n-1] | |
nf = fromIntegral n | |
jf = fromIntegral j | |
approx f n = foldl1 addPoly $ (constPoly (-0.5 * c0)) : [scalePoly ck (t k) | (ck, k) <- zip c [0..n-1]] | |
where | |
c = map ((coeff f n) . fromIntegral) [0..n-1] | |
c0 = head c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment