function [Q, alpha, Val1, Val2, Val3] = boxd2p1_loop(N)

global T Q1 Q2 mu counter z G

counter = 0;

mu = 0.5; % k_F = 1


G = 0.002i;
T = .002;


L = 1 + 5*T;
L = 1.5*L;

zmi = -L; zma = L; dz = (zma-zmi)/N;

%alpha = (.1):.002: (pi/2 - .1); 
alpha = (.2):.01: (pi/2 - .2); 
%alpha = (.2):.01: (.2); 
%alpha = 0.5
Na = length(alpha);

%Q = .1:.1:2.5;
%Q= sqrt(2) -.1;
Qqc = 2*cos(.5536);
Q = Qqc
NQ = length(Q);

Val1=zeros(NQ,Na);
Val2=zeros(NQ,Na);
Val3=zeros(NQ,Na);

for ia = 1:Na; 
    alpha(ia)
    for iQ = 1:NQ
        
        Q1= Q(iQ)*[cos(alpha(ia)), sin(alpha(ia))];
        Q2= Q(iQ)*[cos(alpha(ia)), -sin(alpha(ia))];

%         tic
%         for z = zmi:dz:zma
            z = 0; % 2D case
            
            Val1(iQ,ia) = Val1(iQ,ia) + quad2d(@integrand21,-L-2*Q(iQ),L+Q(iQ), -L-Q(iQ), L+Q(iQ),'AbsTol', 1e-4);
            Val2(iQ,ia) = Val2(iQ,ia) + quad2d(@integrand22,-L-2*Q(iQ),L+Q(iQ), -L-Q(iQ), L+Q(iQ),'AbsTol', 1e-4);
            Val3(iQ,ia) = Val3(iQ,ia) + quad2d(@integrand23,-L-2*Q(iQ),L+Q(iQ), -L-Q(iQ), L+Q(iQ),'AbsTol', 1e-4);
%         end
%         toc
        
%         Val1(iQ,ia) = Val1(iQ,ia)*dz;
%         Val2(iQ,ia) = Val2(iQ,ia)*dz;
%         Val3(iQ,ia) = Val3(iQ,ia)*dz;
%        Val1 + Val2 + Val3
    end
end

end

%%%%%%% 2D fcn %%%%%
function f = integrand21(x,y) % 1,-1,2,-2 

global T Q1 Q2 mu counter z G

counter = counter + 1;

E1 = (x.^2 + y.^2 + z^2)/2 - mu;
E2 = ((x + Q1(1)).^2 + (y+ Q1(2)).^2 + z^2)/2 - mu;
E3 = ((x + Q1(1) + Q2(1)).^2 + (y+ Q1(2) + Q2(2)).^2 + z^2)/2 - mu;
E4 = ((x + Q2(1)).^2 + (y+ Q2(2)).^2 + z^2)/2 - mu;



% f =   1./(1+ exp(E1/T)).*real(1./(E1-E2+G)).*real(1./(E1-E3+G)).*real(1./(E1-E4+G)) ...
%     + 1./(1+ exp(E2/T)).*real(1./(E2-E1+G)).*real(1./(E2-E3+G)).*real(1./(E2-E4+G))...
%     + 1./(1+ exp(E3/T)).*real(1./(E3-E1+G)).*real(1./(E3-E2+G)).*real(1./(E3-E4+G))...
%     + 1./(1+ exp(E4/T)).*real(1./(E4-E1+G)).*real(1./(E4-E2+G)).*real(1./(E4-E3+G));

f = - 1./(1+ exp(E1/T)).*(real(1./(E1-E2+G)).^2.*real(1./(E1-E4+G)) + real(1./(E1-E2+G)).*real(1./(E1-E4+G)).^2)...
    + 1./(1+ exp(E2/T)).*real(1./(E2-E1+G)).^2.*real(1./(E2-E4+G))...
    + 1./(1+ exp(E4/T)).*real(1./(E4-E1+G)).^2.*real(1./(E4-E2+G));


end

