3

I want to plot a graph of 4=(x-2)^2+(y-2)^2, How do I do that within tikz plot? I have tried re-arranging but it just throws errors at me! \draw [domain=0:4] plot ({\x},{2-\sqrt{4\x-\x*\x}}); Is there an easy way for me to type a graph like this easily, I would appreciate it if the re-arrange was unnecissary!

Torbjørn T.
  • 206,688
  • 1
    Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem.

    While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem. Looking at your code snippet it seems fine to me (except for what appears to be an algebra error), but without the full MWE it is difficult to help further.

    – Peter Grill Dec 14 '15 at 21:34
  • 1
    Note that there is a difference between the macros used for typesetting math (such as \sqrt{x}) and the functions defined by pgfmath for doing calculations (e.g. sqrt()). You have used the former, when you should have used the latter. – Torbjørn T. Dec 14 '15 at 21:38

1 Answers1

9

What is causing the error is your use of \sqrt instead of sqrt. The former is a macro intended for typesetting a root, while the latter is a function defined by pgfmath that actually calculates a square root. In addition, multiplication has to be explicit I think, so 4*\x, not 4\x.

As such, a working version of your snippet would be

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [domain=0:4] plot ({\x},{2-sqrt{4*\x-\x*\x}});
\end{tikzpicture}
\end{document}

However, that is not a half circle of radius 2 centered on (2,2), which I guess is what you want.

The correct code for a full circle would be

\draw [domain=0:4,samples=200] plot ({\x},{2+sqrt(4-(\x-2)^2))})
                               plot ({\x},{2-sqrt(4-(\x-2)^2))});

Due to some roundoff problems, this does unfortunately not plot a full circle, I expect there is no point exactly at x=4. A way around that is to use the samples at key to ensure that the endpoints of the interval has a point, i.e.

\draw [samples at={0,0.01,...,3.99,4}] plot ({\x},{2+sqrt(4-(\x-2)^2))})
                                       plot ({\x},{2-sqrt(4-(\x-2)^2))});

A more powerful way of plotting such implicit functions is to use gnuplot as in How to plot smoothly an implicit function using gnuplot and tikz-pgf? The code for the equation in your question would be as below. gnuplot must be installed, and you have to compile with --shell-escape, e.g. pdflatex --shell-escape filename.tex.

\begin{tikzpicture}
    \draw plot[id=curve, raw gnuplot] function{
      f(x,y) = (y-2)**2 + (x-2)**2 - 4;
      set xrange [0:4];
      set yrange [0:4];
      set view 0,0;
      set isosample 1000,1000;
      set cont base;
      set cntrparam levels discrete 0;
      unset surface;
      splot f(x,y)
    };
\end{tikzpicture}

Of course, by far the simplest way of drawing a circle is \draw (2,2) circle[radius=2]; but that doesn't apply to more general functions.

Complete code and output:

enter image description here

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [domain=0:4,samples=200] plot ({\x},{2+sqrt(4-(\x-2)^2))})
                               plot ({\x},{2-sqrt(4-(\x-2)^2))});

\node at (2,2) {1};
\end{tikzpicture}
\begin{tikzpicture}
\draw [samples at={0,0.01,...,3.99,4}] plot ({\x},{2+sqrt(4-(\x-2)^2))})
                                       plot ({\x},{2-sqrt(4-(\x-2)^2))});
\node at (2,2) {2};
\end{tikzpicture}

\begin{tikzpicture}
    \draw plot[id=curve, raw gnuplot] function{
      f(x,y) = (y-2)**2 + (x-2)**2 - 4;
      set xrange [0:4];
      set yrange [0:4];
      set view 0,0;
      set isosample 1000,1000;
      set cont base;
      set cntrparam levels discrete 0;
      unset surface;
      splot f(x,y)
    };
\node at (2,2) {3};
\end{tikzpicture}
\begin{tikzpicture}
\draw (2,2) circle[radius=2];
\node at (2,2) {4};
\end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688