function C = Power2Chebyshev(n)
%
% This code returns the transformation 
% matrix that converts a sum of monomials 
% into a sum of Chebyshev polynomials. 
% Written by Ming Gu for MATH 128B, Spring 2010.
%
C = zeros(n+1,n+1);
C(1,1) = 1;
if (n==0)
    return;
end
C(2,2) = 1;
if (n==1)
    return;
end
C(3,1) = 0.5;
C(3,3) = 0.5;
for k=3:n
    C(k+1,1)     = C(k,2)/2;
    C(k+1,2)     = C(k,1)+C(k,3)/2;
    if (k>=4)
        C(k+1,3:k-1) =(C(k,2:k-2)+C(k,4:k))/2;
    end
    C(k+1,k)     = C(k,k-1)/2;
    C(k+1,k+1)   = C(k,k)/2;
end