%%%%%%% 2D fcn %%%%%
function f = integrand22(x,y) % 1,-1,-2,2 

global T Q1 Q2 mu counter z G

counter = counter + 1;

E1 = (x.^2 + y.^2 + z^2)/2 - mu;
E2 = ((x + Q1(1)).^2 + (y+ Q1(2)).^2 + z^2)/2 - mu;
E3 = ((x + Q1(1) + Q2(1)).^2 + (y+ Q1(2) + Q2(2)).^2 + z^2)/2 - mu;
E4 = ((x - Q2(1)).^2 + (y- Q2(2)).^2 + z^2)/2 - mu;



% f =   1./(1+ exp(E1/T)).*real(1./(E1-E2+G)).*real(1./(E1-E3+G)).*real(1./(E1-E4+G)) ...
%     + 1./(1+ exp(E2/T)).*real(1./(E2-E1+G)).*real(1./(E2-E3+G)).*real(1./(E2-E4+G))...
%     + 1./(1+ exp(E3/T)).*real(1./(E3-E1+G)).*real(1./(E3-E2+G)).*real(1./(E3-E4+G))...
%     + 1./(1+ exp(E4/T)).*real(1./(E4-E1+G)).*real(1./(E4-E2+G)).*real(1./(E4-E3+G));

f = - 1./(1+ exp(E1/T)).*(real(1./(E1-E2+G)).^2.*real(1./(E1-E4+G)) + real(1./(E1-E2+G)).*real(1./(E1-E4+G)).^2)...
    + 1./(1+ exp(E2/T)).*real(1./(E2-E1+G)).^2.*real(1./(E2-E4+G))...
    + 1./(1+ exp(E4/T)).*real(1./(E4-E1+G)).^2.*real(1./(E4-E2+G));


end



%%%%%% 2D fcn %%%%%
function f = integrand23(x,y)

global T Q1 Q2 mu counter z G

counter = counter + 1;

E1 = (x.^2 + y.^2 + z^2)/2 - mu;
E2 = ((x + Q1(1)).^2 + (y+ Q1(2)).^2 + z^2)/2 - mu;
E3 = ((x + Q1(1) + Q2(1)).^2 + (y+ Q1(2) + Q2(2)).^2 + z^2)/2 - mu;
E4 = ((x + Q2(1)).^2 + (y+ Q2(2)).^2 + z^2)/2 - mu;


f =   1./(1+ exp(E1/T)).*real(1./(E1-E2+G)).*real(1./(E1-E3+G)).*real(1./(E1-E4+G)) ...
    + 1./(1+ exp(E2/T)).*real(1./(E2-E1+G)).*real(1./(E2-E3+G)).*real(1./(E2-E4+G))...
    + 1./(1+ exp(E3/T)).*real(1./(E3-E1+G)).*real(1./(E3-E2+G)).*real(1./(E3-E4+G))...
    + 1./(1+ exp(E4/T)).*real(1./(E4-E1+G)).*real(1./(E4-E2+G)).*real(1./(E4-E3+G));




end



% angle dep:
% alpa res = 0.5
% G = 0.05i;
% T = .05;
% 
% alpha V1 V2 V3 sumV
% 
%     0.1000   53.8701  -33.5263   -8.9032   11.4406
%     0.1500   50.7399  -37.5769   -8.0627    5.1003
%     0.2000   48.9393  -39.8836   -7.5898    1.4659
%     0.2500   47.8519  -41.3694   -7.3913   -0.9088
%     0.3000   47.0588  -42.5539   -7.4006   -2.8957
%     0.3500   46.3262  -43.2973   -7.6193   -4.5905
%     0.4000   45.4182  -43.6215   -8.0090   -6.2123
%     0.4500   44.1432  -43.3468   -8.5544   -7.7581
%     0.5000   42.3486  -42.2046   -9.2015   -9.0575
%     0.5500   39.8314  -39.8374   -9.7628   -9.7689
%     0.6000   36.6566  -35.8682   -9.9361   -9.1477
%     0.6500   33.0171  -30.8885   -9.6044   -7.4758
%     0.7000   29.3231  -26.0122   -9.0950   -5.7841
%     0.7500   25.9190  -22.0477   -8.7442   -4.8728
%     0.8000   23.0282  -19.0210   -8.6604   -4.6532
%     0.8500   20.6657  -16.7697   -8.9199   -5.0240
%     0.9000   18.7809  -15.0393   -9.3892   -5.6476

