% Linearize the simulink model with the alpha look closed
disp(' '); disp('Start here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<'); disp(' ')

disp('Determine the state space model with the first loop closed')
disp(' This step is important!!!!!')
[a1,b1,c1,d1]=linmod('controlBack_1p') %Find linear model
%Units x(1)=y(1)=v [ft/sec]
%      x(2)=y(2)=gamma [rad]
%           y(3)=hdot [ft/sec]
disp('Natural frequencies, damping ratios, and poles of system matrix a')
[Wn,Z]=damp(a1) % Find properties of the poles of linearized system
poles=eig(a1)
PSYS1=ss(a1,b1,c1,d1); %Create a linear state space system
%set(PSYS1,'statename',{'v(f/s)' 'gam(r)','compa1','compa2'})
set(PSYS1,'inputname',{'vCf/s)' 'Bhp'})
set(PSYS1,'outputname',{'v(f/s)' 'gam(r)' 'hdot(f/s)'})

VperVC=zpk(PSYS1(1,1))
disp('Setup for second loop of Backside control strategy')
hdotperBhp=zpk(PSYS1(3,2))
[StaticLoopSensitivity,DCgain,num,den]=getTFdata(hdotperBhp,1,1)
[EssStep,Kp,EssRamp,Kv,EssParabola,Ka,type]=ErrorConst(hdotperBhp)

% Use sisotool to design a compensator for this system.
disp('Save to the workspace AND a file the compensator as a system called Chdot')
sisotool
disp('Save to the workspace AND a file the compensator as a system called Chdot')


% [StaticLoopSensitivity,DCgain,num,den]=getTFdata(sys,Nstate,Ncontrol)
%[StaticLoopSensitivity,DCgain,num,den]=getTFdata(VperAlpha1,1,1)
% sisotool
% GamperAlpha=zpk(PSYS(2,1))
% HdotperAlpha=zpk(PSYS(3,1))
% VperBhp=zpk(PSYS(1,2))
% GamperBhp=zpk(PSYS(2,2))
% HdotperBhp=zpk(PSYS(3,2))
