 # The Easiest Way to Draw a BODE Plot in LaTeX!

In this tutorial, we will learn how to draw a Bode Plot in LaTeX using bodegraph package. The package developed by Robert Papanicola provides also facilities to draw Nyquist and Black plots (not considerd in this post)

Special thanks to Catalin for suggesting this interesting subject.

Hi, your examples and the way you explain them really stand out from what you can usually find on the web. I see that you accept new topics to write about. I would be really interested in the following: “How to plot transfer functions”.

- Catalin -

## 1. Semi-Log Grid

Let's start by creating the semi-log grid: the frequency axis on a logarithmic scale, magnitude and phase on a linear scale. This can be done using the command $\verb|\semilog|$ or $\verb|\semilog*|$ as follows:

\documentclass{standalone}

\usepackage{bodegraph} %Bode plot package

\begin{document}

\begin{tikzpicture}[yscale=3/40,xscale=6/5]

\semilog{-2}{3}{-20}{20}

\end{tikzpicture}

\end{document} The command has four inputs: Min and max power of 10 of the frequency axis, min and max of magnitude or phase. In the above example, the frequency ranges from $10^{-2}$ to $10^{3}$. Hence, we have -2 and 3 as inputs of the semilog command. The y-axis ranges from -20 to 20 which are provided directly.

It should be noted that the image size depends on the provided inputs. For example, the image height is 40cm (-20cm to 20cm) and width is 5cm (-2cm to 3cm) . To this end, we add the scaling options $\verb|yscale=3/40|$ and $\verb|xscale=6/5|$ to the  $\verb|tikzpicture|$ environment. In this case, the illustration has the size 6cmX3cm.

The $\verb|\semilog*|$ add more vertical help lines (half value):

\documentclass{standalone}

\usepackage{bodegraph} %Bode plot package

\begin{document}

\begin{tikzpicture}[yscale=3/40,xscale=6/5]

\semilog*{-2}{3}{-20}{20}

\end{tikzpicture}

\end{document} ### - Change the Grid Style

By using the command $\verb|\tikzet|$, we can change the style of main semi-log lines (black lines), the intermediate semi-log lines (blue lines), and semi-log half lines (dashed lines). Here is the corresponding LaTeX code:

\documentclass{standalone}
\usepackage{bodegraph}

\begin{document}

\begin{tikzpicture}[yscale=3/40, xscale=6/3]
\UnitedB
\tikzset{
semilog lines/.style={black},
semilog lines 2/.style={ blue},
semilog half lines/.style={dashed},
semilog label x/.style={below,font=\tiny},
semilog label y/.style={above,font=\tiny} }
\semilog*{-2}{3}{-20}{20}
\end{tikzpicture}

\end{document} Changing the style of semi-log grid of Bode Plot

You can change the line width, color and its style (dashed, dotted, etc). Labels style can be also modified.

• The command $\verb|\UnitedB|$ adds labels to x-axis and y-axis to the magnitude plot. $\verb|\UniteDegre|$ can be used for the phase plot.

You may ask, how one can draw two plots, one for the magnitude and one for the phase?

Answer: use scope environment as follows:

\begin{tikzpicture}
% Grid style
\tikzset{
semilog lines/.style={black},
semilog lines 2/.style={gray},
semilog half lines/.style={gray, dotted},
semilog label x/.style={below,font=\tiny},
semilog label y/.style={above,font=\tiny} }

% Magnitude Plot
\begin{scope}[xscale=7/5, yscale=3/40]
\UnitedB
\OrdBode{10}
\semilog*{-2}{3}{-30}{10}
\end{scope}

%Phase plot
\begin{scope}[yshift=-3.5cm,xscale=7/5,yscale=3/180]
\UniteDegre
\OrdBode{30}
\semilog*{-2}{3}{-180}{0}
\end{scope}

\end{tikzpicture}

• The scaling has been moved to each scope as each plot has different y-axis domain. It should be noted that both plots have the same frequency domain which means $\verb|xscale|$ can be moved back to the options of the $\verb|tikzpicture|$ environment.
• We have added $\verb|yshift=-3.5cm|$ to the phase plot to move it down by 3.5cm.
• We used the command $\verb|\OrdBode{10}|$ in the magnitude plot and $\verb|\OrdBode{30}|$ in the phase plot. It allows us to fix the step in y-axis and the default value is 10. At this level, we know how to create and modify the semi-log grid. The next section highlights the Bode plot of basic transfer functions.

## 2. Basic Transfer Functions

To draw a Bode plot in LaTeX, we use the following command:

 \BodeGraph[options]{domain}{function}

