3

The power functions that i'm trying to plot isn't showing up fully, i've tried changing the domains and stuff but to no avail, can i get some help here please?

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
     axis lines = center,
    xlabel = $x$,
    ylabel = {$y$},
    xmax = {4},
    xmin = {-1},
    ymax = {2},
    ymin = {-2},
    legend pos = outer north east
]
\addplot [
    domain=-10:10, 
    samples=100, 
    color=black,
]
{x^(1/2)};
\addlegendentry{$y=\sqrt{x}$}
\addplot [
    domain=-5:5, 
    samples=100, 
    color=blue,
]
{x^(1/4)};
\addlegendentry{$y=x^{\frac{1}{4}}$}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
    axis lines = center,
    xlabel = $x$,
    ylabel = {$y$},
    xmax = {4},
    xmin = {-4},
    ymax = {2},
    ymin = {-2},
    legend pos = north west
]
\addplot [
    domain=-10:10, 
    samples=100, 
    color=black,
]
{x^(1/3)};
\addlegendentry{$y=x^{\frac{1}{3}}$}
\addplot [
    domain=-10:10, 
    samples=100, 
    color=blue,
]
{x^(1/5)};
\addlegendentry{$y=x^{\frac{1}{5}}$}
\end{axis}
\end{tikzpicture}
\end{document}
cmhughes
  • 100,947
  • 1
    Please make it compilable by adding document class and packages required. Welcome. – Jesse Feb 08 '16 at 04:20
  • 1
    Welcome to TeX.SE. As Jesse said, It is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. I would recommend you use (x) instead of just x. Otherwise I get two graphs output. Also, please clarify what you mean by "showing up fully". – Peter Grill Feb 08 '16 at 04:25
  • My apologies, i've edited it accordingly. By not showing up fully, i mean that the graphs of y=x^(1/3) and y=x^(1/5) are not showing up at the 3rd quadrant as it is supposed to. – Tay Yong Qiang Feb 08 '16 at 04:32
  • Try using (x) instead of x and PGF Math Function to compute cube root may be of help. – Peter Grill Feb 08 '16 at 05:12
  • changing to (x) doesn't help, what about the fifth root though? The cubic root function won't work on it. – Tay Yong Qiang Feb 08 '16 at 05:28
  • The same logic can be employed to generate the 5th root. – Peter Grill Feb 08 '16 at 07:31
  • seems like a duplicate of either http://tex.stackexchange.com/questions/278331/addplot-command-fails-to-generate-graph-for-function-with-rational-exponents/278333#278333, http://tex.stackexchange.com/questions/144454/how-to-plot-x1-3, http://tex.stackexchange.com/questions/69411/pgfplots-cant-plot-some-usual-mathematical-functions, http://tex.stackexchange.com/questions/19052/pgf-math-function-to-compute-cube-root, – cmhughes Feb 08 '16 at 08:56
  • @cmhughes: I suggested one of those to the OP, but it was not clear to the OP how to adapt a 5th root (as per the comments). – Peter Grill Feb 08 '16 at 09:19
  • @PeterGrill fair enough. I'm still surprised that changing a '3' to a '5' needs a new question :) Happy to vote for you, nonetheless! – cmhughes Feb 08 '16 at 14:57
  • 1
    @cmhughes: Yes it is that simple, but that is assuming one can read the code. I can actually relate to the OPs difficulties as it often takes me several guesses as to where to make slight changes to some of the expl3 solutions I am using. Similarly, when it comes to expansion issues. Also, there was a bizarre issue of the 2nd plot needing a \hspace*{-23.5cm} to be able to see it -- see the edit history. Jake rescued me on that one so no longer an issue. – Peter Grill Feb 08 '16 at 22:01

1 Answers1

3

You can adapt the solution from PGF Math Function to compute cube root to generate a function that computes the third and fifth root:

enter image description here

References:

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\pgfmathdeclarefunction{CubeRoot}{1}{% \pgfmathparse{ifthenelse(#1<0,-1,1)*exp((ln(abs(#1)))/3)}% }

\pgfmathdeclarefunction{FifthRoot}{1}{% \pgfmathparse{ifthenelse(#1<0,-1,1)*exp((ln(abs(#1)))/5)}% }

\begin{document}\noindent \begin{tikzpicture} \begin{axis}[ axis lines = center, xlabel = $x$, ylabel = {$y$}, xmax = {4}, xmin = {-1}, ymax = {2}, ymin = {-2}, legend pos = south east ] \addplot [ domain=0:4, samples=100, color=orange, line width=1.0pt, ] {(x)^(1/2)}; \addlegendentry{$y=\sqrt{x}$} \addplot [ domain=0:5, samples=100, color=blue, line width=1.0pt, ] {(x)^(1/4)}; \addlegendentry{$y=x^{\frac{1}{4}}$} \end{axis} \end{tikzpicture} \par \noindent \begin{tikzpicture} \begin{axis}[ axis lines = center, xlabel = $x$, ylabel = {$y$}, xmax = 4, xmin = -4, ymax = 2, ymin = -2, legend pos = south east, clip=true, ] \addplot [ domain=-4:4, samples=100, color=blue, line width=1.0pt, ] {CubeRoot(x)}; \addlegendentry{$y=x^{\frac{1}{3}}$} \addplot [ domain=-4:4, samples=100, color=red, line width=1.0pt, ] {FifthRoot(x)}; \addlegendentry{$y=x^{\frac{1}{5}}$} \end{axis} \end{tikzpicture} \end{document}

Peter Grill
  • 223,288
  • 1
    @Jake: Thanks. That was quite the space for a missing %. Whatever happened to multiple spaces are treated as a single space? :-) – Peter Grill Feb 08 '16 at 08:38