The question
This is the loop from the Nyquist lesson: a gain K = 4, the plant G(s) = 1/(s+1)³ and negative feedback. Nyquist said it is stable. Now we ask how stable: how far is it from the edge?
Cut the loop at the junction and feed in a sine. It goes once through K and G and comes back changed: scaled by |L(jω)| and shifted by ∠L(jω). Sweeping ω draws the open-loop Bode and Nyquist plots.
Danger: a sine that comes back the same size and turned upside down, |L| = 1 and ∠L = −180°. The junction's minus sign flips it upright again, so it feeds itself for ever. On the plots: 0 dB, −180°, and the point −1.
Gain margin
Slide the test frequency up the phase plot until the sine comes back exactly upside down. −3·atan ω = −180° gives ω = √3 = 1.73 rad/s, the phase crossover frequency. It does not depend on K.
At 1.73 rad/s the sine comes back upside down but only 0.50 as big (−6.02 dB), so it dies out. K could grow 2.0 times, 6.02 dB, before it came back full size. That headroom is the gain margin.
Phase margin
Now slide down the magnitude plot to where the sine comes back exactly the same size: |L| = 1, 0 dB. That happens at ω = 1.23 rad/s, the gain crossover frequency.
At 1.23 rad/s the sine comes back full size but shifted only −152.9°, not −180°. 27.1° more lag, from a delay or a slow sensor, would put it on the edge. That headroom is the phase margin.
Using the margins
The same two numbers are distances from −1. The curve crosses the negative real axis at −0.50: GM = 1/0.50 = 2.0. It crosses the unit circle 27.1° short of −1: that angle is the phase margin.
Turn K up by the gain margin, 4 × 2.0 = 8. The magnitude curve rises 6.02 dB, both crossovers meet at 1.73 rad/s and both margins reach zero. Close the loop: the step response swings for ever.
K = 4 (GM 6.02 dB, PM 27.1°) overshoots 54%. K = 2 (GM 12.0 dB, PM 67.6°) overshoots 30%. A common target is GM above 6 dB and PM of 30° to 60°: our K = 4 is short on phase margin.
What is happening
# step 1/10 · loop # about: Junction Σ, Gain K, Plant G(s) open loop L(s) = K·G(s) = 4/(s+1)³ closed loop y/r = L/(1 + L) Nyquist: no encirclement of −1, so the closed loop is stable. A yes/no answer is not enough: a part wears, a gain drifts. How much change can the loop take before it goes unstable?
The loop is L(s) = K/(s+1)³ with unity negative feedback. Every curve is computed from that formula; the step responses are simulated from the closed-loop equation y''' + 3y'' + 3y' + (1 + K)y = Kr. The Python on the margin steps is plain Python 3 (math only): a bisection finds each crossover, then one line reads the margin. python-control's control.margin returns the same four numbers.