%============================================================
% Tony Hyun Kim
% 6.003 PS#10, Matlab exercise
%============================================================

%------------------------------------------------------------
% Parts (a) and (b): See my handwritten work
%------------------------------------------------------------

%------------------------------------------------------------
% Part (c)
%------------------------------------------------------------
G = 1E5;
c = 2*pi*200;

bH = [G*c];
aH = [1 c];
freqs(bH,aH,linspace(0,2*pi*1000));
title('Frequency response of the simple opamp model');
print -dpdf 10c_freq.pdf

H = tf(bH,aH);
figure;
impulse(H);
grid on;
print -dpdf 10c_impulse.pdf

%------------------------------------------------------------
% Part (e): See also my handwritten notes
%           For this part, I toggled `format long'
%------------------------------------------------------------
K = 1;
[bQ,aQ] = feedback(bH,aH,K,1,-1);
% Output:
% bQ = 1.0e+009 *
%      0   1.25663706143592
% aQ = 1.0e+009 *
%      0.00000000100000   1.25663831807298

K = 2;
[bQ,aQ] = feedback(bH,aH,K,1,-1);
% Output:
% bQ = 1.0e+009 *
%      0   1.25663706143592
% aQ = 1.0e+009 *
%      0.00000000100000   2.51327537950890

K = 0;
[bQ,aQ] = feedback(bH,aH,K,1,-1);
% Output:
% bQ = 1.0e+009 *
%      0   1.25663706143592
% aQ = 1.0e+003 *
%      0.00100000000000   1.25663706143592

%------------------------------------------------------------
% Part (f) and (g): See my handwritten work
%------------------------------------------------------------
K = 5E-5;
[bQ,aQ] = feedback(bH,aH,K,1,-1);
figure;
freqs(bQ,aQ,linspace(0,2*pi*1000));
title('Frequency response of bandwidth-expanded opamp under feedback');
print -dpdf 10g_freq.pdf

H2 = tf(bQ,aQ);
figure;
impulse(H2);
grid on;
print -dpdf 10g_impulse.pdf