% G = 0.02i;
% T = .02;
% N= 300;
% 
%     0.1000  144.9981 -117.4566  -11.3953   16.1462
%     0.1500  141.5652 -123.9714   -9.4698    8.1240
%     0.2000  140.3933 -128.2477   -8.5534    3.5922
%     0.2500  141.0851 -131.7467   -8.1574    1.1810
%     0.3000  142.1826 -134.9633   -8.1387   -0.9194
%     0.3500  143.1503 -137.5518   -8.4665   -2.8680
%     0.4000  142.7348 -138.5588   -9.1853   -5.0093
%     0.4500  138.4743 -136.0433  -10.3712   -7.9402
%     0.5000  127.3868 -126.8553  -11.9510  -11.4195
%     0.5500  109.3517 -109.3412  -13.2750  -13.2645
%     0.6000   89.9523  -87.9828  -12.9964  -11.0269
%     0.6500   74.4018  -70.0510  -11.2100   -6.8592
%     0.7000   63.1199  -58.1956  -10.1390   -5.2148
%     0.7500   54.9768  -49.9966   -9.6795   -4.6993
%     0.8000   48.5486  -43.8863   -9.6086   -4.9463
%     0.8500   43.4887  -39.0870   -9.8934   -5.4918
%     0.9000   39.6768  -35.5059  -10.6630   -6.4920
% 
% G = 0.01i;
% T = .01;
% N = 500;
%     0.1000  288.6498 -260.3553  -12.4313   15.8633
%     0.1500  286.5061 -269.1382   -9.9312    7.4367
%     0.2000  288.3985 -276.1110   -8.7358    3.5516
%     0.2500  292.5888 -283.5035   -8.2570    0.8283
%     0.3000  296.6871 -291.4071   -8.1256   -2.8456
%     0.3500  305.8878 -299.9499   -8.4457   -2.5078
%     0.4000  312.6440 -308.0150   -9.1948   -4.5658
%     0.4500  313.0981 -309.3645  -10.7093   -6.9758
%     0.5000  283.3645 -282.5908  -13.2354  -12.4618
%     0.5500  220.6041 -220.3821  -14.8472  -14.6252
%     0.6000  171.8444 -169.1310  -13.9214  -11.2080
%     0.6500  141.1676 -136.2459  -11.4266   -6.5049
%     0.7000  119.9696 -114.7328  -10.3950   -5.1582
%     0.7500  104.3770  -99.3195   -9.9480   -4.8906
%     0.8000   92.3727  -87.6062   -9.8761   -5.1096
%     0.8500   83.0153  -78.4789  -10.1561   -5.6197
%     0.9000   75.4831  -71.1998  -10.8962   -6.6129
% 
% G = 0.02i;
% T = .01;
% N = 500;
%     0.4000  148.4464 -143.9153   -9.0515   -4.5203
%     0.4500  145.9764 -142.8341  -10.4340   -7.2917
%     0.5000  132.8584 -132.0889  -12.5406  -11.7711
%     0.5500  109.1229 -109.0044  -14.0185  -13.9001
%     0.6000   87.6004  -85.1598  -13.3095  -10.8689
% 
% G = 0.005i;
% T = .005;
% N = 1000
%     0.4000  646.3392 -641.5846   -9.1366   -4.3819
%     0.4500  665.6744 -661.4145  -10.6429   -6.3830
%     0.5000  612.1354 -610.8717  -14.1041  -12.8405
%     0.5500  431.9896 -431.4629  -15.5122  -14.9855
%     0.6000  335.2559 -332.3378  -14.3120  -11.3940
