8

I need to plot the following function using Tikz: enter image description here

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,
        ]
        \addplot[
        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:

enter image description here

3 Answers 3

14

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]
        \addplot[
        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}

enter image description here

10

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,
        ]
        \addplot[
        domain = 0:30,
        samples = 100,
        smooth,
        thick,
        black,
        ] {\a*exp(-x/15)*( cos(deg(x)) + sin(deg(x))/40 )};
        \addplot [domain = 0:30,
        thick,
        dashed,
        blue,
        ]{\a};
        \addplot [domain = 0:30,
        thick,
        dashed,
        blue,
        ]{-0.8106};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

2
  • 3
    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
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .