2

I slightly modified the code written for a second order step response found here. I wanted to add extra text around the figure. I have to through many iterations before I get the placement right. I was thinking if I temporarily placed some gridlines overlayed on the figure, I would get a better idea as to where to place my extra text. This is the output that I was shooting for:

enter image description here

Can you tell me the best approach to place the texts around the figure? Thanks!

CODE

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{amssymb,amsthm}
\usepackage{mathtools}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            width=9cm,
            height=6cm,
            axis lines=middle,
            xmin=0, xmax=15,
            ymin=0, ymax=1.5,
            xlabel=$t$,
            ylabel={$y(t)$},
            xlabel style={at=(current axis.right of origin), anchor=west},
            ylabel style={at=(current axis.above origin), anchor=south},
            xtick={0, 0.4726, 1.79398, 1.96605, 3.2236, 11.0855},
            xticklabels={$0$, $$, $$, $$, $T_p$, $T_s$},
            every x tick/.style={black},
            ytick={0, 0.1, 0.9, 1, 1.3714},
            yticklabels={$0$, $0.1$, $0.9$, $1$, $M_{p_t}$},
            every y tick/.style={black}
        ]
        \addplot[black, densely dotted] coordinates{(0.4726,0.1)} -- (axis cs:0,0.1);
        \addplot[black, densely dotted] coordinates{(0.4726,0.1)} -- (axis cs:0.4726,0);
        %
        \addplot[black, densely dotted] coordinates{(1.79398,0.9)} -- (axis cs:0,0.9);
        \addplot[black, densely dotted] coordinates{(1.79398,0.9)} -- (axis cs:1.79398,0);
        %
        \addplot[black, densely dotted] coordinates{(1.96605,1)} -- (axis cs:1.96605,0);
        %
        \addplot[black, densely dotted] coordinates{(3.2236,1.3714)} -- (axis cs:0,1.3714);
        \addplot[black, densely dotted] coordinates{(3.2236,1.3714)} -- (axis cs:3.2236,0);
        %
        \addplot[black, densely dotted] coordinates{(11.0855,1.025)} -- (axis cs:11.0855,0);

        \addplot[black, dashed] coordinates{(15,1)} -- (axis cs:0,1);
        %
        \addplot[cyan, dashed] coordinates{(15,0.975)} -- (axis cs:0,0.975);
        \addplot[cyan, dashed] coordinates{(15,1.025)} -- (axis cs:0,1.025);
        %
        \addplot[smooth,
                 black,
                 thick,
                 mark=none,
                 domain=0:12.4,
                 samples=100]
        {1-exp(-0.3*x)*(cos(deg(sqrt(1-0.3^2)*x))+0.3/(sqrt(1-0.3^2))*sin(deg(sqrt(1-0.3^2)*x)))};
        %
        \addplot[black, thick] coordinates{(15,0.9872)} -- (axis cs:12.4,0.9872);
        %
        \coordinate (trleft) at (axis cs:0,0);
        \coordinate (trright) at (axis cs:1.96605,0);
        %
        \coordinate (tr1left) at (axis cs:0.4726,0);
        \coordinate (tr1right) at (axis cs:1.79398,0);
        %
        \coordinate (ess1) at (axis cs:14,1.1);
        \coordinate (ess2) at (axis cs:14,1);
        \coordinate (ess3) at (axis cs:14,0.9872);
        \coordinate (ess4) at (axis cs:14,0.8872);
    \end{axis}

    \draw [densely dotted] (tr1left) -- ++(0,-0.5cm) coordinate (a1);
    \draw [densely dotted](tr1right) -- ++(0,-0.5cm) coordinate (a2);
    \draw [<->] ([yshift=2pt]a1) -- ([yshift=2pt]a2) node [midway,fill=white] {${\scriptstyle \hat{t}_r}$};

    \draw [densely dotted] (trleft) -- ++(0,-1cm) coordinate (b1);
    \draw [densely dotted](trright) -- ++(0,-1cm) coordinate (b2);
    \draw [<->] ([yshift=2pt]b1) -- ([yshift=2pt]b2) node [midway,fill=white] {$T_r$};

    \draw [->] (ess1) node [right] {$\bar{\epsilon}$} -- (ess2);
    \draw [<-] (ess3) -- (ess4);

    \draw [decorate,decoration={brace,mirror, amplitude=5pt},xshift=0pt,yshift=0pt]
    (1.62,3) -- (1.62,4) node [blue!70,pos=0.85,xshift=1.6cm]
    {\footnotesize Max Overshoot};

    \draw [thick,red,{latex-}] (1.85,3.5) to[out=180,out=30] (2.2,3.95);

    \node at (3.5,2.0) {\tiny{\fbox{poles if complex $\rightarrowtail$ no zeros}}};

     \node at (1,-1.5) {\tiny{$\omega_{n}\uparrow\Rightarrow \text{faster response}$}};

\end{tikzpicture}
\end{document}
  • 1
    You probably pressed the wrong button when inserting the code, I corrected that, hoping it is OK with you. If you are using compat=1.11 or newer, you do not need the axis cs: in the axis environment. Are you asking abut the annotations in the axis or below of it? – user121799 Oct 25 '18 at 2:21
3

