I'm trying to write a procedure in maple to that will approximate a function f as a Chebyshev polynomial of degree n on the interval[-1..1] without using any built-in Maple functions relating to Chebyshev polynomials. http://en.wikipedia.org/wiki/Chebyshev_polynomials
For instance a procedure CPlot so that ,for example, CPlot(f,[2,3,4]) produces a plot of the function f on [-1, 1] together with ,in a different color, its 2nd,3rd and 4th Chebychev approximations. It should work for arbitrary lists of arbitrary length as the second argument. This is my current code:
ChebT := proc(n,x)
local i,firstT,secondT,generalT;
firstT := 1;
if n=0 then return firstT end if;
secondT := x;
if n=1 then return secondT end if;
for i from 1 to n-1 do
generalT := 2*x*secondT - firstT;
firstT := secondT;
secondT := generalT;
end do;
return expand(secondT)
end proc:
CPlot:=proc(f,L::list)
local j, K,num_ip,num_prj,c,chb;
K:=f(x);
for j from 1 to nops( L) while j<(nops(L)+1) do
num_ip := (f,g) -> evalf(Int(f*g/sqrt(1-x^2),x=-1..1)*2/Pi);
num_prj := (f,n) -> seq(num_ip(f,ChebT(i,x)),i=0..n);
c := num_prj(f(x),L[j]);
chb := c -> c[1]/2 + sum(c[i]*ChebT(i-1,x),i=2..nopc(c)); *
K:=K, chb([c]);
end do;
plot([K], x=-1..1, colour=[green, red, blue, yellow],linestyle=[1,2,3,4], thickness=[5,2,3,4]);
end proc:
When trying:
f:=x->x^2:
ChebyPlot(f,[2,5,10]);
I get "Error, (in ChebT) final value in for loop must be numeric or character" on line *
And if I use the build in function T for Chebyshev polynomial, by calling with(orthopoly,T) ,instead of ChebT,which I tested before and it works,all plots on the graph look the same. Any suggestions?