Loop Shaping Simulator — Bode Plot and Stability Margins
Draw the open-loop frequency response L=C*G_p of a PI controller on a second-order process in real time. Change parameters and watch crossover frequency, phase margin, gain margin and bandwidth respond.
Parameters
Process gain K_p
—
First time constant T_1
s
PI proportional gain K_c
—
PI integral time T_i
s
Added compensator (loop shaping)
Compensator type
Strength α (zero/pole ratio)
—
Center frequency ω_m
rad/s
Second time constant T_2 = 0.5 s is fixed. Frequency range omega in [0.01, 100] rad/s (log scale). "Shape it" inserts the compensator from 0→100%, so you watch the Bode curve bend and the phase margin improve.
Live results (updated every frame)
—
Crossover omega_c
—
Phase margin PM
—
Gain margin GM
—
Bandwidth omega_BW
—
Added phase φ_m
—
Overshoot OS
—
Compensator insertion
—
Stability
Open-loop Bode plot L(jω) — reshaped by the compensator
Blue = shaped |L| / ∠L, grey dashed = original L (no compensator); yellow line = ω_c, yellow dot = phase margin PM, red line = ω_180 (gain margin).
Closed-loop step response — phase margin sets the shape
Larger PM → less overshoot and faster settling. Grey dashed = uncompensated, green = shaped closed-loop response.
Theory & Key Formulas
Cascading the process $G_p(s) = K_p/((T_1 s+1)(T_2 s+1))$ with the PI compensator $C(s) = K_c(1+1/(T_i s))$ gives the open-loop transfer function:
With the defaults (K_p=1, T_1=1, T_2=0.5, K_c=5, T_i=2) both PM and GM are positive, giving a stable design. Bandwidth is estimated as ω_BW ≈ 1.5 ω_c.
What is the Loop Shaping Simulator?
🙋
My control class keeps mentioning "loop shaping". What exactly are we shaping?
🎓
In short, you shape the Bode plot of the open-loop transfer function $L(s) = C(s)\,G_p(s)$. You want high gain at low frequency for tracking, a phase margin of 40 to 60 degrees near the gain crossover frequency omega_c for stability, and enough roll-off at high frequency to suppress noise. On the Bode plot above, where the blue magnitude line crosses 0 dB is omega_c, and how far the green phase line is from -180 degrees there is the phase margin PM.
🙋
Raising K_c moves omega_c to the right. So I just crank K_c up for a fast response?
🎓
Right idea, but there is a wall. Pushing K_c moves omega_c higher and the response gets faster. But real plants keep losing phase at higher frequency, so if omega_c gets too high the PM collapses and eventually goes negative, at which point the loop oscillates. Try sweeping K_c to 20, 30, 50 in the slider — PM will shrink and finally show "no crossover" or a negative value. Rule of thumb: aim for PM 40 to 60 degrees and GM 6 to 12 dB.
🙋
What is the integral time T_i for?
🎓
The integrator in a PI controller pushes the low-frequency gain to infinity so the steady-state error goes to zero. Shrinking T_i strengthens the integral action and raises low-frequency |L| further, but it also adds phase lag near omega_c and eats into PM. In the simulator, drop T_i from 2 to 0.5 — the low-frequency magnitude jumps up, but the phase sticks to -90 degrees longer. A safe starting point in practice is "T_i comparable to the dominant plant time constant T_1".
🙋
A big T_1 means slow response, right?
🎓
Exactly. With larger T_1 the magnitude starts rolling off earlier, so omega_c drops and the closed-loop bandwidth shrinks. Increase T_1 from 1 to 5 and watch omega_c slide left. Classical control often estimates closed-loop bandwidth as 1.3 to 1.7 times omega_c — here we display 1.5 times. For rigorous bandwidth you should look at the closed-loop response separately, but the strength of loop shaping is that one Bode plot tells you about speed, stability and error all at once.
Frequently Asked Questions
With process time constants T_1=1 s and T_2=0.5 s, choosing K_c so that omega_c lands around 2 to 4 rad/s gives a PM in the 40 to 60 degree sweet spot. Setting T_i=2 s on the same scale as T_1 balances strong low-frequency integral action against the extra phase lag near omega_c. Move K_c or T_i and watch the PM and GM cards respond.
When the magnitude |L(jω)| never drops below 0 dB in the displayed range [0.01, 100] rad/s, the gain crossover frequency does not exist and the tool issues a warning. This typically happens when K_c or K_p is so large that the loop gain stays high across the whole range, or so small that the loop is below 0 dB everywhere. Bring the gains back to a physically reasonable range (PI gain 0.1 to 30).
Yes. The open loop here is PI (-90 degrees) plus a second-order process (-180 degrees total), so the maximum phase lag asymptotically approaches -180 degrees but never reaches it at any finite ω. The phase crossover omega_180 then lies outside the displayed range and GM is theoretically infinite — shown as "∞". Real plants have measurement delay and high-frequency poles, so actual GM is finite.
Loop shaping indirectly shapes the sensitivity S=1/(1+L) and the complementary sensitivity T=L/(1+L). At low frequency where |L|>>1 we have S≈1/|L| (disturbance rejection), and at high frequency where |L|<<1 we have T≈|L| (noise rejection). H-infinity design picks weighting functions W_S(s), W_T(s) and directly minimises the norm of [W_S S; W_T T]. The "desired shape of L" learned with this tool gives a good starting guess for those weights.
Real-world applications
Process PID tuning: Temperature, flow and level loops in chemical plants are typically modelled as FOPDT (first-order plus dead-time) plants and closed with PI or PID controllers. Practitioners start from a method like Ziegler-Nichols, then fine-tune by inspecting the open-loop Bode plot for adequate phase and gain margins. The open-loop view here is designed to build the intuition for that "tune → check → adjust" loop.
Servo and motor control: Position and velocity loops on servo motors often run on top of inner current loops. Each cascade level is loop-shaped individually, with the inner loop chosen one decade faster than the outer to maintain a clean separation. Mechanical resonance and inertia mismatch eat into the phase margin, making the Bode-plot-based check the workhorse of servo tuning.
Switched-mode power supplies: The voltage loop of a DC-DC converter needs a type II or type III compensator to handle the LC double pole and still hit a healthy phase margin. Just as in this tool, designers inspect ω_c and PM directly on the Bode plot. Textbooks such as Erickson's "Fundamentals of Power Electronics" cover this as a central topic.
Robotics and automotive motion control: Manipulator joints, vehicle steering, braking and quadrotor attitude loops all rely on inner PI/PID controllers shaped in the frequency domain. Even when MPC or reinforcement learning takes the outer role, classical loop shaping still ensures the inner loops stay stable.
Common pitfalls and warnings
The most common misconception is to assume increasing K_c always makes the response faster. ω_c does grow roughly with K_c, but as ω_c moves higher the plant's accumulated phase lag pulls PM down rapidly, and beyond some point PM goes negative and the closed loop becomes unstable. Try K_c at 5, 10, 30, 100 in the slider — PM collapses from about 60° to 30° to 10° to "negative". Speed and stability are a trade-off, and the PM 40 to 60° rule of thumb is the usual compromise.
The next common trap is to assume a good phase margin alone guarantees a non-oscillatory closed loop. PM only looks at one frequency (the gain crossover); systems with multiple crossings or sharp resonances can have a positive PM yet still pass the Nyquist locus dangerously close to -1. Practitioners also check the Nyquist plot and the peak of |S(jω)| — a sensitivity peak M_S ≤ 2 (i.e. ≤ 6 dB) is a typical robustness target. The simple plant in this tool has only real-axis poles, so PM is a reliable indicator; lightly damped systems need more care.
Finally, do not treat ω_BW ≈ 1.5 ω_c as an exact formula. It is an empirical approximation that works well around PM 60°. With a small PM (resonant) design the ω_BW/ω_c ratio can exceed 2, and with a heavily damped design it can be less than 1. The number shown here is a quick estimate. When you need the true closed-loop bandwidth, compute T(jω) = L(jω)/(1+L(jω)) and find the -3 dB frequency directly.
Set the process gain Kp (slider slKpVal) in units of output per input; typical range 0.5–5.0 for industrial processes.
Adjust the dominant time constant T1 (slider slT1Val) in seconds; e.g., 2.0 s for a thermal or level-control loop.
Configure the PI controller: set proportional gain Kc (slider slKcVal) and integral time Ti (slider slTiVal in seconds).
The simulator computes L(jω) = Kc(1 + 1/jωTi) × Kp/(1 + jωT1)² and renders the open-loop Bode magnitude and phase.
Read crossover frequency ωc, phase margin PM, gain margin GM, and closed-loop bandwidth ωBW from the output labels.
Worked Example
The tool's plant is G_p(s)=K_p/((T_1 s+1)(T_2 s+1)) with T_2=0.5 s fixed. With K_p=2.0, T_1=1.5, PI K_c=1.2, T_i=3.0, the open loop L(jω) crosses 0 dB near ω_c ≈ 1.24 rad/s. The simulator yields phase margin PM ≈ 71°, and because the open-loop phase only asymptotes to −180° (never reaching it), the gain margin GM is displayed as "∞". Approximate bandwidth ω_BW ≈ 1.5 ω_c ≈ 1.9 rad/s. This large PM gives well-damped, robust level control.
Practical Notes
Increase Kc to raise ωc and bandwidth, but watch phase margin; below 30° introduces oscillation and instability near plant resonance.
For processes with unknown T1 (e.g., chemical reactors), use relay-tuning experiments to estimate the open-loop corner frequency before tuning Ti.
Gain margin below 2.0 (6 dB) signals weak robustness to process-gain errors; add integral action conservatively if Kp tolerance exceeds ±20%.
Bandwidth should not exceed 1/10 of the sampling frequency in digital implementations to avoid aliasing and actuator saturation.