lambda = 4500*10^-7; % in mm
L = 100; % in mm
p = 3600; % line density per mm

N = p*L; % Total number of reflectors
a = 1/p; % Distance between reflectors

d = 0.00005;

center = asin(lambda/(2*a)); % first order, Littrow configuration

theta = linspace(center-d,center+d,10000);

alpha = 2*pi*a/lambda*sin(theta);
T = length(theta);
I = zeros(1,T);
for j = 1:T
    if (sin(alpha(j)) ~= 0)
        I(j) = 1/N^2*(sin(N*alpha(j))./sin(alpha(j))).^2;
    else
        I(j) = 1;
    end
end

%plot(alpha,I);
plot(180/pi*theta,I);
xlim(180/pi*[center-d center+d]);
xlabel('Exit angle');
ylabel('Intensity normalized to peak');
title('Diffracted light intensity in first order of Littrow configuration');
text(54.0935,0.7,'Incident light: 450nm','fontsize',12);
text(54.0935,0.64,'Grating: L = 10cm, 3600 lines per mm','fontsize',12);
