# Pgfplots : can't plot some usual mathematical functions

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}[]
\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)};


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}$}]
\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"

-
Is there a way to generalise this? If for instance I wanted a jump at 0.8. – Evpok Jun 4 '15 at 23:11

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]
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[enlargelimits=false]
\end{axis}
\end{tikzpicture}
\end{document}

-

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 :)

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}

\end{document}

-
Well I was initially wrong - this is in fact the right function. – Hans-Peter E. Kristiansen Aug 31 '12 at 11:38
whoever downvoted this answer, please specify why – cmhughes Aug 31 '12 at 15:16
I downvoted by mistake, but I can not undo it. – Hans-Peter E. Kristiansen Aug 31 '12 at 17:16
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 v A Nov 26 '13 at 23:00
I fixed your mistake with the voting. – alexy13 Feb 3 '14 at 20:12

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]