[Tex/LaTex] Pgfplots : can’t plot some usual mathematical functions

pgfplotstikz-pgf

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"

Best Answer

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}
Related Question