• $\verb|options|$: by default we have the following options: samples=50, thick and blue. All TikZ options and gnuplot can be used.
• $\verb|domain|$: the function (magnitude or phase) are drawn in the provided domain. For example, If we would like to draw the function from $10^{-2}$ rad/s to $10^{3}$ rad/s we provide the domain -2:3.
• $\verb|function|$: Here we provide our function (magnitude or phase). Fortunately, there are ready commands for elementary transfer functions

### - First order system

The transfer function of a first order system is given by:

$F(s)=\frac{K}{1+\tau s}$

where $K$ and $\tau$ are the gain and time constant of the system respectively. Here is the commands that can be used for a first order system:

• $\verb|\POAmp{K}{tau}|$: corresponds to the magnitude of a first order system;
• $\verb|\POAmpAsymp{K}{tau}|$: corresponds to the asymptotes  of the magnitude of a first order system;
• $\verb|\POArg{K}{tau}|$: corresponds to the phase of a first order system;
• $\verb|\POArgAsymp{K}{tau}|$: corresponds to the asymptotes  of the phase of a first order system; Example 1: Consider the following transfer function:

$F(s)=\frac{1}{1+0.1 s}$

Here is the Bode Plot in LaTeX:

\documentclass{standalone}
\usepackage{bodegraph}

\begin{document}

\begin{tikzpicture}[
gnuplot def/.append style={prefix={}},
]

% Grid Style
\tikzset{
semilog lines/.style={black},
semilog lines 2/.style={gray},
semilog half lines/.style={gray, dotted},
semilog label x/.style={below,font=\tiny},
semilog label y/.style={above,font=\tiny} }

% Magnitude Plot
\begin{scope}[xscale=7/5, yscale=3/40]
\UnitedB
\semilog*{-2}{3}{-30}{10}
% Asymptotic Line
\BodeGraph[black,samples=500]{-2:2.5}{\POAmpAsymp{1}{0.1}}
% Bode Plot
\BodeGraph[blue]{-2:2.5}{\POAmp{1}{0.1}}
\end{scope}

% Phase Plot
\begin{scope}[yshift=-3.5cm,xscale=7/5,yscale=3/90]
\UniteDegre
\OrdBode{30}
\semilog*{-2}{3}{-90}{0}
% Asymptotic Line
\BodeGraph[black,samples=500]{-2:3}{\POArgAsymp{1}{0.1}}
% Bode Plot
\BodeGraph[red]{-2:3}{\POArg{1}{0.1}}
\end{scope}

\end{tikzpicture}

\end{document}


It should be noted that we added samples=500 to the asymptotic line to get a precise plot.

### - Second order system

The transfer function of a second order system is given by:

$F(s)=\frac{K \omega^2}{s^2+2\xi \omega s+\omega^2}$

Here is the commands that can be used to draw a Bode plot of a second order system:

• $\verb|\SOAmp{K}{xi}{w}|$: corresponds to the magnitude of a second order system;
• $\verb|\SOAmpAsymp{K}{xi}{w}|$: corresponds to the asymptotic line  of the magnitude of a second order system;
• $\verb|\SOArg{K}{xi}{w}|$: corresponds to the phase of a second order system;
• $\verb|\SOArgAsymp{K}{xi}{w}|$: corresponds to the asymptotic line of the phase of a second order system; Example 2: Consider the transfer function of a second order system where $K=2$, $\omega=10$ and for different damping values $\xi=0.1$, $\xi=0.5$ and $\xi=1$

Here is the Bode Plot in LaTeX of a second order system:

\documentclass{standalone}
\usepackage{bodegraph}

\begin{document}

\begin{tikzpicture}[
gnuplot def/.append style={prefix={}},
]

% Grid Style
\tikzset{
semilog lines/.style={black},
semilog lines 2/.style={gray,dotted},
semilog half lines/.style={gray, dotted},
semilog label x/.style={below,font=\tiny},
semilog label y/.style={above,font=\tiny} }

% Magnitude Plot
\begin{scope}[xscale=7/5, yscale=3/50]
\UnitedB
\semilog{-2}{3}{-30}{20}
% xi=0.1
\BodeGraph[red,samples=500]{-2:1.9}{\SOAmp{2}{0.1}{10}}
% xi=0.5
\BodeGraph[green,samples=500]{-2:1.9}{\SOAmp{2}{0.5}{10}}
% xi=1
\BodeGraph[blue,samples=500]{-2:1.9}{\SOAmp{2}{1}{10}}
\end{scope}

