function tw = trigquad0(n,alpha,beta)

% by Marco Vianello, University of Padova 
% 8 Nov 2011

% computes the angles theta and weights w of a quadrature formula
% exact for trigonometric polynomials of degree not greater than n 
% on the angular interval [alpha,beta] with beta-alpha<=2*pi

% uses quadvgk, the version of quadgk for vector-valued functions 
% http://www.mathworks.com/matlabcentral/fileexchange/18801

% input
% n: trigonometric degree of exactness 
% alpha,beta: endpoints of the angular interval of length <= 2*pi  

% output
% tw: 2-column array of the 2n+1 angular nodes and weights

% the formula integrates the canonical trigonometric basis with accuracy 
% from about 10^(-15) (small omega) to about 10^(-14) (omega-->pi) 
% WARNING: from n around 200 computation becomes slow and memory 
% problems can occurr (Matlab 7.7.0) 
% robust for n up to 100-120


% omega: half-length of the angular interval, 0<omega<=pi
omega=(beta-alpha)/2;

k=[0:1:2*n];
z=cos((pi*k+pi/2)/(2*n+1));
tw(:,1)=2*asin(sin(omega/2)*z)+(alpha+beta)/2;

mom=(quadvgk(@(t)chebpolys(t,n,omega),[-omega;omega],n+1))';

T=cos((2*(1:n))'*acos(z));
tw(:,2)=((mom(1)+2*(mom(2:n+1))*T)/(2*n+1))';

end


function Y = chebpolys(x,m,omega)
k=(1:m+1);
Y=cos(2*(k'-1)*acos(sin(x/2)/sin(omega/2)));
end


