# How can I draw horizontal lines on a plot?

I need to plot the following function using Tikz:

but I don't know how to add the max and min lines at the function's bounds. This is the code I've written so far:

    \begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 30,
ymin = -1.5, ymax = 2.0,
]
domain = 0:30,
samples = 200,
smooth,
thick,
black,
] {exp(-x/10)*( cos(deg(x)) + sin(deg(x))/40 )};
\end{axis}
\end{tikzpicture}


and this is the output plot:

You only need a local bounding box.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin = 0, xmax = 30,
ymin = -1.5, ymax = 2.0,
]
\begin{scope}[local bounding box=plot]
domain = 0:30,
samples = 200,
smooth,
thick,
black,
] {exp(-x/10)*( cos(deg(x)) + sin(deg(x))/40 )};
\end{scope}
\end{axis}
\draw[dashed] (plot.north east) -- (plot.north west) node[left=2ex] {max}
(plot.south east) -- (plot.south west) node[left=2ex] {min};
\end{tikzpicture}
\end{document}


You have to find the min value of your function. For this, I have used the MATLAB and find the min y value as -0.8106.

\documentclass[margin=3mm]{standalone}
%
\usepackage{tikz} % TikZ and PGF
\usepackage{pgfplots}
\usepackage{calc}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
\def\a{1}
\begin{axis}[
xmin = 0, xmax = 30,
ymin = -1.5, ymax = 2.0,
]
domain = 0:30,
samples = 100,
smooth,
thick,
black,
] {\a*exp(-x/15)*( cos(deg(x)) + sin(deg(x))/40 )};
thick,
dashed,
blue,
]{\a};
thick,
dashed,
blue,
]{-0.8106};
\end{axis}
\end{tikzpicture}
\end{document}


• The options smooth and samples=100 are probaly not of much use for a horizontal line. Mar 12, 2020 at 20:28
• @PeterGrill, absolutely right. I have made copy and paste.
– user31034
Mar 12, 2020 at 20:32

You should achieve straight line by simply using \draw [dashed] (x1,y1) -- (x2,y2) and insert the coordinates you want.

I advise you see the Tikz manual: http://ftp.fau.de/ctan/graphics/pgf/base/doc/pgfmanual.pdf