% Tony Hyun Kim

% 6.638: PS1, Problem 3d

c = 2.998e8; % Speed of light [m/s]

d = 150e-6;  % GTI cavity length [m]

R1 = 0.04;   % Input mirror reflectivity



N = 1000;

lambda = linspace(798,803,N)*1e-9; % Wavelengths [m]



% Derived parameters

t0 = 2*d/c;

w  = 2*pi*c./lambda;

sR1 = sqrt(R1);



% Computation

x = exp(-i*w*t0); % Defined in pset

r = (-sR1+x)./(1-sR1*x);



phi = 180/pi*angle(r);

phi = unwrap(phi);

plot(w/(2*pi*1e12),phi);

xlabel('f [THz]');

ylabel('\Phi_{GTI} (°)');

grid on;

title('Unwrapped phase of \Phi_{GTI} over 798-803nm');



% Compare against analytic solution

hold on;

phi_a = 180/pi*atan((R1-1)*sin(w*t0)./...

                    (-2*sR1+(1+R1)*cos(w*t0)));

phi_a = unwrap(phi_a);                

plot(w/(2*pi*1e12),phi_a,'r');          

legend('Numerical','Analytic formula given in (c)');