2

I want to draw hyperbola with pgfplots:

(x+1)^2/4-(y-2)^2/9=1

  • 2
    Have you tried anything? This site usually works best when you present your best attempt. It's also polite to give us a minimal working example that we can start from. – Teepeemm Aug 13 '16 at 2:29
  • @Teepeemm I'm starting to use TikZ , so I do not show any initial work. – casio Aug 13 '16 at 2:31
  • The easiest solution is to use (y-2)^2/9=(x+1)^2/4-1 and take the square root (plus or minus). Note that x<-3 or x>1, so you will need four \addplots total. – John Kormylo Aug 13 '16 at 3:35
6

As stated in comments, you can reformulate the equation as "y = f(x)" with the problem of roots.

An alternative is to interprete it as "f(x,y)= (x+1)^2/4-(y-2)^2/9" and draw the contour "f(x,y) = 1" :

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.13}


\begin{document}

\begin{tikzpicture}
    \begin{axis}[view={0}{90}]
    \addplot3[domain=-10:10,
        contour gnuplot={labels=false,levels={1}}
    ] {(x+1)^2/4-(y-2)^2/9};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

The value domain=-10:10 determines the computed range. Since domain y is missing, pgfplots assumes that it should also use -10:10.

Note that you need to compile this by means of pdflatex -shell-escape file.tex and you need gnuplot installed.

Note that the approach as such works for any kind of plot program, not just pgfplots.

1

I would strongly recommend googling around for a tikz/pgf tutorial to learn at least the basics on your own. You'll learn much more that way.

The most straightforward approach here is to solve for y=2±√(9*(x+1)^2/4-9) with x < -3 or 1 < x. Plotting this requires four commands, and results in

\begin{tikzpicture}
    \begin{axis}[xmin=-10,xmax=10,ymin=-10,ymax=10]
        \addplot[domain=-10:-3] {2+sqrt(9*(x+1)^2/4-9)};
        \addplot[domain=-10:-3] {2-sqrt(9*(x+1)^2/4-9)};
        \addplot[domain=1:10] {2+sqrt(9*(x+1)^2/4-9)};
        \addplot[domain=1:10] {2-sqrt(9*(x+1)^2/4-9)};
    \end{axis}
\end{tikzpicture}

first plot

This is not a great plot (there's the obvious gap on the left, and if you look closer, you'll see that the right has a few straight segments and angles near the vertex). To fix this, we can change to a parametric plot: x=2*sec(t)-1, y=3*tan(t)+2. Plotting this still requires two commands (to avoid the vertical asymptotes of sec and tan) and results in:

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.13}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[xmin=-10,xmax=10,ymin=-10,ymax=10]
        \addplot[domain=-89:89] ({2*sec(x)-1},{3*tan(x)+2});
        \addplot[domain=91:269] ({2*sec(x)-1},{3*tan(x)+2});
    \end{axis}
\end{tikzpicture}

\end{document}

final plot

1

text

\documentclass[border=2pt]{standalone} 
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-10,xmax=10, ymin=-15, ymax=15,
   restrict x to domain=-20:20]% remove crossing lines at t=90 and t=270
  \addplot[variable=t,domain=0:360,samples=200] ({2*sec(t)-1}, {3*tan(t)+2});
\end{axis}
\end{tikzpicture}
\end{document}

demo

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.