3

I want to create a figure representating a generic periodic function. Here is my attempt.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[>=latex,scale=0.6]
    % Axe horizontal
    \draw[thick, ->] (-4,0) -- (10,0);

    % Fonction (courbe périodique)
    \draw[thick]
    plot [smooth, samples=100, domain=-3:9]
    (\x,{0.5*sin(2*\x r) + sin(3*\x r) + 0.2*sin(4*\x r)});

    % node f
    \node at (8.5,-0.5) {$f$};

    % Ligne verticale
    \draw[thick] (0,-2) -- (0,2);

    % Indication de la période p (approximative)
    \draw[<->] (0.53, 1.607) -- ({2*pi+0.53},1.607) node[midway, above] {$p$};
\end{tikzpicture}
\end{document}

Is there a better and easier way to produce a similar plot that demands less computations? Thank you very much once again!

1
  • 3
    You can use the path operations sin and cos and piece your function together. Though, they only plot a specific segment (a quarter circle). You could adjust the start and end with \pgfpathcurvebetweentime but that's a lot of manual fiddling around. You can always save the diagram as a PDF and import it in your document: standalone, externalize, memoize. Commented 21 hours ago

2 Answers 2

6

I suggest little changes of @Zarko's answer, for a better (I think) visualisation of a periodic function in a book:

enter image description here

Code:

\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{arrows.meta,
    quotes}

\begin{document}
    \begin{tikzpicture}[scale=0.5, 
        >=Stealth,%Latex,
        trig format=rad, % or deg   <------
        samples=201         ]
        % Axes
        \draw[->] (-1.1*pi,0) -- ++ (5.3*pi,0) node[below left] {$f$};
        \draw[->] (0,-2)      -- ++ (0,5)    node[below left] {$A$};
        % Fonction (courbe périodique)
        \draw[thick]    plot [domain=-pi:4*pi]
        (\x,{0.5*sin(2*\x) + sin(3*\x) + 0.2*sin(4*\x)});
        \draw[thick,red]    plot [domain=0:2*pi]
        (\x,{0.5*sin(2*\x) + sin(3*\x) + 0.2*sin(4*\x)});
        % Indication de la période p 
        \draw[|<->|] (0, 2) to ["$p=2\pi$"]   ++ (2*pi,0);
        \draw[|<->|] (2*pi, 2) to ["$p=2\pi$"]   ++ (2*pi,0);
        
        \begin{scope}[yshift=5.5cm]
            \draw[->] (-1.1*pi,0) -- ++ (5.3*pi,0) node[below left] {$f$};
            \draw[->] (0,-2)      -- ++ (0,5)    node[below left] {$A$};
            % Fonction (courbe périodique)
            \draw[thick]    plot [domain=-pi:4*pi]
            (\x,{0.5*sin(2*\x) + sin(3*\x) + 0.2*sin(4*\x)});   
        \end{scope}
    \end{tikzpicture}
\end{document}
5

Your code is quit fine, so maybe some minor changes:

  • you not need to load tikz twice
  • I would use tikz libraries arrows.meta and quotes for a bit nicer arrows heads and simpler draw labels on edge
  • using option trig format=rad, make writing equation more simpler
  • I wouldn't use smooth option, rather increase number of samples
\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{arrows.meta,
                quotes}

\begin{document}
    \begin{tikzpicture}[scale=0.5, 
    >=Stealth,%Latex,
    trig format=rad, % or deg   <------
    samples=201         ]
% Axes
\draw[->] (-1.1*pi,0) -- ++ (4.3*pi,0) node[below left] {$f$};
\draw[->] (0,-2)      -- ++ (0,4.4)    node[below left] {$A$};
% Fonction (courbe périodique)
\draw[thick]    plot [domain=-pi:3*pi]
    (\x,{0.5*sin(2*\x) + sin(3*\x) + 0.2*sin(4*\x)});
% Indication de la période p (approximative)
\draw[|<->|] (0.53, 2) to ["$p$"]   ++ (2*pi,0);
    \end{tikzpicture}
\end{document}

enter image description here

Addendum:
First version of answer just show what OP can do better with result as (s)he show in question. However, idra presented in @Raffaele Santoro answer is interesting. Based on it, I would draw your image on the following way:

\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{arrows.meta,
                quotes}

\begin{document}
    \begin{tikzpicture}[scale=0.5, 
    >=Stealth,%Latex,
    trig format=rad, % or deg   <------
    samples=201         ]
% Axes
\draw[->] (-1.1*pi,0) -- ++ (4.3*pi,0) node[below left] {$f$};
\draw[->] (0,-2)      -- ++ (0,4.4)    node[below left] {$A$};
% Fonction (courbe périodique)
\draw[thick,densely dotted]    plot [domain=-pi:3*pi]
    (\x,{0.5*sin(2*\x) + sin(3*\x) + 0.2*sin(4*\x)});
\draw[thick]    plot [domain=0:2*pi]
    (\x,{0.5*sin(2*\x) + sin(3*\x) + 0.2*sin(4*\x)});
% Indication de la période p (approximative)
\draw[<->|, very thin] (0, 1.9) to ["$p$"]   ++ (2*pi,0);
    \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.