7

I want to create a graph like the one shown in the figure (it is done with Geogebra), where on the x-axis there are labels in radians (for example $\pi/6, \pi/4, \pi/2$ etc.) with its relative unit of measurement and on the y-axis values from $0$ to $+\infty$.

enter image description here

In the meantime I have a difficulty to put the labels relative to the respective function maximums (in radians) where between $0$ and $\pi/2$ there are (5) five maximus of the green function $f(x)=|\sin(10x)|$. The violet function is $g(x)=10|\cos(10x)|$ and between $0$ and $\pi/2$ there are six (6) maximus.

Into my MWE there are some differences:

  1. I am not able to insert labels into radians (and for every maximum).
  2. I can't get the same graph as with Geogebra.
  3. I am not able to draw for the violet function for any point (for example A) its tangent to the graph to show that the slope is steeper than the green function.

Here my image with the MWE. enter image description here

\documentclass{article}
\usepackage{tikz,amsmath}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
            xmin=0,xmax=8*pi,
            xlabel={$x$},
            ymin=0,ymax=10,
            axis on top,
            legend style={legend cell align=right,legend plot pos=right}]
\addplot[color=red,domain=0:7*pi,samples=101] {abs(sin(10*x))};
\addplot[color=green,domain=0:7*pi,samples=101] {10*abs(cos(10*x))};
\addlegendentry{$f(x)=|\sin(10x)|$}
\addlegendentry{$g(x)=10|\cos(10x)|$}
\end{axis}
\end{tikzpicture}
\end{document}

I kindly ask for your valuable help and any improvement of my code is welcome. Thank you very much.

  • @marmot Done! I always listen to the best when they're right. :-) – Sebastiano Sep 5 '18 at 9:59
  • 1
    Mille grazie!!! – user121799 Sep 5 '18 at 14:10
  • 2
    An off-topic suggestion: I noticed that you used the simple | as your absolute value delimiter. This creates wrong spacing when used together with \sin or \cos. Instead, use \lvert\sin(10x)\rvert and observe the correct spacing between the opening | and the sin. – Ruixi Zhang Sep 7 '18 at 0:59
  • @RuixiZhang Next time I'll try to change the code. Now I'm connected to a lucky location and I hope to remember to make the change. Thank you and always good work. You're very talented. – Sebastiano Sep 8 '18 at 19:45
8
  1. You can use deg to convert to radians.
  2. Are you asking about the coloring or the background grid?
  3. For the third part I use (a slightly modified version of) Jake's great answer.
  4. In order to add symbolic x ticks, I use a combination of this answer by Peter Grill and that answer by Faekynn.

As for the question why the output of my previous answer did not resemble the GeoCobra result: I copied your question before the edit and did not realize that you added the factor of 10. I fixed that now. Note also that, if you plot 7 periods, it is advantageous to set the number of samples to 7*integer+1, which is why I use samples=99 instead of 101.

\documentclass{article}
\usepackage{tikz,amsmath}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usepackage{fp} % for frac
\makeatletter % from https://tex.stackexchange.com/a/198046/121799
\def\parsenode[#1]#2\pgf@nil{%
    \tikzset{label node/.style={#1}}
    \def\nodetext{#2}
}

\tikzset{
    add node at x/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [name path global = position line #1-1]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=1pt, name path global = position line #1-2]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            \endgroup
        }
    }
}
\makeatother
\begin{document}
% based on https://tex.stackexchange.com/a/34958/121799
\foreach \X [count=\Y] in {0,...,7}
{\pgfmathsetmacro{\myx}{\X*pi/10}
\ifnum\Y=1
\xdef\LstX{\myx}
\else
\xdef\LstX{\LstX,\myx}
\fi
}
\begin{tikzpicture}
\begin{axis}[width=12cm,height=7cm,
        tangent/.style args={at pos #1 with length #2}{
            add node at x={#1}{
                [
                    sloped, 
                    append after command={(\tikzlastnode.west) edge [thick, red!75!black] (\tikzlastnode.east)},
                    minimum width=#2
                ]
            }      
      }, 
      xtick=\LstX,
      xticklabel={\pgfmathsetmacro{\tmp}{round(10*\tick/pi)}
      \pgfmathsetmacro{\mygcd}{gcd(\tmp,10)}%
      \pgfmathtruncatemacro{\mynumerator}{\tmp/\mygcd}%
      \pgfmathtruncatemacro{\mydenominator}{10/\mygcd}%
      \ifnum\mynumerator=0
       $\pgfmathprintnumber{0}$
      \else
       \ifnum\mynumerator=1
        $\frac{\pi}{\pgfmathprintnumber{\mydenominator}}$
       \else
        $\frac{\mynumerator\pi}{\pgfmathprintnumber{\mydenominator}}$
       \fi
      \fi}, % https://tex.stackexchange.com/a/304032/121799
            xmin=0,xmax=0.8*pi,
            xlabel={$x$},
            ymin=0,ymax=10,
            axis on top,
            legend style={legend cell align=right,legend plot pos=right}]
%           
\addplot[color=red,domain=0:0.7*pi,samples=99] {abs(sin(10*deg(x)))};
\addplot[color=green!60!black,domain=0:0.7*pi,samples=99,
tangent={at pos 0.1 with length 5cm}] {10*abs(cos(10*deg(x)))};
\addlegendentry{$f(x)=|\sin(10x)|$}
\addlegendentry{$g(x)=10|\cos(10x)|$}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |

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.