tikz problem (non-continuous function) [duplicate]

I want to plot a function x^3/(x^2-1) but it doesn't work.

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[domain=-5:5,smooth]
\draw[->] (-5,0) -- (5,0) node[right] {$x$};
\draw[->] (0,-5) -- (0,5) node[above] {$y$};
\draw [color=blue, thick] plot ({\x},{pow(\x,3)/(pow(\x,2)-1)});
\end{tikzpicture}

\end{document}


It does something like this. It should be non-continuous function, not this.

How to fix it? Thanks.

marked as duplicate by dustin, Heiko Oberdiek, mafp, barbara beeton, Claudio FiandrinoAug 27 '13 at 6:08

• The output of this are only axis and a caption. – goLK Aug 26 '13 at 21:19
• I've added a little bit of information on how to add your own labels for ticks and such to my answer. If this doesn't help feel free to ask. – Philipp Aug 26 '13 at 23:15

If using pgfplots would be ok for you then you could avoid such problems. Instead of the standalone class you can, of course, use any other documentclass you like.

Edit: I pimped my example a little. If you want to label some points or add function definitions etc. to your plot you can do this like in any other tikzpicture: creating nodes and drawing works in the same way as usual. To make your life easy you can access the in-plot coordinates via axis cs:, so if you want to create a node at the in-plot point x=1 and y=0 you can use something like \node at (axis cs:1,0) {$x=1$};.

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
restrict y to domain=-5:5,
samples=1000,
ticks=none,
xmin = -5, xmax = 5,
ymin = -5, ymax = 5,
unbounded coords=jump,
axis x line=middle,
axis y line=middle,
xlabel={$x$},
ylabel={$y$},
x label style={
at={(axis cs:5.02,0)},
anchor=west,
},
every axis y label/.style={
at={(axis cs:0,5.02)},
anchor=south
},
legend style={
at={(axis cs:-5.2,5)},
anchor=west, font=\scriptsize
}
]

\addplot[color=blue, mark=none, domain=-5:5] {x^3)/((x^2) - 1)};
\addlegendentry[blue]{$f(x)=\frac{x^3}{x^2 - 1}$}
\draw[dashed] ({axis cs:1,0}|-{rel axis cs:0,1}) -- ({axis cs:1,0}|-{rel axis cs:0,0});
\draw[dashed] ({axis cs:-1,0}|-{rel axis cs:0,1}) -- ({axis cs:-1,0}|-{rel axis cs:0,0});
\node[below right, font=\scriptsize] at (axis cs:1,0) {$x=1$};
\node[below left, font=\scriptsize] at (axis cs:-1,0) {$x=-1$};
\end{axis}
\end{tikzpicture}
\end{document}


Edit:

Here is a pure tikz solution. It's not very elegant but in case one doesn't want to use pgfplots it might be a good starting point.

 \documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[smooth, scale=0.5]
\pgfmathsetmacro\ymax{pow(1.05,3)/(pow(1.05,2)-1)}
\draw[->] (-10,0) -- (10,0) node[right] {$x$};
\draw[->] (0,-\ymax) -- (0,\ymax) node[above] {$y$};
\draw [color=blue, thick, domain=-10:-1.05, samples=200] plot ({\x},{pow(\x,3)/(pow(\x,2)-1)});
\draw [color=blue, thick, domain=-0.96:0.96, samples=200] plot ({\x},{pow(\x,3)/(pow(\x,2)-1)});
\draw [color=blue, thick, domain=1.05:10, samples=200] plot ({\x},{pow(\x,3)/(pow(\x,2)-1)});
\draw [color=black, dashed] (-1, -\ymax) -- (-1, \ymax);
\draw [color=black, dashed] (1, -\ymax) -- (1, \ymax);

\end{tikzpicture}

\end{document}


• Thanks for your reply but I use my documentclass and I also need similar looking graphs. – goLK Aug 26 '13 at 21:22
• @goLK here is just using the standalone class to generate the image. The image wont be affected if you use the article class instead. You can always remove the x and y tick marks and add y and x if that is what you mean by similar looking. – dustin Aug 26 '13 at 21:27
• @goLK I improved the look of my example a little. Is your problem with the documentclass solved by dustin's comment? – Philipp Aug 26 '13 at 21:49
• Thanks but "! Package tikz Error: Unknown coordinate system 'ticklabel*'.". – goLK Aug 26 '13 at 21:55
• @goLK Strange, for me it's compiling fine. Have you tried compiling just my example or does the problem arise in your real document? – Philipp Aug 26 '13 at 21:59