How to Plot Bode Diagram in MATLAB
This tutorial will discuss how to plot the bode plot of the frequency response of a system using the bode()
function in MATLAB.
Plot the Bode Plot of the Frequency Response of a System Using the bode()
Function in MATLAB
If you want to plot the frequency response or magnitude and phase data of a system model, you can use the bode()
function, which will plot both the magnitude and phase of the frequency response of a given system. For example, let’s create a bode plot of a dynamic system by giving its transfer function to the bode()
function. See the code below.
t_fun = tf([1 2 0.5],[9 1 0]);
bode(t_fun)
Output:
In the above code, we use the transfer function coefficients and the tf()
function to define the dynamic function. The first argument of the tf()
function is the vector containing the coefficients of the numerator, and the second argument is the vector containing the coefficients of the denominator, and the sequence is from higher to lower power. If power is missing, then 0 will be used as its coefficient.
As you can see in the output, the bod()
function automatically gives the plot title and labels. If you want to change these settings, you have to save the magnitude and phase in a separate variable and then plot it using the plot()
function and after that, you can give your desired title and labels to the plot. For example, let’s create the above bode plot using the subplot()
and plot()
functions and change its title using the title()
function. See the code below.
t_fun = tf([1 2 0.5],[9 1 0]);
[magn,phas,w] = bode(t_fun);
figure
subplot(1,2,1)
plot(w,20*log10(magn(:)))
set(gca, 'XScale', 'log')
title('Magnitude')
subplot(1,2,2)
plot(w,phas(:))
set(gca, 'XScale', 'log')
title('Phase')
Output:
As you can see in the output, the plots are the same but with a different title. You can also change other properties like the labels, line styles, plot color using the Color
property, etc. Check this link for more details about the bode()
function.