% Tony Hyun Kim
% 6.638: PS5, Problem 5.2c (Fast saturable absorber)
clear all; close all;
params = struct('g0',0.7/2,...
                'l',0.14/2,...
                'T_R',2.7e-12,...
                'tauL',0.87e-6,...
                'E_L',0.2e-6,...
                'q0',0.03/2,...
                'tauA',1e-12,...
                'E_A',0.77e-12);

fun_fsa = @(t,y) p2c_fsa(t,y,params);
t = [0 5e5];
options = odeset('RelTol',1e-6);
[T Y] = ode45(fun_fsa,params.T_R*t,[0 0.09],options);

t = T/params.T_R;
tspan = [min(t) max(t)];

figure;
subplot(211);
plot(t,Y(:,1));
xlabel('t/T_R');
ylabel('P [W]');
xlim(tspan);
title('Case (i): Fast saturable absorber');

subplot(212);
plot(t,Y(:,2)); hold on;
plot(t,params.l+q_fsa(Y(:,1),params),'r--');
xlim(tspan);
xlabel('t/T_R');
ylabel('g and l+q');
legend('g','l+q','Location','Best');

figure;
plot(Y(:,1),Y(:,2));
xlabel('P [W]');
ylabel('g');
grid on;
