%------------------------------------------------------------

% Tony Hyun Kim

% 6.302 PS6, Problem 2

%------------------------------------------------------------

N = 1000;

w = logspace(-2,4,N);

s = j*w;



La = 20./(s/5+1);

Lb = 20./((s/5+1).*(s/10+1));

Lc = 100./(s.*(s/10+1).*(s/1000+1));

Ld = 100./(s.*(s/10+1).*(s/100+1));

Le = 500*(s/5+1)./(s.^2.*(s/100+1).*(s/1000+1));



L = Le;



r = abs(L);

phi = phase(L);



for k = 1:N

    if(r(k)<1)

        break;

    end

end

w_c = 1/2*(w(k)+w(k-1))

% Critical phase corresponding to w_c

phi_c = 1/2*(phi(k)+phi(k-1));

phi_M = 180/pi*(pi+phi_c)



for k = 1:N

    if (phi(k)<-pi)

        break;

    end

end

w_phi = 1/2*(w(k)+w(k-1))

gain_c = 1/2*(r(k)+r(k-1));

g_M = 1/gain_c



