1

The image shows that the graph of [sin(x)]^100 (in red) lies below the x-axis in four small intervals, as the function is non-negative its graph should be on or above the x-axis. So there is an error when plotting the graph of [sin(x)]^100, but I have no idea how to correct it.

I would appreciate anyone willing to point me to a solution or paths to it.

enter image description here

The image was generated with the code

\documentclass[a4paper]{scrbook}
\usepackage{xcolor}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[xscale=1.5,axis x line=bottom,axis y line=left, 
        axis x line=middle,axis y line=middle,xmax=6.7,ymax=1.3,
        every inner x axis line/.append style={- 
             stealth},xlabel=$x$,ylabel=$f_n(x)$,]
        \addplot+ [domain=0:2*pi,smooth,mark=none,blue] {sin(deg(x))};
        \addplot+ [domain=0:2*pi,smooth,mark=none,red] {sin(deg(x))^100};
    \end{axis}
\end{tikzpicture}

\end{document}

2 Answers 2

3

Source of your problem is use smooth (spline function) on too small number of sampled. So, if you:

  • increase number od samples, for example to 401,
  • remove smooth instruction,
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
    axis lines = middle,
    xmax=6.7,   ymax=1.3,
    xlabel=$x$, ylabel=$f_n(x)$,
%
clip=false,
legend style = {at ={(0.8,0.8)}, anchor=west,
                font=\scriptsize, 
                legend cell align=left,
                },  
%
trig format=rad,
domain=0:2*pi,  samples=401,
no marks,
                ]
\addplot    {sin(x)};
\addplot    {sin(x)^100};

\legend{$\sin(x)$, $(\sin(x))^{100}$}
\end{axis}
    \end{tikzpicture}
\end{document}

you will get desired result:

enter image description here

Off-topic:

  • clear-up axis preamble (too many axis instructions where the last ones overwrite the first ones)
  • move common option from addplot to axis preamble

Edit (1): To diagram is added legend.

Edit (2): A version of your diagram where x ticks are given in radians and function curves are thicker (for exercise):

\documentclass[border=3.141592]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
    x = 12mm,
    axis lines = middle,
    xmax=7,   ymax=1.4,
    xlabel=$x$ (rad), ylabel=$f_n(x)$,
% legend
clip=false,
legend style = {at ={(0.8,0.8)}, anchor=west,
                font=\scriptsize,
                legend cell align=left,
                },
% new
trig format=rad,
      xtick = {pi/4,    pi/2,     3*pi/4,   pi,
               5*pi/4, 3*pi/2,    7*pi/4,  2*pi},
xticklabels = {$\pi/4$, $\pi/2$, $3\pi/4$, $\pi$,
               $5\pi/4$, $3\pi/2$, $7\pi/4$, $2\pi$},
  tick label style = {font=\footnotesize},
x tick label style = {yshift=-2ex, anchor=center},
extra y ticks = {0},
%
domain=0:2*pi,  samples=401,
no marks,
every axis plot post/.append style={thick},
                ]
\addplot    {sin(x)};
\addplot    {sin(x)^100};

\legend{$\sin(x)$, $(\sin(x))^{100}$}
\end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

1
  • Off-topic: for large n, (sin x)^n is very very close to exp(n (x-pi/2)^2/2). This can help for easier plotxand computation.
    – Jhor
    Aug 1 at 8:01
1

Try adding samples=100 to your code for sin(x)^100, it should fix the problem. This is the new code:

begin{tikzpicture}
    \begin{axis}[xscale=1.5,axis x line=bottom,axis y line=left, 
        axis x line=middle,axis y line=middle,xmax=6.7,ymax=1.3,
        every inner x axis line/.append style={- 
             stealth},xlabel=$x$,ylabel=$f_n(x)$,]
        \addplot+ [domain=0:2*pi,smooth,mark=none,blue] {sin(deg(x))};
        \addplot+ [samples=100,domain=0:2*pi,smooth,mark=none,red] {sin(deg(x))^100};
    \end{axis}
\end{tikzpicture}
2

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.