RL = 3.4;
w = logspace(-1,RL);

Gp = (-1-j)*exp(-0.001*j*w)./(w.^(3/2));

% Compensator
K = 1.5e4*1;
T = 1/785;
Gc = 1/10*(1+10*T*j*w)./(1+T*j*w);

L = K*Gc.*Gp;

subplot(211);
mag = abs(L);
loglog(w,mag);
xlim([1e-1 10^RL]);
xlabel('\omega');
ylabel('Loop transfer magnitude');
title('6.302 PS#7, Problem 5');

subplot(212);
angle = 180/pi*phase(L);
semilogx(w,angle);
xlim([1e-1 10^RL]);
xlabel('\omega');
ylabel('Loop transfer phase');
title('The phase margin is \phi_m = 61.7^\circ');
% Find the phase margin
for k = 1:length(w)
    if (mag(k)<1)
        break;
    end
end
Crit_phase = 1/2*(angle(k-1)+angle(k));
Pm = abs(-180-Crit_phase)