4

How do I shade the region bound by the standard parabola and the square root function?

\documentclass{amsart}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{calc,intersections}



\usepackage{pgfplots}
\pgfplotsset{compat=1.11}



\begin{document}

\raisebox{0mm}[0mm][0mm]
{
\begin{tikzpicture}
\begin{axis}[width=2.25in, height=2.25in, axis equal image, axis on top, clip=false,
    axis lines=middle,
    xmin=-2,xmax=4, domain=-2:4,
    ymin=-0.75,ymax=4,
    restrict y to domain=-0.75:4,
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=\textit{x},ylabel=\textit{y},
    axis line style={shorten >=-12.5pt, shorten <=-12.5pt},
    xlabel style={at={(ticklabel* cs:1)}, xshift=12.5pt, anchor=north west},
    ylabel style={at={(ticklabel* cs:1)}, yshift=12.5pt, anchor=south west}
]

%The graphs of the square root function and the standard parabola are plotted.
\addplot[samples=501, domain=-2:2] {x^2};
\addplot[samples=501, domain=0:4] {sqrt(x)};


%Coordinate A is the point of tangency to the graph of the standard parabola, and B is the
%x-intercept of the tangent line.
\coordinate (A) at (-15/8,225/64);
\coordinate (B) at (-15/16,0);

%Coordinate P is the point of tangency to the graph of the square root function, and Q is
%the y-intercept of the tangent line.
\coordinate (P) at (15/4,{sqrt(15/4)});
\coordinate (Q) at (0,{1/4*sqrt(15)});

\end{axis}


%A "pin" is drawn to A.
\draw[draw=gray, shorten <=1mm, shorten >=1mm] (A) -- ($(A)!0.5cm!-90:(B)$) node[anchor=west, inner sep=0,  font=\scriptsize]{\makebox[0pt][r]{$y=x^{2}$}};

%A "pin" is drawn to P.
\draw[draw=gray, shorten <=1mm, shorten >=1mm] (P) -- ($(P)!0.5cm!90:(Q)$);
\node[anchor=north, inner sep=0, outer sep=0, font=\scriptsize] at ($(P)!0.4cm!90:(Q)$){\makebox[0pt][l]{$y=\sqrt{x}$}};

\end{tikzpicture}
}

\end{document}
  • 1
    This is the code for a typical plot seen in a Calculus course. – user143462 Nov 29 '18 at 19:28
6

That's a standard task for the fillbetween library.

\documentclass{amsart}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{calc}



\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{fillbetween}


\begin{document}

\raisebox{0mm}[0mm][0mm]
{
\begin{tikzpicture}
\begin{axis}[width=2.25in, height=2.25in, axis equal image, axis on top, clip=false,
    axis lines=middle,
    xmin=-2,xmax=4, domain=-2:4,
    ymin=-0.75,ymax=4,
    restrict y to domain=-0.75:4,
    xtick={\empty},ytick={\empty},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=\textit{x},ylabel=\textit{y},
    axis line style={shorten >=-12.5pt, shorten <=-12.5pt},
    xlabel style={at={(ticklabel* cs:1)}, xshift=12.5pt, anchor=north west},
    ylabel style={at={(ticklabel* cs:1)}, yshift=12.5pt, anchor=south west}
]

%The graphs of the square root function and the standard parabola are plotted.
\addplot[samples=501, domain=-2:2,name path=parabola] {x^2};
\addplot[samples=501, domain=0:4,name path=root] {sqrt(x)};

\addplot[red!10] fill between [of=parabola and root, soft clip={domain=0:1}];
%Coordinate A is the point of tangency to the graph of the standard parabola, and B is the
%x-intercept of the tangent line.
\coordinate (A) at (-15/8,225/64);
\coordinate (B) at (-15/16,0);

%Coordinate P is the point of tangency to the graph of the square root function, and Q is
%the y-intercept of the tangent line.
\coordinate (P) at (15/4,{sqrt(15/4)});
\coordinate (Q) at (0,{1/4*sqrt(15)});

\end{axis}


%A "pin" is drawn to A.
\draw[draw=gray, shorten <=1mm, shorten >=1mm] (A) -- ($(A)!0.5cm!-90:(B)$) node[anchor=west, inner sep=0,  font=\scriptsize]{\makebox[0pt][r]{$y=x^{2}$}};

%A "pin" is drawn to P.
\draw[draw=gray, shorten <=1mm, shorten >=1mm] (P) -- ($(P)!0.5cm!90:(Q)$);
\node[anchor=north, inner sep=0, outer sep=0, font=\scriptsize] at ($(P)!0.4cm!90:(Q)$){\makebox[0pt][l]{$y=\sqrt{x}$}};

\end{tikzpicture}
}

\end{document}

enter image description here

  • Please see the response that I posted. – Adelyn Nov 30 '18 at 0:05
  • @Adelyn I just looked at your follow-up question. As far as I can see, the shading does appear there. Is that correct? (As far as I can see the main issue is that the dimension of your left minipage is to generous, so you may just reduce 6in to 3.5in unless you want to use other methods such as wrapfig.) – user121799 Nov 30 '18 at 1:17
  • The shading is not there. I replaced red!10 with black and nothing happens - the region between the two graphs is not shaded. Do I have to manually download a package onto the hard drive? – Adelyn Dec 7 '18 at 17:44
  • Regarding the follow-up question, I would prefer not to change the margins for any minipage environment. What would you suggest for a wrapfig? – Adelyn Dec 7 '18 at 17:47
  • @Adelyn I am sorry, this is the output I am getting on my machine. And I have no idea why it is not there on your machine. And as for your follow-up question, I guess it is better if you place requests there. If you do not accept this answer because it does not give you the desired output, that's perfectly fine. But I cannot really make statements on answers that I did not write. – user121799 Dec 7 '18 at 19: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.