30

I'm beginning to learn pgfplots and I would like to plot some functions: cubic root, inverse, and some trigonometric functions.

The problem is that for y=1/x function, it joins up the points between negative and positive parts of the domain: we can't see the asymptote.

\documentclass{minimal}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
\addplot [domain=-10:10, samples=100]{x^(-1)};
\end{axis}
\end{tikzpicture}
\end{document}

With the function y=x^{1/3}, it doesn't display the negative part of the domain. And with the trigonometric functions, it just doesn't do anything right...

\addplot[domain=-27:27]{x^(1/3)};
\addplot[domain=-2*pi:2*pi]{cos(rad(x))};

thank you very much if you can help me a little bit.


thank you very much for your answers, it's really helping. Just a last thing: the cubic root function has a negative part in its domain that cannot be displayed. Do you know why?

\begin{tikzpicture} 
\begin{axis}[
    width=8cm,xlabel={$x$},
    ylabel={$y$},grid=both, axis x line=middle, axis y line=middle, 
    title={$f(x)=x^{1/3}$}] 
\addplot[blue,domain=-27:27, no markers,samples=100] {x^(1/3)}; 
\end{axis} 
\end{tikzpicture}

NB: Yes, the cubic root function has a partially negative domain, and no, there is no imaginary part. NB: i'm sorry i'm insisting on one of my first questions in this comment which is supposed to be an "answer", i'm just new here and, as i'm not registered yet, i don't know how i can ask something related with the topic in a new "question comment"

1
  • Is there a way to generalise this? If for instance I wanted a jump at 0.8.
    – Evpok
    Jun 4, 2015 at 23:11

3 Answers 3

27

To keep the negative and positive parts of the 1/x plot separate, you need to make sure that the function is evaluated at x=0. If your domain is symmetric, you can just specify an odd number of samples (samples=101, for example). You also have to make sure that non-real values aren't just silently discarded, but cause a jump in the plot. To do that, specify unbounded coords=jump (instead of the default behaviour discard).

The trigonometric functions in PGF expect degrees, so you'll have to convert radians to degrees using deg(x) (not rad(x), that's used for converting degrees to radians).

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[enlargelimits=false]
\addplot [domain=-10:10, samples=101,unbounded coords=jump]{x^(-1)};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[enlargelimits=false]
\addplot[domain=-2*pi:2*pi, samples=100]{cos(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}
18

The other answers provide nifty ways to plot 1/x, but no one has covered how to plot x^(1/3)

The issue comes up in quite a few different graphing programs/calculators. Cube roots are often calculated using logarithms, which is why they sometimes appear not to be defined for negative numbers. Of course, we know that we can take the cube root of any real number, so we have to trick the program/calculator.

One way to do this is to plot

x/|x| * (|x|)^(1/3)

which plots the cube root function, and sneakily switches the signs appropriately. Of course, this function is not defined at 0, so it's not actually equal to the cube root function, but it does the trick for us :)

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
    \begin{axis}
       \addplot[blue,domain=-10:10, samples=200]{x/abs(x)*abs(x)^(1/3)};
    \end{axis}
\end{tikzpicture}

\end{document}
5
  • Well I was initially wrong - this is in fact the right function. Aug 31, 2012 at 11:38
  • whoever downvoted this answer, please specify why
    – cmhughes
    Aug 31, 2012 at 15:16
  • I downvoted by mistake, but I can not undo it. Aug 31, 2012 at 17:16
  • 1
    If you add declare function = { cbrt(\x) = \x / abs(\x) * abs(\x)^(1/3); } to the tikzpicture arguments, you can conveniently use cbrt as the cube root function in more complex expressions.
    – Ruud
    Nov 26, 2013 at 23:00
  • I fixed your mistake with the voting.
    – alexyorke
    Feb 3, 2014 at 20:12
6

I think your expectations are too great as pgfplots is not a Computer Algebra System(CAS). So you need to help it by massaging the data. Also samples option makes a lot of difference since the plots are really connecting the dots, and to see whether a value is unbounded it has to evaluate at that point otherwise the results will be finite and it will connect those dots.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[restrict y to domain=-9.9:9.9]
\addplot [domain=-10:10, samples=200]{x^(-1)};
\addplot[blue,domain=-10:10, samples=200]{x^(1/3)};
\addplot[red,domain=-2*pi:2*pi, samples=200]{sin(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .