% L82_dfan.m - ducted fan computations for Lecture 9.2
% RMM, 19 Nov 02
%
% This file contains the code corresponding to CDS 110 Lecture 9.1
%
% Note that we include a small damping factor in the dynamics to keep
% MATLAB happy (it doesn't like pure imaginary poles).

% System parameters
J = 0.0475;			% inertia around pitch axis
m = 1.5;			% pitch of fan
r = 0.25;			% distance to flaps
g = 10;				% gravitational constant
d = 0.001;			% damping factor (estimated)
l = 0.05;			% offset of center of mass

% Transfer functions for dynamics
Pi = tf([r], [J d m*g*l]);		% inner loop (pitch)
Po = tf([1/m], [1 0 0]);		% outer loop (position)

%
% Inner loop control design
%
% This is the controller for the pitch dynamics.  Goal is to have
% fast response for the pitch dynamics so that we can use this as a 
% control for the lateral dynamics
%

% Design a simple lead controller for the system
k = 15*300;  a = 25;  b = 300;
Ci = k*tf([1 a], [1 b]);		% lead compensator
Li = Pi*Ci;
figure(1); margin(Li); title('Inner loop margins');

% Compute the varioius sensitivity functions
Si = 1/(1+Li);
PSi = Pi/(1+Li);
CSi = Ci/(1+Li);
Ti = Li/(1+Li);

figure(2);
  subplot(221); bodemag(Si); grid on; title('S')
  subplot(222); bodemag(PSi); grid on; title('PS')
  subplot(223); bodemag(CSi); grid on; title('CS')
  subplot(224); bodemag(Ti); grid on; title('T')
  print -dpng dfp4bode.png

figure(3); 
  subplot(221); step(Si, 0.15); grid on; title('S')
  subplot(222); step(PSi, 0.15); grid on; title('PS')
  subplot(223); step(CSi, 0.15); grid on; title('CS')
  subplot(224); step(Ti, 0.15); grid on; title('T')
  print -dpng dfp4step.png
