
function demo_example_sphrect


x=(1/180)*[-125*pi, -67*pi; 41*pi, 65*pi];
[Emh1,cards1,nv]=do_example(x);


x=(1/180)*[-109*pi, -102*pi; 49*pi, 53*pi];
[Emh2,cards2,nv]=do_example(x);

grid on;

subplot(3,1,1), semilogy(nv,Emh1(:,1),'bs-',nv,Emh1(:,2),'r*-')
legend('trig','leg');
subplot(3,1,2), semilogy(nv,Emh2(:,1),'bs-',nv,Emh2(:,2),'r*-')
legend('trig','leg');
subplot(3,1,3),plot(nv,cards1(:,1)./cards1(:,2),'bo-',nv,...
    cards2(:,1)./cards2(:,2),'bv-')



function [Emh,cards,nv]=do_example(x)

nv=2:1:60;

Emh=[];
cards=[];

for n=nv
    
    a=-1+2*rand(100,1);
    b=-1+2*rand(100,1);
    c=-1+2*rand(100,1);
    
    tv=[a'; b'; c'];
    
    [thetaw,xw]=cubature_S2_rects_nodes(x,n,'classic','sphrect');
    
    xx=xw(:,1); yy=xw(:,2); zz=xw(:,3); ww=xw(:,4);
    Ixwc=inteval(n,xx,yy,zz,ww,tv);
    cardcl=length(xx);
    
    [thetaw,xw]=cubature_S2_rects_nodes(x,n,'legendre','sphrect');
    
    xx=xw(:,1); yy=xw(:,2); zz=xw(:,3); ww=xw(:,4);
    cardleg=length(xx);
    
    Ixwl=inteval(n,xx,yy,zz,ww,tv);
    
    
    [thetawR,xwR]=cubature_S2_rects_nodes(x,2*n,'classic','sphrect');
    
    xx=xwR(:,1); yy=xwR(:,2); zz=xwR(:,3); ww=xwR(:,4);
    I=inteval(n,xx,yy,zz,ww,tv);
    
    Ecl=abs((Ixwc-I)./I);
    Ecl_mean=mean(Ecl);
    
    Eleg=abs((Ixwl-I)./I);
    Eleg_mean=mean(Eleg);
    
    
    Emh=[Emh; Ecl_mean Eleg_mean];
    
    cards=[cards; cardcl cardleg];
    
    fprintf('\n \t n: %3.0f',n)
    fprintf('\n \t MEAN REL.ERR. TRIG.: %2.2e LEG.: %2.2e',Ecl_mean,...
        Eleg_mean)
    fprintf('\n \t CARDINALITY: TRIG: %6.0f LEG: %6.0f \n',cardcl,cardleg)
    
end








function Ixw=inteval(n,x,y,z,w,tv)

a=tv(1,:); b=tv(2,:); c=tv(3,:);

fx=(x*a+y*b+z*c+3).^n;
Ixw=w'*fx;


