% Book: Laser Electronics by Verdeyen
% Problem: Chapter 1 problem 6
% Script Author: meg noah meg_noah@student.uml.edu
% note: random coin-flipping yields different
%       results when you run/re-run the script.
function Lasers_Ch1_Pr6
x = 0:0.01:2.0*pi;
N=100;V=zeros(N);dN=1;
coins = fix(rand(N,1)*8+1);
jump=[0,45,90,135,180,-45,-90,-135];
colors=['r','g','b','k','m'];
phase=jump(coins)*pi/180.0;
D = 0;arg = 0;j=0;figure(1);
ylabel('Density of Exposure');
xlabel('Exposure time [T]');
for i = 1:dN:N
    arg = arg + phase(i);j=j+1;
    D = D + (cos((x+arg)/2)).^2;
    Visibility = (max(D)-min(D))/(max(D)+min(D));
    V(i) = Visibility;
    plot(x,D,colors(mod(j,5)+1)),grid on;hold on;
end
figure(2);
plot(V);axis([1 N 0 1]);
ylabel('Fringe Visibility');
xlabel('Exposure time [T]');