3

I'm doing a school project and I need to create a graph like this one. I created the graph but dont know how the add the vertical dashed red lines...

enter image description here

    \begin{tikzpicture}[>=latex]
    \begin{axis}[
            axis lines = middle,
            xlabel = $t$,
            ylabel = {$x(t)$},
            xtick={-50,-40,...,50},
            ytick={-2,-1,...,2},
            xmin=-50.5,
            xmax=50.5,
            ymin=-2.5,
            ymax=2.5,
            width=155mm,
            height=70mm,
        ]
        \addplot[
            scatter,
            domain=-50:50,
            samples=120,
            color=blue,
        ]
        {sin(180*x/12)+cos(180*x/8)};
        \addplot[
            only marks,
            domain=-50:50,
            samples=120,
            color=red
        ]
        {sin(180*x/12)+cos(180*x/8)};
    \end{axis}
\end{tikzpicture}

This is the code that I have created and the graph.

enter image description here

3

I solved the question using ycomb, dashed in the options

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[
        axis lines = middle,
        xlabel = $t$,
        ylabel = {$x(t)$},
        xtick={-50,-40,...,50},
        ytick={-2,-1,...,2},
        xmin=-50.5,
        xmax=50.5,
        ymin=-2.5,
        ymax=2.5,
        width=155mm,
        height=70mm,
    ]
    \addplot[
        scatter,
        domain=-50:50,
        samples=120,
        color=blue,
    ]
    {sin(180*x/12)+cos(180*x/8)};
    \addplot[
        only marks,
        domain=-50:50,
        samples=120,
        color=red
    ]{sin(180*x/12)+cos(180*x/8)};
    \addplot[
        ycomb,
        dashed,
        domain=-50:50,
        samples=120,
        color=red
    ]
    {sin(180*x/12)+cos(180*x/8)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

2
  • 2
    I took a liberty for formatting and complete your code fragment and add image, which it produce. – Zarko May 7 '20 at 22:27
  • Thanks for that appreciate it – user214980 May 8 '20 at 18:58
0

An optimized version of the above:

  • you can avoid the repetition of domain=-50:50,samples=120, for every \addplot by adding them to the options of the axis environment;
  • the scatter plot is not required at all: it produces another type of plot, that colors the marks according to the value of a given variable. Therefore you need only two \addplot commands (one for the blue line, and the other for the comb);
  • you can plot the marks with the ycomb plot;
  • finally, you can write the expression of the function only once using the declare function option.

This results in the following MWE:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}[>=latex]
    \begin{axis}[
            axis lines       = middle,
            xlabel           = $t$,
            ylabel           = {$x(t)$},
            xtick            = {-50,-40,...,50},
            ytick            = {-2,-1,...,2},
            xmin             = -50.5,
            xmax             = 50.5,
            ymin             = -2.5,
            ymax             = 2.5,
            width            = 155mm,
            height           = 70mm,
            domain           = -50:50,
            samples          = 120,
            declare function = {%
                F1          = 1/12;
                F2          = 1/8;
                lincomb(\t) = sin(180*F1*\t)+cos(180*F2*\t);
            },
        ]

        \addplot [color=blue] {lincomb(x)};
        \addplot[
            ycomb,
            mark         = *,
            mark options = {scale = 0.75,fill = red},
            color        = red,
            dashed,
        ] {lincomb(x)};
    \end{axis}
\end{tikzpicture}
\end{document}

Your Answer

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