function [C,X,Y] = cheby(fun,n,a,b)
% [C] = cheby(fun,n)
% [C,X,Y] = cheby(fun,n)
% [C,X,Y] = cheby(fun,n,a,b)
% Construction of the collocation polynomial.
% The method is Chebyshev interpolation.
% fun is the 'string function', input.
% n is the degree, input.
% a is the left endpoint, input.
% b is the right endpoint, input.
% C is the coefficient list for the polynomial, output.
% X are the abscissas, output.
% Y are the ordinates, output.
if nargin==2, a=-1; b=1; end
d = pi/(2*n+2);
C = zeros(1,n+1);
for k=1:n+1,
X(k) = cos((2*k-1)*d);
end
X = (b-a)*X/2+(a+b)/2;
x = X;
Y = eval(fun);
for k = 1:n+1,
z = (2*k-1)*d;
for j = 1:n+1,
C(j) = C(j) + Y(k)*cos((j-1)*z);
end
end
C = 2*C/(n+1);
C(1) = C(1)/2;