6

I would like to plot the area bounded by the following four curves:

  • x^2 - y^2 = 4
  • x^2 - y^2 = 1
  • xy = 1
  • xy = 2

I'm fond of pstricks but tikz is fine too, whichever is simpler would be best.

I tried doing it myself, but I can't even get as far as plotting x^2 - y^2 = c because the y^2 is throwing me off. I've also tried looking at other examples of shading areas but it looks overly complicated.

Also, if anyone wouldn't mind annotating some lines to explain what it does so that I can try to recreate some other graphs that would be awesome, but not essential.

Herbert's answers are great, however I would like the negative side as well. I can't figure out how clipping actually works.

I've butchered Herbert's code and I'm only missing the shaded area:

\documentclass[11pt]{article}
\usepackage{pst-plot}
\begin{document}

\psset{unit=2}
\begin{pspicture}(-3.5,-3.5)(3.5,3.5)
\psaxes{->}(0,0)(-3.5,-3.5)(3.5,3.5)[$x$,-90][$y$,90]
\psset{algebraic,plotpoints=51}
\psclip[linestyle=none]{%
  \pscustom{\psplot{1}{3}{1/x}\lineto(3,3)}
  \pscustom{\psplot{2}{1}{sqrt(x^2-1)}\lineto(4,1)}
  \pscustom{\psplot{2}{2.5}{sqrt(x^2-4)}\lineto(-1,3)}  
  \pscustom{\psplot{2.5}{1}{2/x}\lineto(1,-1)}  
}
  \psframe*[linecolor=magenta,opacity=0.3](3,3)
\endpsclip

\psplot[linecolor=red]{0.35}{3}{1/x}%
\psplot[linecolor=green]{3}{1}{sqrt(x^2-1)}%
\psplot[linecolor=yellow]{3}{2}{sqrt(x^2-4)}%
\psplot[linecolor=blue]{3}{0.65}{2/x}%
\psplot[linecolor=green]{3}{1}{-sqrt(x^2-1)}%
\psplot[linecolor=yellow]{3}{2}{-sqrt(x^2-4)}%

%\psclip[linestyle=none]{%
%  \pscustom{\psplot{-0.5}{-3}{1/x}\lineto(-3,-3)}
%  \pscustom{\psplot{-3}{-1}{sqrt(x^2-1)}\lineto(-4,-1)}
%  \pscustom{\psplot{-3}{-2}{sqrt(x^2-4)}\lineto(-1,3)}  
%  \pscustom{\psplot{-3}{-0.75}{2/x}\lineto(-1,1)}  
%}
%  \psframe*[linecolor=magenta,opacity=0.3](0,0)(-3,-3)
%\endpsclip

\psplot[linecolor=red]{-0.35}{-3}{1/x}%
\psplot[linecolor=green]{-3}{-1}{-sqrt(x^2-1)}%
\psplot[linecolor=yellow]{-3}{-2}{-sqrt(x^2-4)}%
\psplot[linecolor=green]{-3}{-1}{sqrt(x^2-1)}%
\psplot[linecolor=yellow]{-3}{-2}{sqrt(x^2-4)}%
\psplot[linecolor=blue]{-3}{-0.65}{2/x}%

\end{pspicture}
\end{document}
7

That is not easy, because you have to find first the intersections. And, of course, I suppose you mean only the area in the positive part. The following example works only with latex->dvips->ps2pdf

\documentclass[11pt]{article}
\usepackage{pst-intersect}
\begin{document}

