6

I am trying to plot f(x)=abs(x*(x-1)^(1/3)) on [0,2]:

abs(x*(x-1)^(1/3))

I have to use tikzpicture environment but I am not able to do it in a nice way.

MWE:

\documentclass{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{center}
\begin{tikzpicture}[declare function={f(\x)=ifthenelse(\x>=0&&\x<=1,abs(\x*(\x-1)^(1/3)),\x*(\x-1)^(1/3));}]
    \begin{axis}[
            axis on top,
            legend pos=outer north east,
            axis lines = center,
            xticklabel style = {font=\tiny},
            yticklabel style = {font=\tiny},
            xlabel = $x$,
            ylabel = $y$,
            legend style={cells={align=left}},
            legend cell align={left},
        ]
        \addplot[very thick,red,samples=81,domain=0:2,name path=f] {f(x)};
    \end{axis}
\end{tikzpicture}
\end{center}

What I get

The same effect is produced if I try with \addplot[very thick,red,samples=81,domain=0:2,name path=f] {abs(x*(x-1)^(1/3))};:

\begin{center}
\begin{tikzpicture}
    \begin{axis}[
            axis on top,
            legend pos=outer north east,
            axis lines = center,
            xticklabel style = {font=\tiny},
            yticklabel style = {font=\tiny},
            xlabel = $x$,
            ylabel = $y$,
            legend style={cells={align=left}},
            legend cell align={left},
        ]
        \addplot[very thick,red,samples=81,domain=0:2,name path=f] {abs(x*(x-1)^(1/3))};
    \end{axis}
\end{tikzpicture}
\end{center}

If I do not use declare function the result is the same

Thanks and have a nice 2019!

  • 5
    Your document is incomplete – user31729 Jan 1 at 0:02
11

You set the brackets in such a way that the compiler sees (x-1)^(1/3). It does not know how to plot the third root of a negative number, and let's it be. I fixed the brackets to obtain

\documentclass{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{fillbetween} 
\begin{document}
\begin{center}
\begin{tikzpicture}[declare function={f(\x)=ifthenelse(\x>=0&&\x<=1,\x*abs(\x-1)^(1/3),\x*(\x-1)^(1/3));}]
    \begin{axis}[
            axis on top,
            legend pos=outer north east,
            axis lines = center,
            xticklabel style = {font=\tiny},
            yticklabel style = {font=\tiny},
            xlabel = $x$,
            ylabel = $y$,
            legend style={cells={align=left}},
            legend cell align={left},
        ]
        \addplot[very thick,red,samples=161,domain=0:2,name path=f] {f(x)};
    \end{axis}
\end{tikzpicture}
\end{center}

\begin{center}
\begin{tikzpicture}
    \begin{axis}[
            axis on top,
            legend pos=outer north east,
            axis lines = center,
            xticklabel style = {font=\tiny},
            yticklabel style = {font=\tiny},
            xlabel = $x$,
            ylabel = $y$,
            legend style={cells={align=left}},
            legend cell align={left},
        ]
        \addplot[very thick,red,samples=161,domain=0:2,name path=f] {abs(x)*abs(x-1)^(1/3)};
    \end{axis}
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

Happy New Year!

  • 1
    Good to know! This time I did not forget the odd samples haha, Happy New Year!! – manooooh Jan 1 at 0:37
  • 1
    @manooooh Oh, this was a real question (and not just a trick to have the first question in 2019)? ;-) – user121799 Jan 1 at 0:42
  • 1
    It is? You know I thought about it while taking a shower but for the time zones I said "There is no way it is the first question of 2019" :D. If so, you are the first best answer of 2019 ;). – manooooh Jan 1 at 0:44

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.