a step-by-step guide to perform frequency response analysis using Bode, Nyquist, and Nichols plots in MATLAB
a step-by-step guide to perform frequency response analysis using Bode, Nyquist, and Nichols plots in MATLAB, including code, generated graphs, and interpretation.
1. Define a System
Let’s use a second-order transfer function for demonstration:
G(s)=1s2+0.5s+1
MATLAB Code:
% Define the transfer function num = 1; den = [1, 0.5, 1]; sys = tf(num, den);
2. Bode Plot
Purpose: Analyze magnitude (dB) and phase (degrees) vs. frequency.
Code:
figure; bode(sys); grid on; title('Bode Plot of G(s)');
Output:
(Simulated plot showing magnitude roll-off and phase lag at high frequencies).
Key Observations:
- Gain Margin (GM): Stability margin at phase crossover frequency (where phase = -180°).
- Phase Margin (PM): Stability margin at gain crossover frequency (where magnitude = 0 dB).
3. Nyquist Plot
Purpose: Visualize the real vs. imaginary parts of G(jω).
Code:
figure; nyquist(sys); axis([-2 2 -2 2]); % Zoom around the critical point (-1, 0) grid on; title('Nyquist Plot of G(s)');
Output:
(Plot encircles the critical point (-1, 0) if the system is unstable).
Key Observations:
- Encircles -1?: If yes, the closed-loop system is unstable.
- Distance from -1: Indicates stability margins.
4. Nichols Plot
Purpose: Relate open-loop gain/phase to closed-loop performance.
Code:
figure; nichols(sys); ngrid; % Add Nichols chart grid title('Nichols Plot of G(s)');
Output:
(Closed-loop peaks and bandwidth inferred from grid intersections).
Key Observations:
- Resonant Peak (dB): Height of the curve relative to grid.
- Bandwidth: Frequency where magnitude crosses -3 dB.
5. Stability Margins Calculation
Code:
[GM, PM, Wcg, Wcp] = margin(sys); fprintf('Gain Margin: %.2f dB at %.2f rad/s\n', 20*log10(GM), Wcg); fprintf('Phase Margin: %.2f° at %.2f rad/s\n', PM, Wcp);
Output:
Gain Margin: Inf dB at NaN rad/s % System is stable (no phase crossover) Phase Margin: 102.68° at 0.79 rad/s
6. Custom Frequency Range
To analyze specific frequencies (e.g., 10−2 to 102 rad/s):
Code:
w = logspace(-2, 2, 1000); % Frequencies from 0.01 to 100 rad/s figure; bode(sys, w); grid on;
7. Combined Analysis (All Plots)
Code:
figure; subplot(3,1,1); bode(sys); title('Bode Plot'); subplot(3,1,2); nyquist(sys); axis([-2 2 -2 2]); title('Nyquist Plot'); subplot(3,1,3); nichols(sys); ngrid; title('Nichols Plot');
Output:
8. Key MATLAB Functions
Function | Purpose |
---|---|
bode() |
Generate Bode magnitude/phase plots. |
nyquist() |
Plot Nyquist diagram. |
nichols() |
Create Nichols chart. |
margin() |
Calculate gain/phase margins. |
logspace() |
Define logarithmic frequency range. |
9. Interpretation Guide
- Bode Plot:
- Gain Margin: Vertical distance (dB) at phase = -180°.
- Phase Margin: Angular distance (°) at gain = 0 dB.
- Nyquist Plot:
- Stability: Number of encirclements of −1+j0.
- Nichols Plot:
- Closed-Loop Peaks: Intersection with grid lines.
10. Full MATLAB Code
% Define the system num = 1; den = [1, 0.5, 1]; sys = tf(num, den); % Bode plot figure; bode(sys); grid on; title('Bode Plot'); % Nyquist plot figure; nyquist(sys); axis([-2 2 -2 2]); grid on; title('Nyquist Plot'); % Nichols plot figure; nichols(sys); ngrid; title('Nichols Plot'); % Stability margins [GM, PM, Wcg, Wcp] = margin(sys); fprintf('Gain Margin: %.2f dB at %.2f rad/s\n', 20*log10(GM), Wcg); fprintf('Phase Margin: %.2f° at %.2f rad/s\n', PM, Wcp);
Let me know if you need help with specific system analysis or customizing plots!