%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Elec 360 - Performance Measures Homework Solution to Extra Problems %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;
zeta=[.1 .5 1 1.5];
beta = sqrt(1-zeta.^2);
wn = 1;

% Compute rise-time
for i=1:length(zeta)
  if zeta(i) < 1
    % These only exist for underdamped systems
    Tr(i) = (pi-atan(beta(i)/zeta(i))) / (wn * beta(i));
    Tp(i) = pi./(wn*beta(i));
    PO(i) = 100*exp(-pi*zeta(i)./beta(i));
  else
    Tr(i) = 0;
    Tp(i) = 0;
    PO(i) = 0;
  end

  if zeta(i) < 0.8
    Tr1(i) = (2.16*zeta(i) + 0.6) / wn;
  else
    Tr1(i) = (4.65*zeta(i) - 1.3) / wn;
  end
end

% Compute remaining performance measures
Ts = 4./(zeta*wn);

% Display answers
Tr
Tr1
Tp
Ts
PO

% Compute step response of system
t=0:.02:14;
for i=1:length(zeta)
  num = wn^2;
  den = [1 2*zeta(i)*wn wn^2];
  G = tf(num, den);
  y(i,:) = step(G, t)';
end

% Plot results
figure(1);
clf;
plot(t,y);
grid on;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Just for the heck of it, generate a continuous surface plot %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
zeta = .1:.1:2;
wn = 1;

t=0:.1:14;
for i=1:length(zeta)
  num = wn^2;
  den = [1 2*zeta(i)*wn wn^2];
  G = tf(num, den);
  y(i,:) = step(G, t)';
end

figure(2);
clf;
[T Z] = meshgrid(t, zeta);
surf(T, Z, y);
xlabel('Time');
ylabel('Damping');
