4

I would like to use only the tikz and pgfplots package to graph the function $f(x)=\sqrt[3]{(x^{3}-x^{2})}$. How should I proceed? Does the code below not compile?

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\draw[->] (-4,0) -- (4,0) node[right]{$x$};
\draw[->] (0,-4) -- (0,4) node[above]{$y$};
\draw[domain=-2:2, samples=100] plot(\x, {(\x^(3)-\x^(2))^(1/3)});
\end{tikzpicture}

\end{document}

Here is an overleaf link with the error we observed: Click Here!

4
  • 1
    This code doesn't plot the cube root, but the square of x³–x². – Bernard Feb 9 at 19:44
  • @Bernard In fact, I was wrong to enter the code. I already made the correction! I apologize! – fsbmat Feb 9 at 21:06
  • Don't be sorry. This kind of errors happen to all of us from time to time. – Bernard Feb 9 at 21:08
  • BTW (x*x-x)*x is faster and more accurate than x^3-x^2. Try to avoid using ^ when computing. It should be noted that y^3=x has three possible solutions. generally one real and two complex. – John Kormylo Feb 9 at 22:57
6

A pstricks solution:

\documentclass[svgnames, border=3pt]{standalone}
\usepackage{pst-plot}
\usepackage{auto-pst-pdf}

\begin{document}

\psset{unit=2cm, algebraic, arrowinset=0.12, arrowsize=3pt, linejoin=1, showorigin=false}

\begin{pspicture*}(-2.6,-2.6)(2.6,2.6)
\psaxes[linecolor=LightSteelBlue, tickcolor=LightSteelBlue, arrows =->, ticksize=2pt -2pt, labelFontSize=\scriptstyle](0,0)(-2.6,-2.6)(2.6,2.6)[$x$,-135] [$y$,-135]
\uput[dl](0,0){$O$}
\psset{linewidth=1.2pt, linecolor=IndianRed, plotpoints=200, plotstyle=curve}
\psplot{-2.6}{1}{-(x^2-x^3)^(1/3)}%
\psplot{1}{2.6}{(x^3-x^2)^(1/3)}%
\psline[linewidth=0.6pt, linecolor=Crimson!50](-2.8,-3.133)(2.8,2.467)
\end{pspicture*}

\end{document} 

enter image description here

5
  • Hi @Bernard, it was great. Thank you! However, this is a graphic for a project where we wanted to use only tikz, pgfplots and some of their libraries. – fsbmat Feb 9 at 23:03
  • I understand. Yet, many things are simpler to do with pstricks. – Bernard Feb 9 at 23:05
  • If it doesn't work with tikz and pgfplots, we really have to go for pstricks. Thank you for your help and availability! – fsbmat Feb 9 at 23:09
  • I think it should work, but unfortunately, I don't know tikz well enough to help. – Bernard Feb 9 at 23:15
  • Got it, but I appreciate your help anyway! – fsbmat Feb 9 at 23:22
4

Got it! I used the fact that $x^(1/3)=(x/abs(x))*(abs(x))^(1/3)$. In that case, it compiled perfectly!

\documentclass{report}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-4,0) -- (4,0) node[right]{$x$};
\draw[->] (0,-4) -- (0,4) node[above]{$y$};
\draw[domain=-2:2, samples=100, blue] plot(\x, {((((\x)^(3)-(\x)^(2)))/(abs(((\x)^(3)-(\x)^(2)))))*(abs(((\x)^(3)-(\x)^(2))))^(1/3)});
\end{tikzpicture}
\end{document}

enter image description here

Thanks to everyone who helped! We will keep good options here!

1
  • 1
    I have just checked pgfmanual. There is function sign(x), so you can write sign(f(\x))*pow(abs(f(\x)),1/3) in this case. – Black Mild Feb 10 at 5:07
4

Here are two Asymptote solutions for comparison with TikZ or pgfplots incoming answers as you are expected.

  1. Asymptote has built-in cubic root function, so just go straight with it

enter image description here

// http://asymptote.ualberta.ca/
unitsize(1cm);
import graph;
real f(real x) {return cbrt(x^3-x^2);} //<<< the cubic root function
path g = graph(f,-2,2,n=1000,operator --); // operator .. is not good around (0,0)
draw(g,red);
axes("$x$","$y$");
  1. Asymptote has contour module for graphs of implicit function.

enter image description here

unitsize(1cm);
import contour;
import graph;
real F(real x, real y) { return x^3 - x^2 -y^3; }
guide[][] g = contour(F,a=(-2,-2), b=(2,2), new real[] {0});
draw(g[0],magenta);
axes("$x$","$y$");

Update Using TikZ with sign function.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=\x*\x*\x-\x*\x;}]
\draw[->] (-3,0) -- (3,0) node[below right]{$x$};
\draw[->] (0,-3) -- (0,3) node[below left]{$y$};
\draw[orange,thick] plot[domain=-2:3,samples=300] (\x,{sign(f(\x))*pow(abs(f(\x)),1/3)}) 
;
\end{tikzpicture}
\end{document}

My bad and tricky code: In trying to use TikZ, I have to use a trick to get rid off negative base in power function, and joining pieces of the graph in different domains.

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[samples=100,smooth,
declare function={f(\x)=\x*\x*\x-\x*\x;}
]
\draw[->] (-3,0) -- (3,0) node[below right]{$x$};
\draw[->] (0,-3) -- (0,3) node[below left]{$y$};
\draw[orange,thick]
plot[domain=-2:1] (\x,{-pow(-f(\x),1/3)})--(1,0)--
plot[domain=1:3] (\x,{pow(f(\x),1/3)}) 
;
\end{tikzpicture}
\end{document}
6
  • I appreciate the suggestion, but we are trying to build a report for building graphs in LATEX using only the tikz and pgfplots packages. So we ask for help! – fsbmat Feb 9 at 21:03
  • Your piece of code unfortunately is no MWE. What tikzlibraries do you use? What error messages do you get? – Harald Lichtenstein Feb 9 at 21:51
  • @fsbmat see my update with TikZ. Tricky ^^ – Black Mild Feb 9 at 22:33
  • Hi @BlackMild I did. Am I evaluating why you use $-f (x)$ and $yscale = -1$? – fsbmat Feb 9 at 23:00
  • 1
    @fsbmat I correct my code a bit. Now without yscale: \draw[orange,thick] plot[domain=-2:1] (\x,{-pow(-f(\x),1/3)})--(1,0)-- plot[domain=1:3] (\x,{pow(f(\x),1/3)}) ; – Black Mild Feb 10 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.