Bode plot in Matlab

Ming Sun

Ming Sun / November 28, 2022

9 min read––– views

The following code first does the Bode plot based on a s domain transfer function. Then it reads the csv data from Simplis POP and AC simulation results. After that, it compares the Bode plot between s domain transfer function and Simplis simulation results.

buck.m
clc; clear; close all;

L = 1e-6;
C = 1e-6;
R = 1;
Vg = 5;

s = tf('s');

Gvd = Vg/(1+s*L/R+L*C*s^2);

h = bodeplot(Gvd);                    % Plot the Bode plot of G(s)
setoptions(h, 'FreqUnits', 'Hz');   % change frequency scale from rad/sec to Hz
set(findall(gcf,'type','line'),'linewidth',2)
grid on;
hold on;

% read simplis simulation results from csv file
data = csvread("simplis.csv", 1, 0);
freq = data(:,1);
mag = data(:,2);
phase = data(:,3);

ax = findobj(gcf, 'type', 'axes');
phase_ax = ax(1);
mag_ax = ax(2);


% append simplis plot to bode plot
plot(phase_ax, freq, phase, 'r--', 'LineWidth', 2);
plot(mag_ax, freq, mag, 'r--', 'LineWidth', 2);
legend('Math', 'Simplis')
Bode plot in Matlab
Fig. 1Bode plot in Matlab

Query y axis range

clc; clear; close all;

s = tf('s');

Rtot = 1e6;
Vref = 1.2;
Vout = 1.8;

ratio = Vref/Vout;
R2 = ratio*Rtot;
R1 = (1-ratio)*Rtot;

C3 = 10e-12;
R3 = 100e3;

C4 = 50e-12;
R4 = 100e3;
C5 = 100e-15;

p1 = 1/2/pi/R3/C3;
p2 = 1/2/pi/R4/C5;
z1 = 1/2/pi/R1/C3;
z2 = 1/2/pi/R4/C4;

H = -(1+s*R1*C3)*(1+s*R4*C4)/s/R1/C4/(1+s*R3*C3)/(1+s*R4*C5);

h = bodeplot(H);                    % Plot the Bode plot of H1(s)
setoptions(h, 'FreqUnits', 'Hz');   % change frequency scale from rad/sec to Hz
set(findall(gcf,'type','line'),'linewidth',2)
grid on;
hold on;

ax = findobj(gcf, 'type', 'axes');
phase_ax = ax(1);
mag_ax = ax(2);
xlim([100, 100e6]);
yrange = ylim(mag_ax);

plot(mag_ax, [p1,p1], yrange, ':', 'LineWidth', 2, "Color", "#3B82F6");
plot(mag_ax, [z1,z1], yrange, ':', 'LineWidth', 2, "Color", "#EF4444");
plot(mag_ax, [p2,p2], yrange, '--', 'LineWidth', 2, "Color", "#3B82F6");
plot(mag_ax, [z2,z2], yrange, '--', 'LineWidth', 2, "Color", "#EF4444");

legend(mag_ax, "Bode plot","pole1","zero1","pole2","zero2");
Bode plot in Matlab
Fig. 2Bode plot in Matlab

Enable phase wrapping

comparision.m
clc; clear; close all;

Ts = 1e-6;
wn = pi/Ts;
Q = -2/pi;
s = tf('s');

H1 = exp(-s*Ts)*s*Ts/(1-exp(-s*Ts));
H2 = 1+s/wn/Q + (s/wn)^2;

opts = bodeoptions('cstprefs');
opts.PhaseWrapping='on';
h = bodeplot(H1, opts);  
hold on;
h = bodeplot(H2, opts);
setoptions(h, 'FreqUnits', 'Hz');   % change frequency scale from rad/sec to Hz
set(findall(gcf,'type','line'),'linewidth',2)
grid on;
hold on;
xlim([10, 10e6]);
legend("exponential", "simplified");
Enable phase wrapping
Fig. 3Enable phase wrapping

Phase wrapping and phase matching

comparison.m
clc; clear; close all;

Vg = 3.8;
V = 20;
L = 1e-6;
C = 10e-6;
fsw = 3e6;
Rsns = 0.3;
R = 20;

Dp = Vg/V;
D = 1-Dp;
Ts = 1/fsw;
Se = (V-Vg)/L*Rsns;
Sr = Vg/L*Rsns;

s = tf('s');

Gvc0 = R*Dp/(2*Rsns + Dp^3*R*Se*Ts/Vg);
wp = 2/R/C;
wn = pi*fsw;
Q = 1/pi/(Dp*(1+Se/Sr)-0.5);
wrhpz = R*Dp^2/L;

Gvc = Gvc0*(1-s/wrhpz)/(1+s/wp)/(1+s/wn/Q+s^2/wn^2);

h = bodeplot(Gvc);                    % Plot the Bode plot of G(s)
setoptions(h, 'FreqUnits', 'Hz');   % change frequency scale from rad/sec to Hz
set(findall(gcf,'type','line'),'linewidth',2);
p = getoptions(h);
p.PhaseMatching = 'on';
p.PhaseMatchingFreq = 1;
p.PhaseMatchingValue = 0;
setoptions(h,p);

grid on;
hold on;

% read simplis simulation results from csv file
data = csvread("simplis.csv", 1, 0);
freq = data(:,1);
mag = data(:,2);
phase = data(:,3);

ax = findobj(gcf, 'type', 'axes');
phase_ax = ax(1);
mag_ax = ax(2);


% append simplis plot to bode plot
plot(phase_ax, freq, phase, 'r--', 'LineWidth', 2);
plot(mag_ax, freq, mag, 'r--', 'LineWidth', 2);
legend('Math', 'Simplis')
xlim([100, 1.67e6]);
Gvc transfer function Bode plot comparison between Mathematics derivation and Simplis
Fig. 4Gvc transfer function Bode plot comparison between Mathematics derivation and Simplis

References and materials

[1] Bode plot document from MathWorks

[2] buck.m - download

[3] simplis.csv - download

[3] xlim documentation at MathWorks


HomeWikis
SnippetsAbout
Google ScholarLinkedIn