% Phase plot
\begin{scope}[yshift=-3cm,xscale=7/5,yscale=3/180]
\UniteDegre
\OrdBode{30}
\semilog{-2}{3}{-180}{0}
% xi=0.1
\BodeGraph[red,samples=500]{-2:3}{\SOArg{2}{0.1}{10}}
% xi=0.5
\BodeGraph[green,samples=500]{-2:3}{\SOArg{2}{0.5}{10}}
% xi=1
\BodeGraph[blue,samples=500]{-2:3}{\SOArg{2}{1}{10}}
\end{scope}

\end{tikzpicture}

\end{document}


### - Bode Plot of an Integrator

The transfer function of an integrator is given by:

$F(s)=\frac{K}{s}$

Here is the commands that can be used to draw the Bode plot of an integrator:

• $\verb|\IntAmp{K}|$: corresponds to the magnitude of an integrator;
• $\verb|\IntArg{K}|$: corresponds to the phase of an integrator; Example 3: Consider the transfer function of an integrator with gain $K=1$. Here is the LaTeX code of Bode Plot of ​an integrator:

\documentclass{standalone}
\usepackage{bodegraph}

\begin{document}

\begin{tikzpicture}[
gnuplot def/.append style={prefix={}},
]

% Grid Style
\tikzset{
semilog lines/.style={black},
semilog lines 2/.style={gray},
semilog half lines/.style={gray, dotted},
semilog label x/.style={below,font=\tiny},
semilog label y/.style={above,font=\tiny} }

% Magnitude Plot
\begin{scope}[xscale=7/4, yscale=3/80]
\UnitedB
\OrdBode{20}
\semilog*{-2}{2}{-40}{40}
% Bode Plot
\BodeGraph[orange]{-2:2}{\IntAmp{1}}
\end{scope}

% Phase Plot
\begin{scope}[yshift=-2.5cm,xscale=7/4,yscale=3/120]
\UniteDegre
\OrdBode{30}
\semilog*{-2}{2}{-120}{0}
% Bode Plot
\BodeGraph[orange]{-2:2}{\IntArg{1}}
\end{scope}

\end{tikzpicture}

\end{document}


### - Bode Plot of A delay

The transfer function of a delay is described by:

$F(s)=e^{-T_r s}$

Here is the commands that can be used to draw the Bode plot of a delay:

• $\verb|\RetAmp{Tr}|$: corresponds to the magnitude of a delay;
• $\verb|\RetArg{Tr}|$: corresponds to the phase of a delay;

These are the basic transfer functions that we will use to draw the Bode plot of any complex function. We invite the reader to check the bodegraph manual for more details.

## 3. Bode Plot of higher order transfer functions

Any complex function can be viewed as a factorization of basic transfer functions mentioned above. For example:

$F(s)=\frac{2s+1}{s^2+s+1}$

It can be viewed as follows:

$F(s)=\frac{1}{s^2+s+1}\left(\frac{1}{2s+1}\right)^{-1}$

which corresponds to a second order system and the inverse of a first order system. To this end, we consider the magnitude of a second order system minus the magnitude of a first order system (logarithmic operation). Here is the LaTeX code:

\documentclass{standalone}

\usepackage{bodegraph}

\begin{document}

\begin{tikzpicture}[
gnuplot def/.append style={prefix={}},
]

% Grid Style
\tikzset{
semilog lines/.style={black},
semilog lines 2/.style={gray},
semilog half lines/.style={gray, dotted},
semilog label x/.style={below,font=\tiny},
semilog label y/.style={above,font=\tiny} }

% Magnitude Plot
\begin{scope}[xscale=7/4, yscale=3/60]
\UnitedB
\semilog*{-2}{2}{-40}{20}
% Bode Plot
\BodeGraph[blue!80]{-2:2}{\SOAmp{1}{0.5}{1} - \POAmp{1}{2}}
\end{scope}

% Phase Plot
\begin{scope}[yshift=-3.5cm,xscale=7/4,yscale=3/105]
\UniteDegre
\OrdBode{15}
\semilog*{-2}{2}{-90}{15}
%Bode Plot
\BodeGraph[blue!80]{-2:2}{ \SOArg{1}{0.5}{1}- \POArg{1}{2}}
\end{scope}

\end{tikzpicture}

\end{document} Do you have any questions, remarks or suggestions, Leave us a comment!

Check more engineering examples!

### This Post Has 4 Comments

1. This is perfect 😍️, Thanks for the write up!
I wonder how one can add text to the Bode Plot?

1. Hey Kev!
2. 1. 