7

I would like to plot the function x2(1 − x2) − y2 = 0 in LaTeX.

I tried running following code

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}

\usepackage{pgfplots}
\pgfplotsset{width=10cm}



\begin{document}

\begin{tikzpicture}
  \begin{axis}[
      view={0}{90},
      no markers,
    ]

    \addplot3+[
      contour gnuplot={
        levels=0,
        labels=false,
      }
    ]
    {x^2(1-x^2)-y^2};

  \end{axis}
\end{tikzpicture}


\end{document}

but the output is enter image description here

But should be

enter image description here

Thanks for helping.

New contributor
Aris Martin Accola is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
7

enter image description here

@Superuser27 is correct about the missing *. Additionally, I don't understand why you're using \addplot3. Finally, that's not a function.

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}

\usepackage{pgfplots}
\pgfplotsset{width=10cm}



\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    ]
    \addplot[domain=-1:1,samples=1000]{ sqrt(x*x*(1-x*x)) * -1 };
    \addplot[domain=-1:1,samples=1000]{ (x*x*(1-x*x))^0.5 * 1 };
    \draw (axis cs:1,-0.07,1)--(axis cs:1,0.07);
  \end{axis}
\end{tikzpicture}


\end{document}
  • I used addplot for the contour gnuplot. So i don‘t have to calculate the function equal 0 for y. – Aris Martin Accola Mar 15 at 14:44
5

math

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    ]
    \addplot[no marks,variable=\t,domain=0:360,samples=100] ({cos(\t)}, {0.5*sin(2*\t)});
  \end{axis}
\end{tikzpicture}

\end{document}

demo

  • Best answer.... – JPi 2 days ago
2

I had some trouble running your code (compatibility issues) but now it should work. I think the answer is simply that you missed a * in {x^2(1-x^2)-y^2};.

enter image description here

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}

\usepackage{pgfplots}
\pgfplotsset{width=10cm}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      view={0}{90},
      no markers,
    ]
    \addplot3[
      contour gnuplot={
        levels=0,
        labels = false,
        },
      domain=-2:2,
      domain y=-1.3:1.3,
    ]
    {x^2*(1-x^2)-y^2};

  \end{axis}
\end{tikzpicture}
\end{document}
  • how did you manage to compile it, I am having hard-time compiling it with the error being: Package pgfplots Error: sorry, plot file{texstudio_Ml6192_contourtmp0.table} could not be opened. {x^2*(1-x^2)-y^2};. If you have some idea, let me know. – Raaja Mar 14 at 13:24
  • 2
    I got the same error. I had to look around online and found that Overleaf also complains but still gives you an output. @Raaja – Superuser27 Mar 14 at 13:27
  • 1
    Huhhh, I see. Thanks for the update! – Raaja Mar 14 at 13:28
  • 2
    Usually x^2 is implemented using exp(2*log(x)) which can't handle negative numbers. – John Kormylo Mar 14 at 13:55
  • 1
    @Raaja I had the same problem first compiling it on my computer. So make sure you have installed gnuplot on your computer and then you need to add it to your path so that the program can find gnuplot. You also need to enable —shell-escape. – Aris Martin Accola Mar 15 at 14:41

Your Answer

Aris Martin Accola is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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