echo on

% BUILD PLANT AND WEIGHTS

g = tf(1,[1 2]);
w2 = tf([1 1],[0.2 20]);

% BODE PLOT FOR W2

om = logspace(-2,3,100);
w2_f = frd(w2,om);
bodemag(w2_f)
grid
pause

% BUILD AND PLOT CONSTRAINTS

omlow = logspace(-2,0,50);
w1low = 13.15;
w2low = abs(frd(w2,omlow));
blow = w1low/(1-w2low);

omhigh = logspace(2,3,50);
w1high = 0;
w2high = abs(frd(w2,omhigh));
bhigh = (1-w1high)/w2high;
bodemag(blow,bhigh)
grid
pause

% CHOOSE LOOP GAIN AND PLOT VS CONSTRAINTS

loop = drawmag(fcat(blow,bhigh));
pause

loop = tf(20,[1 1]);
loopold = loop;
loop_f = frd(loop,om);
bodemag(blow,bhigh,loop_f) % see if you meet bounds
grid
pause

% CHECK ROBUST PERFORMANCE (NS AND BOUNDS)

s = 1/(1+loop);
t = loop/(1+loop);
max(real(pole(t))) % check NS
pause

omextra = logspace(0.01,3,50);
w1slow = frd(13.15*s,omlow);
w1shigh = frd(tf(0,1),omextra);
w1s = fcat(w1slow,w1shigh);
w2t = frd(w2*t,[omlow omextra]);
gam = abs(w1s)+abs(w2t);
bodemag(gam) % check peak < 1
grid
pause

% MOVE TO DESIGN 2 - RECALCULATE CONSTRAINTS AS NEEDED

w1low = 93.5;
blow = w1low/(1-w2low);
bodemag(blow,bhigh)
grid
pause

% NEW LOOP GAIN

loop = drawmag(fcat(blow,bhigh));
pause

loop = tf([1 10],[1 1])*loopold;
loop_f = frd(loop,om);
bodemag(blow,bhigh,loop_f) % see if you meet bounds
grid
pause

% CHECK ROBUST PERFORMANCE AGAIN

s = 1/(1+loop);
t = loop/(1+loop);
max(real(pole(t))) % check NS
w1slow = frd(93.5*s,omlow);
w1s = fcat(w1slow,w1shigh);
w2t = frd(w2*t,[omlow omextra]);
gam = abs(w1s)+abs(w2t);
bodemag(gam) % check peak < 1
grid

echo off

