Chebyshev coefficients
Chebyshev coefficients is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Chebyshev coefficients are the basis of polynomial approximations of functions. Write a program to generate Chebyshev coefficients.
Calculate coefficients: cosine function, 10 coefficients, interval 0 1
C
<lang C> // Program to calculate Chebyshev coefficients // Code taken from Numerical Recipes in C 1/e
- include <math.h>
- define PI 3.141592653589793
void chebft(float a, float b, float c[], int n, float (*func)()) {
int k,j; float fac,bpa,bma,f[300]; bma = 0.5 * (b-a) bpa = 0.5 * (b+a) for(k = 0;k<n;k++) { float y = cos(PI*(k+0.5)/n); f[k] = (*func)(y*bma+bpa); } fac = 2.0/n; for (j = 0;j<n;j++) { double sum = 0.0; for(k = 0;k<n;k++) sum += f[k] * cos(PI*j*(k+0.5)/n); c[j] = fac*sum; }
} </lang>
J
From 'J for C Programmers: Calculating Chebyshev Coefficients [[1]] <lang J> chebft =: adverb define
f =. u 0.5 * (+/y) - (-/y) * 2 o. o. (0.5 + i. x) % x
(2 % x) * +/ f * 2 o. o. (0.5 + i. x) *"0 1 (i. x) % x
) </lang> Calculate coefficients: <lang J>
10 (2&o.) chebft 0 1
1.64717 _0.232299 _0.0537151 0.00245824 0.000282119 _7.72223e_6 _5.89856e_7 1.15214e_8 6.59629e_10 _1.00227e_11 </lang>