load(interpol);

p:lagrange([[0,0],[1,16],[2,3],[5,-2],[7,8]]);

define(P(x),p);

P(0);P(1);P(2);P(5);P(7);

kill(x);wxplot2d([p],[x,0,7]);

subreg(a,b,n,ff):=block([v],
v(i):=a+(i-1)*(b-a)/n,
xy:makelist([v(i),ff(v(i))],i,1,n+1)
);

f(x):=sin(x);xy:subreg(0,3*%pi,5,f);x;

Q:lagrange(xy);

kill(x);wxplot2d([Q,sin(x)],[x,0,3*%pi]);

h(x):=1/(1+25*x^2);

traceapprox(a,b,n,ff):=([Q1],
xy:bfloat(subreg(a,b,n,ff)),
Q1:lagrange(xy),
wxplot2d([Q1,ff],[x,a,b])
);

traceapprox(0,3*%pi,5,f);xy;

traceapprox(-1,1,20,h);

T0:chebyshev_t(0,x);T1:chebyshev_t(1,x);T2:chebyshev_t(2,x);T3:chebyshev_t(3,x);T4:chebyshev_t(4,x);

P:chebyshev_t(7,x);

allroots(P);

subcheby(n,f1):=block([u],
u(i):=cos((2*i-1)*%pi/(2*n+2)),
makelist(bfloat([u(i),f1(u(i))]),i,1,n+1)
);

subcheby(7,h);

traceapproxcheby(n,f3):=([Q1],
xy:subcheby(n,f3),
Q1:lagrange(xy),
wxplot2d([Q1,f3],[x,-1,1])
);

traceapproxcheby(10,h);