\psset{unit=2}
\begin{pspicture*}(-0.5,-0.5)(3,3)
\psaxes{->}(0,0)(2.75,2.75)[$x$,-90][$y$,90]
\psset{algebraic,plotpoints=51,linewidth=1.5pt}
\pssavepath[linecolor=red!40]{Pa}{\psplot{0.15}{3}{1/x}}%
\pssavepath[linecolor=blue!40]{Pb}{\psplot{0.15}{3}{2/x}}%
\pssavepath[linecolor=green!40]{Pc}{\psplot{0}{3}{(x-1)*(x+1)}}%
\pssavepath[linecolor=yellow]{Pd}{\psplot{0}{3}{(x-2)*(x+2)}}%
\psintersect[name=A]{Pa}{Pc}\psintersect[name=B]{Pa}{Pd}
\psintersect[name=C]{Pb}{Pc}\psintersect[name=D]{Pb}{Pd}
\pscustom[fillcolor=magenta,fillstyle=solid,linestyle=none]{%
  \psplot{\psGetIsectCenter{A}{}{1} I-A1.x}%
         {\psGetIsectCenter{C}{}{1} I-C1.x}{(x-1)*(x+1)}
  \psplot{\psGetIsectCenter{C}{}{1} I-C1.x}%
         {\psGetIsectCenter{D}{}{1} I-D1.x}{2/x}
  \psplot{\psGetIsectCenter{D}{}{1} I-D1.x}%
         {\psGetIsectCenter{B}{}{1} I-B1.x}{(x-2)*(x+2)}
  \psplot{\psGetIsectCenter{B}{}{1} I-B1.x}%
         {\psGetIsectCenter{A}{}{1} I-A1.x}{1/x}
}
\end{pspicture*}

\end{document}

enter image description here

Clipping is also possible, but it is not so easy to understand how the clipping path has to be build. This example works also with xelatex

\documentclass[11pt]{article}
\usepackage{pst-plot}
\begin{document}

\psset{unit=2}
\begin{pspicture}(-3,-3)(3,3)
\psaxes{->}(0,0)(-3,-3)(2.75,2.75)[$x$,-90][$y$,0]
\psset{algebraic,plotpoints=51}
\psclip[linestyle=none]{%
  \pscustom{\psplot{1}{3}{1/x}\lineto(3,3)}
  \pscustom{\psplot{2}{1}{sqrt(x^2-1)}\lineto(4,1)}
  \pscustom{\psplot{2}{2.5}{sqrt(x^2-4)}\lineto(-1,3)}  
  \pscustom{\psplot{2.5}{1}{2/x}\lineto(1,-1)}  
}
  \psframe*[linecolor=magenta,opacity=0.3](3,3)
\endpsclip
\psplot[linecolor=red]{0.7}{3}{1/x}%
\psplot[linecolor=green]{2}{1}{sqrt(x^2-1)}%
\psplot[linecolor=yellow]{2}{2.5}{sqrt(x^2-4)}%
\psplot[linecolor=blue]{2.5}{1}{2/x}%
\psclip[linestyle=none]{%
    \pscustom{\psplot{-1}{-3}{1/x}\lineto(-3,-3)}
    \pscustom{\psplot{-2}{-1}{-sqrt(x^2-1)}\lineto(-4,-1)}
    \pscustom{\psplot{-2}{-2.5}{-sqrt(x^2-4)}\lineto(1,-3)} 
    \pscustom{\psplot{-2.5}{-1}{2/x}\lineto(-1,1)}  
}
\psframe*[linecolor=magenta,opacity=0.3](-3,-3)
\endpsclip
\psplot[linecolor=red]{-0.7}{-3}{1/x}%
\psplot[linecolor=green]{-2}{-1}{-sqrt(x^2-1)}%
\psplot[linecolor=yellow]{-2}{-2.5}{-sqrt(x^2-4)}%
\psplot[linecolor=blue]{-2.5}{-1}{2/x}%
\end{pspicture}

\end{document}

enter image description here

For the clipping path: If one curve ends then the next one starts with a straight line between these two curves. This is the reason why I choose the '\lineto` macro which moves my current point to a place where a follwing straight line to the next curve doesn't go through the clipped area. That's all.

  • I've tried to get the negative side as well (code is in the question) but I can't figure out how you've shaded the area. The documentation for psclip online doesn't seem to be anything like what you've done. Could you explain how it works or send me a link to the documentation you used? Thanks! – CanofDrink Apr 5 '16 at 12:29
  • 1
    I'll have a look after coffee – user2478 Apr 5 '16 at 12:44
  • I've just noticed as well, in your first method your yellow and green lines don't look correct. It's probably because of the (x-1)*(x+1) bit. I'm more than happy with your second answer though so it's not an issue. – CanofDrink Apr 5 '16 at 12:56
  • 1
    Yes, i know, I' d forgotten the sqrt. However, it's not important for the solution – user2478 Apr 5 '16 at 13:00
  • 1
    It moves the current point to (-3,-3) you can use any other useful coordinates. Important is the fact that there will be a straight line to the starting Point of the next curve. And that line shouldn't go through the clipped area. Use linestyle=solid instead of none, then you'll see what happens – user2478 Apr 5 '16 at 14:56
