# Plotting an implicit function using TikZ

How could one plot an implicit function, like (x^2+y^2)^3-4x^2y^2 using TikZ?

I know gnuplot supports them, but they are drawn using a surface built from the function and intersecting the result with the z=0 plane. How would one implement this work flow directly from TikZ? I know you can easily pass explicit functions to gnuplot but I don't want to split up the implicit function in its constituent parts if it's not necessary.

Of course, it's possible to plot in the gnuplot program and save the results, but I'm curious whether there is a more direct way :).

-

Probably the easiest thing to do would be to use TikZ's front end to gnuplot. If you use the raw gnuplot option you can pass a more complicated script to gnuplot to make it to what you want (rather than just plot a simple function).

I think it's almost impossible to do within TeX itself, which is after all a text processing system and not a computer algebra system. You could use pgfmath (the math engine within TiKZ) to generate the surface points, and only plot the points where z is sufficiently close to 0. But how would you connect these points?

Another method would be to use the function to write a differential equation and then implement Euler's method to plot a curve. But that probably won't be as pretty as you want—assuming the curve is closed you would have to guess-and-check to make sure you plotted enough to close the curve and not too much to overlap.

There might also be a LuaTeX option but I don't know anything about that.

This particular curve is highly singular so first-stab TeX efforts probably wouldn't be very successful here.

-
Sorry, maybe I should've been more clear, any solution was good but I was looking/hoping for something like raw gnuplot. I am trying it as we speak, thank you for the answer. – Pieter Oct 31 '10 at 11:07
Sorry I misunderstood. I reworded my answer so it doesn't assume that attitude. – Matthew Leingang Oct 31 '10 at 11:23
It kinda works now, gnuplot returns an impression of the implicit curve. The problem is that raw gnuplot returns the samples and TikZ just connects the dots. But I guess that's worth a follow-up question :). – Pieter Oct 31 '10 at 11:37
This curve can be parametrized; maybe that makes your life easier. – Matthew Leingang Oct 31 '10 at 12:20
Of course it's possible within TeX, but I agree that it's probably inappropriate to attempt this since it would be way too slow. – Hendrik Vogt Oct 31 '10 at 13:01

With PSTricks (If you don't mind to make a try):

\documentclass[pstricks]{standalone}
\usepackage{pst-func}
\begin{document}

\begin{pspicture}(-2,-2)(2,2)
\psaxes{<->}(0,0)(-1.5,-1.5)(1.5,1.5)[$x$,0][$y$,90]
\psplotImp[
linecolor=red,
stepFactor=0.1,
algebraic,
](-1.5,-1.5)(1.5,1.5){(x^2+y^2)^3-4*x^2*y^2}
\end{pspicture}

\end{document}

-

This blog post explains how to plot an algebraic curve using raw gnuplot

http://pbelmans.wordpress.com/2010/11/11/howto-draw-algebraic-curves-using-pgftikz/

I didn't try it, but it looks cool!

-
That's actually my own post discussing how to do this, and produce plots that don't suffer from the bug discussed in tex.stackexchange.com/questions/4745/…. Nowadays I think pgfplots supports some feature to do this without hacking things. – Pieter Mar 15 '13 at 7:20