' APPROXIMATION OF A FUNCTION WITH THE CHEBYSHEV POLINOMIALS (Chebyshev interpolation)
' The n-th order Chebyshev polinomial (for |x|<=1) is defined as
' Tn(x) = cos(n·arccosx)
' Using addition theorem for cosine function, it can be shown that
' Tn+1(x) = 2x·Tn(x) - Tn-1(x) for n>=1 and therefore :
' T0(x) = 1 : T1(x) = x : T2(x) = 2x^2 - 1 : T3(x) = 4x^3 - 3x ...and so on
' To approximate an arbitrary function f(x) in the interval[a,b], we replace the independent variable x by the normalized
' Variable x1 for the interval [-1,+1] with the conversione x1 = (x-0.5(a+b))/0.5(a+b)
pi = 3.141592653589793
nomainwin
OPEN " - CHEBYSHEV INTERPOLATION - " for graphics_fs_nsb as #1
#1, "trapclose [quit]"
#1, "down ; fill black"
#1, "font Verdana 12 ; size 1"
#1, "color green"
' In the Chebyshev interpolation we must select a number of not equally spaced 'x' values for which 'y' has to be returned
' In our case a trigonometric function effe(x) has been selected to generate 'y' values.
' 50 'x-y' pairs has been selected. This number can be varied for a greater/lower precision. Make a trial
n = 50
dim x(n),y(n),c(n)
a = 0 : b = 9
for k = 0 TO n
x(k) = (b - a)/2*COS(pi*(2*k + 1)/(2*n + 2)) + (a + b)/2
y(k) = effe(x(k))
next k
' Calculate c(j) , the Chebyshev coefficients
for j = 0 to n
c(j) = 0
for k = 0 TO n
c(j) = c(j) + y(k)*cos(j*pi*(2*k + 1)/(2*n + 2))
next k
c(j) = 2/(n + 1)*c(j)
next j
' Plot the trigonometric function effe()
i = 0
for x1 = a to b step 0.02
y1 = effe(x1)
if x1 = a then
#1,"place ";x1*100 + 100;" ";800 - y1
else
#1,"goto ";x1*100 + 100;" ";800 - y1
end if
next x1
' Now plot the points calculated by the Chebyshev interpolation
#1,"color red"
i = 0
for x1 = a to b step 0.2
' calcolo f(n)(x)
fC = 0
x2 = (x1 - (a + b)/2)/(b - a)*2
for j = 0 to n
if x2 = 1 or x2 = -1 then
fC = fC + c(j)*cos(j*(pi/2 - x2*pi/2))
else
fC = fC + c(j)*cos(j*(pi/2 - atn(x2/sqr(1 - x2*x2))))
end if
next j
fC = fC - c(0)/2
#1, "line ";x1*100 + 90;" ";800 - fC;" ";x1*100 + 110;" ";800 - fC
#1, "line ";x1*100 + 100;" ";790 - fC;" ";x1*100 + 100;" ";810 - fC
i = i + 1
next x1
wait
[quit]
close #1
end
function effe(x3)
effe = sin(x3*9)*150 + cos(x3* 3.3)*50 + 250
end function