8

Here is an example using pgfplots and its fillbetween libary.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}

\begin{document}

    \begin{tikzpicture}
      \begin{axis}[
        set layers,
        axis lines=middle,
        xmin=-3.1,xmax=3,xtickmax=2.9,
        ymin=-3.1,ymax=3,ytickmax=2.9,
        samples=100
      ]
        \addplot[name path=A+,brown, domain=2:3] {sqrt(x^2-4)};
        \addplot[brown, domain=2:3] {-sqrt(x^2-4)};
        \addplot[brown,domain=-3:-2] {sqrt((x)^2-4)};
        \addplot[name path=A-,brown,domain=-3:-2] {-sqrt((x)^2-4)};
        \addplot[name path=B+,green,domain=1:3] {sqrt(x^2-1)};
        \addplot[green,domain=1:3] {-sqrt(x^2-1)};
        \addplot[green,domain=-3:-1] {sqrt(x^2-1)};
        \addplot[name path=B-,green,domain=-3:-1] {-sqrt((x)^2-1)};
        \addplot[name path=C+,red,domain=.15:3]{1/x};
        \addplot[name path=C-,red,domain=-3:-.15]{1/x};
        \addplot[name path=D+,blue,domain=.15:3]{2/x};
        \addplot[name path=D-,blue,domain=-3:-.15]{2/x};

        \path[%draw,line width=3,orange,
          name path=AC+,
          intersection segments={
            of=A+ and C+,
            sequence={L2[reverse] -- R1[reverse]}
          }
        ];
        \path[%draw,line width=3,purple,
          name path=BD+,
          intersection segments={
            of=B+ and D+,
            sequence={L1 -- R2}
          }
        ];

        \path[%draw,line width=3,orange,
          name path=AC-,
          intersection segments={
            of=A- and C-,
            sequence={L1 -- R2}
          }
        ];
        \path[%draw,line width=3,purple,
          name path=BD-,
          intersection segments={
            of=B- and D-,
            sequence={L2[reverse] -- R1[reverse]}
          }
        ];

        \pgfonlayer{axis grid}
          \path [
            fill=orange!30,
            intersection segments={
              of=AC+ and BD+,
              sequence={R2--L2}
            }
          ]--cycle;
          \path [
            fill=orange!30,
            intersection segments={
              of=AC- and BD-,
              sequence={R2--L2}
            }
          ]--cycle;
        \endpgfonlayer

      \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here


Here is an example with only the positive part. Unfortunaly the order and/or the direction of the path segments seems to change if the x- and/or the y-range is changed.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}

\begin{document}

    \begin{tikzpicture}
      \begin{axis}[
        set layers,
        axis lines=middle,
        xmin=0,xmax=3,xtickmax=2.9,
        ymin=0,ymax=3,ytickmax=2.9,
        domain=.15:3,
        samples=100,
      ]
        \addplot[name path=A,brown, domain=2:3] {sqrt(x^2-4)};
        \addplot[name path=B,green,domain=1:3] {sqrt(x^2-1)};
        \addplot[name path=C,red]{1/x};
        \addplot[name path=D,blue]{2/x};

        \path[%draw,line width=3,orange,
          name path=AandC,
          intersection segments={
            of=A and C,
            sequence={R1 -- L2}
          }
        ];
        \path[%draw,line width=3,purple,
          name path=BandD,
          intersection segments={
            of=B and D,
            sequence={L1 -- R2}
          }
        ];
        \pgfonlayer{axis grid}
          \path [
            fill=orange!30,
            intersection segments={
              of=AandC and BandD,
              sequence={L2[reverse] -- R2}
            }
          ]--cycle;
        \endpgfonlayer

      \end{axis}
    \end{tikzpicture}
\end{document}

Result:

enter image description here

  • This is great. Thanks for updating your answer, it's nice to have two options. – CanofDrink Apr 5 '16 at 14:21

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.