I focus here on some of the missing annotations. You can place them rather conveniently if you load the positioning library and/or place them relatively to the current axis shape. All my examples are at the very end of the code.

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.16}
\usepackage{amssymb,amsthm}
\usepackage{mathtools}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            width=9cm,
            height=6cm,
            axis lines=middle,
            xmin=0, xmax=15,
            ymin=0, ymax=1.5,
            xlabel=$t$,
            ylabel={$y(t)$},
            xlabel style={at=(current axis.right of origin), anchor=west},
            ylabel style={at=(current axis.above origin), anchor=south},
            xtick={0, 0.4726, 1.79398, 1.96605, 3.2236, 11.0855},
            xticklabels={$0$, $$, $$, $$, $T_p$, $T_s$},
            every x tick/.style={black},
            ytick={0, 0.1, 0.9, 1, 1.3714},
            yticklabels={$0$, $0.1$, $0.9$, $1$, $M_{p_t}$},
            every y tick/.style={black}
        ]
        \addplot[black, densely dotted] coordinates{(0.4726,0.1)} -- (axis cs:0,0.1);
        \addplot[black, densely dotted] coordinates{(0.4726,0.1)} -- (axis cs:0.4726,0);
        %
        \addplot[black, densely dotted] coordinates{(1.79398,0.9)} -- (axis cs:0,0.9);
        \addplot[black, densely dotted] coordinates{(1.79398,0.9)} -- (axis cs:1.79398,0);
        %
        \addplot[black, densely dotted] coordinates{(1.96605,1)} -- (axis cs:1.96605,0);
        %
        \addplot[black, densely dotted] coordinates{(3.2236,1.3714)} -- (axis cs:0,1.3714);
        \addplot[black, densely dotted] coordinates{(3.2236,1.3714)} -- (axis cs:3.2236,0);
        %
        \addplot[black, densely dotted] coordinates{(11.0855,1.025)} -- (axis cs:11.0855,0);

        \addplot[black, dashed] coordinates{(15,1)} -- (axis cs:0,1);
        %
        \addplot[cyan, dashed] coordinates{(15,0.975)} -- (axis cs:0,0.975);
        \addplot[cyan, dashed] coordinates{(15,1.025)} -- (axis cs:0,1.025);
        %
        \addplot[smooth,
                 black,
                 thick,
                 mark=none,
                 domain=0:12.4,
                 samples=100]
        {1-exp(-0.3*x)*(cos(deg(sqrt(1-0.3^2)*x))+0.3/(sqrt(1-0.3^2))*sin(deg(sqrt(1-0.3^2)*x)))};
        %
        \addplot[black, thick] coordinates{(15,0.9872)} -- (axis cs:12.4,0.9872);
        %
        \coordinate (trleft) at (axis cs:0,0);
        \coordinate (trright) at (axis cs:1.96605,0);
        %
        \coordinate (tr1left) at (axis cs:0.4726,0);
        \coordinate (tr1right) at (axis cs:1.79398,0);
        %
        \coordinate (ess1) at (axis cs:14,1.1);
        \coordinate (ess2) at (axis cs:14,1);
        \coordinate (ess3) at (axis cs:14,0.9872);
        \coordinate (ess4) at (axis cs:14,0.8872);
    \end{axis}

    \draw [densely dotted] (tr1left) -- ++(0,-0.5cm) coordinate (a1);
    \draw [densely dotted](tr1right) -- ++(0,-0.5cm) coordinate (a2);
    \draw [<->] ([yshift=2pt]a1) -- ([yshift=2pt]a2) node [midway,fill=white] {${\scriptstyle \hat{t}_r}$};

    \draw [densely dotted] (trleft) -- ++(0,-1cm) coordinate (b1);
    \draw [densely dotted](trright) -- ++(0,-1cm) coordinate (b2);
    \draw [<->] ([yshift=2pt]b1) -- ([yshift=2pt]b2) node [midway,fill=white] {$T_r$};

    \draw [->] (ess1) node [right] {$\bar{\epsilon}$} -- (ess2);
    \draw [<-] (ess3) -- (ess4);

    \draw [decorate,decoration={brace,mirror, amplitude=5pt},xshift=0pt,yshift=0pt]
    (1.62,3) -- (1.62,4) node [blue!70,pos=0.85,xshift=1.6cm]
    {\footnotesize Max Overshoot};

    \draw [thick,red,{latex-}] (1.85,3.5) to[out=180,out=30] (2.2,3.95);

%    \node at (3.5,2.0) {\tiny{\fbox{poles if complex $\rightarrowtail$ no zeros}}};

%    \node at (1,-1.5) {\tiny{$\omega_{n}\uparrow\Rightarrow \text{faster response}$}};
% text below axis
    \node[below=1cm of current axis.south,align=center](omegad)
    {$\omega_d=\omega_n\sqrt{1-\zeta^2}$\\
    damped frequancy of oscillation};
    \node[below=2cm of current axis.south west,align=left] (omegan)
    {$\omega_{n}\uparrow\Rightarrow \text{faster response}$\\
    $\zeta_{n}\downarrow\Rightarrow \text{larger overshoot}$};
    \node[draw] (s12) at  ([yshift=1mm]current axis.north)
     {$s_{1,2}=-\zeta\omega_n\pm\omega_n\sqrt{\zeta^2-1}$};
    \node[draw] (poles) at  ([yshift=-2mm]current axis.center)
     {poles if complex $\rightarrowtail$ no zeros};
\end{tikzpicture}
\end{document}

enter image description here

I personally would make the figure slightly larger.

  • thanks for your response! If I, however, wanted to slightly tweak the position of the \omega_{n} and \zeta_{n} text to the right, how do I do that? Sincerely appreciated! – Joe Oct 25 '18 at 4:27
  • 2
    @Joe Just replace \node[below=2cm of current axis.south west,align=left] by \node[below=2cm of current axis.south west,align=left,xshift=4mm], i.e. add an appropriate xshift. – user121799 Oct 25 '18 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.