function w = chtrfm1(N, v,iflag)

%  The function w =  chtrfm1(N,x,iflag) computes the Chebyshev
%  tranforms w.r.t. to the nodes: x_j=cos(j pi/N)
% 
%  Input:
%  N:          Degree of polynomials, must be even
%  v:          Sequence to be transformed
%  iflag=1:    Spectral  to Physical
%  iflag.ne.1: Physical  to Spectral
%  
%
%  Output:
%  w:       transformed sequence
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (iflag==1)

back=[2*v(1); v(2:N); 2*v(N+1); flipud(v(2:N))];
w=0.5*fft(back);                            % Transform back to
w=w(1:N+1);                                 % physical space

else

w=fft([v; flipud(v(2:N))]);               % Extend and compute fft
w=w(1:N+1).*[0.5; ones(N-1,1); 0.5]/N;    % w contains Chebyshev
                                          % coefficients of v

end

if max(abs(imag(v))) == 0; w = real(w); end    % Real data in,
                                                    %real derivative out

