%% Experiment 6.1
%% Name:
%% Objectives
% To verify the effect of pole location upon stability. To verify the effect upon stability of loop gain in a negative feedback system.
%% Minimum Required Software Packages
% MATLAB, Simulink, and the Control System Toolbox.
%
%
%% Prelab
%% Problem 1
% Fiind the equivalent transfer function of the negative feedback system of Figure P6.19 below if G(s) = \frac{K}{s(s+2)^2} and H(s) = 1.
%
% Answer:
% {G(s) \over 1 + H(s)G(s)} = {K \over s(s+2)^2 + K} = {K \over s^3 + 4s^s + 4s + K}
%
%%
%
%% Problem 2
% For the system of Prelab Problem 1, find two values of gain that will yield closed-loop, overdamped, second-order poles. Repeat this for two gains which will produce underdamped poles.
% Answer:
% {K \over s^3 + 4s^2 + 4s + K} = {\alpha \omega_n^2 \over (s+\alpha) \left(s^2 + 2\zeta\omega_n s + \omega_n^2\right)} = {\alpha \omega_n^2 \over s^3 + 2\zeta\omega_n s^2 + \omega_n^2 s + \alpha s^2 + 2\alpha\zeta\omega_n s + \alpha \omega_n^2} = {\alpha \omega_n^2  \over s^3 + s^2(\alpha + 2\zeta \omega_n) + s(\omega_n^2 + 2 \alpha \zeta \omega_n) + \alpha \omega_n^2}
% K = \alpha \omega_n^2
% \alpha + 2\zeta\omega_n = 4
% \omega_n^2 + 2\alpha\zeta\omega_n = 4
% For second order to dominate, \zeta\omega_n \ll \alpha, which is defined as \alpha \ge 5\zeta\omega_n. In other words, \alpha = q\zeta\omega_n, where q \ge 5.
% Putting all variables in terms of q:
% \alpha = {4q \over q+2}, \zeta = {\pm 2 \over q-2}, \omega_n = \pm2{(q-2) \over q+2}, and K = {16q(q-2)^2 \over (q+2)^3}
% Overdamped (\zeta > 1):
% The condition {\pm 2 \over q-2} > 1is satisified \forall q \in (0,2) \cup (2,4). This corresponds to values of K on the range (0,1.185). Since q<5, the second order term does not dominate. K=1 and K={1/2} are overdamped.
%
% Underdamped \zeta \in [0, 1):
% The condition 0 \le \zeta < 1is satisfied \forall q \in (4, \infty). This corresponds to values of K on the range (1.185,\infty). K=2 and K=3 are underdamped.
%%
%% Problem 3
% For the system in Prelab Problem 1, find the value of gain, K, that will make the system critcally damped.
% Answer:
% The system is critically damped when K=1.185.
%%
%% Problem 4
% For the system of Prelab Problem 1, find the value of gain, K, that will make the system marginally stable. Also, find the frequency of oscialltion at that value of Kthat makes the system marginally stable.
% Answer:
% Marginally stable if real part of second order poles is 0. In other words, \zeta \omega_n = 0. Since \zeta \omega_n = {4 \over q+2}, for this condition to be satisfied, q must go to +\infty.  Finding the corresponding value of K:\lim_{q\to\infty} K = 16. This is the only marginally stable value of K. Taking the limit of q to -\inftyproduces the same result of K=16.
%
%
%
%
%% Problem 5
% For each of Prelab Problems 2, 3, and 4, plot on one graph the pole locations for each case and write the corresponding value of gain, K, at each pole.
% Answer:
%
%
%
%
%
%
%
%
%
%% Lab
%
%% Problem 1
% Using Simulink, set up the negative feedback system of Prelab 1. Plot the step response of the system at each value of gain calculated to yield overdamped, underdamped, critically damped, and marginally stable responses.
% For your Simulink, please provide a screenshot that clearly shows the system.
%
%
%
%% Problem 2
% Plot the step responses for two values of gain, K, above that calculated to yield marginal stability.
%
%
%% Problem 3
% At the output of the negative feedback system, cascade the transfer function G_1(s) = \frac{1}{s^2+4}. Set the gain, K, at a value below that calculated for matginal stability and plot the step response. Repeat for Kcalculated to yield marginal stability.
% For your Simulink, please provide a screenshot that clearly shows the system.
%
%
%
%
%% Postlab
%
%% Problem 1
% From your plots, discuss the conditions that lead to unstable responses.
% Unstable responses occur when the gain overpowers the damping of the system so much that it cannot reach steady state.
%
%
%% Problem 2
% Discuss the effect of Gain upon the nature of the step response of a closed-loop system.
% Increasing the gain decreases the stability of the system, as it increases the amplitude and frequency of oscillation.
%
%