# Easiest way to plot a function with PGF/TikZ

I am new to TikZ and/or PGF (whatever the difference is :/), as you can see. Anyway, I need to draw the graph of the equation x^(2/3) + y^(2/3) = a^(2/3), where a = 2.
Any suggestions? Here is what I have so far.

\documentclass{article}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
Some text goes here.
\begin{center}
\begin{tikzpicture}[>=latex']
% Draw x-axis
\draw[very thick,->] (-3,0) -- (3.5,0)
node[right] {$x$};
% Draw y-axis
\draw[very thick, ->] (0,-3) -- (0,3.5)
node[above] {$y$};
% Draw graph of equation
\draw[smooth, color=blue, domain=-3:3, ultra thick, line cap=butt, samples=400]
plot (\x,{ (2^{2/3} - (\x)^{2/3} )^{3/2} });
\end{tikzpicture}
\end{center}
\end{document}

-

While tikz can do basic graphs, it is more of drawing package than a graphing package. For graphs, I would recommend you use the pgfplots package, which internally uses tikz to do the actual drawing. To use pgfplots you invoke the axis environment.

The following graph in blue is the function you had in your MWE:

Based on the math explained in ガベージコレクタ's solution, I have parmaterically graphed the solution to the original equation:

## Notes:

• Using pgfmathdeclarefunction isn't required, but makes it easier to read.
• This solution in the MWE has a discontinuity at 0, so if you use unbounded coords=discard, you will see messages of the sort:

coordinate (2Y7.559544e-3],3Y0.0e0]) has been dropped because it is unbounded (in y)

These go away if you use unbounded coords=jump.

• There have been a few cases where I needed to rewrite the expressions such as this as

\pgfmathdeclarefunction{Function}{1}{%
\pgfmathparse{exp((3/2)*ln( 2^(2/3) - exp((1/3)*ln(#1)) ))}}


but it does not appear to be necessary in this case.

Thought I would point it out in case you run into problems with other similar expressions.

• Also, it should be noted that pgf trigonometric functions expect the parameter to be in degrees, hence the use of the deg() function in the parametric graph.

## Code:

\documentclass{article}

\usepackage{pgfplots}% This uses tikz

\newcommand*{\A}{2}
\pgfmathdeclarefunction{Function}{1}{% as per original MWE
\pgfmathparse{(2^(\A) - (#1)^(1/3) )^(3/2)}%
}

\pgfmathdeclarefunction{SolutionX}{1}{%
\pgfmathparse{\A*(cos(deg(\t)))^3}%
}

\pgfmathdeclarefunction{SolutionY}{1}{%
\pgfmathparse{\A*(sin(deg(\t)))^3}%
}

\tikzset{My Line Style/.style={smooth, ultra thick, samples=400}}

\begin{document}
\begin{tikzpicture}
\addplot[My Line Style, color=blue, domain=0:3] (\x,{Function(\x)});
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[axis lines=middle, xmin=-2.5, xmax = 2.5, ymin=-2.5, ymax = 2.5]
\addplot[My Line Style, color=red,  variable=\t, domain=-2*pi:0]
({SolutionX(\t)},{SolutionY(\t)});
\end{axis}
\end{tikzpicture}
\end{document}

-
+1: could you add some explanation about the difference between TikZ and pgf as the OP seems a little unsure :) –  cmhughes Sep 29 '12 at 2:53
@cmhughes: Ok, added a brief blurb. Let me know if you think that is sufficient. –  Peter Grill Sep 29 '12 at 3:09
@Peter Grill: +1. I find your answer better, because you are using pgfplots. -but as you can see in the other answer your solution is not complete. –  Hans-Peter E. Kristiansen Sep 29 '12 at 19:41
@Hans-PeterE.Kristiansen: Yes I see how the math portion should be corrected but I was focused on the TeX part and showing how to graph an equation, not how to solve it. But will fix the math as well soon. –  Peter Grill Sep 29 '12 at 21:06
@Hans-PeterE.Kristiansen: Have up updated solution. –  Peter Grill Sep 30 '12 at 6:02

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{algebraic,plotpoints=300}

\def\a{2}
\def\x(#1){\a*cos(#1)^3}
\def\y(#1){\a*sin(#1)^3}

\begin{document}
\begin{pspicture}[showgrid=true](-2,-2)(2,2)
\psparametricplot[linecolor=red]{0}{\psPiTwo}{\x(t)|\y(t)}
\end{pspicture}
\end{document}


With PSTricks the solution is as follows,

## Animated version:

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{algebraic,plotpoints=300}
\usepackage[nomessages]{fp}
\FPeval\Delta{round(2*pi/30:2)}

\def\a{2}
\def\x(#1){\a*cos(#1)^3}
\def\y(#1){\a*sin(#1)^3}

\begin{document}
\multido{\n=0.00+\Delta}{31}{%
\begin{pspicture}[showgrid=true](-2,-2)(2,2)
\psparametricplot[linecolor=red]{0}{\n}{\x(t)|\y(t)}
\end{pspicture}}
\end{document}


## With implicit function plot:

\documentclass[pstricks,border=3pt]{standalone}
\usepackage{pst-func}
\begin{document}
\begin{pspicture*}(-2,-2)(2,2)
\psplotImp[linecolor=red,stepFactor=0.05,algebraic](-2.1,-2.1)(2.1,2.1){(x^2)^(1/3)+(y^2)^(1/3)-4^(1/3)}
\end{pspicture*}
\end{document}

-
I am still trying to learn how to use the packages you used in your solution. I am unable to get LaTeX to reproduce your same results. I'll keep working on it though. Thanks for your answer! –  Nathan Sep 30 '12 at 20:43
@Nathan: use xelatex or latex-dips-ps2pdf rather than pdflatex. –  kiss my armpit Sep 30 '12 at 23:15