# Graphics: Area between curves

I'm currently trying to figure out how to create certain graphics containing two curves and shaded the area between them:

with tikz and pgftools. In the first drawing the curves are: f(x)=1/2*x^2-2*x+5 and g(x)=-1/10*x^2+2 and a=1, b=4. In the second one the curves could be f(x)=-1/6*x^2+2, g(x)=1/4*x^2 with x_1=-2.19 and x_2=2.19.

I've read through several threads here but I can't seem to figure out what the code could be so I hope you can help me to deliver my course some nice graphics :)

With recent (v1.10) fillbetween pgfplotslibrary and taking a look at pgfplots documentation or suggested answers in comments, it's easy to draw something like

with next code:

\documentclass[border=3mm,tikz]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}

\begin{tikzpicture}
\begin{axis}[axis lines=middle,
xlabel=$x$,
ylabel=$y$,
enlargelimits,
ytick=\empty,
xtick={1,4},
xticklabels={a,b}]
\addplot[name path=F,blue,domain={-.2:5}] {0.5*x^2-2*x+5} node[pos=.8, above]{$f$};

\addplot[name path=G,green,domain={-.2:5}] {-0.1*x^2+2}node[pos=.1, below]{$g$};

\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=F and G, soft clip={domain=1:4}]
;
\node[coordinate,pin=30:{$A$}] at (axis cs:3.8,3){};

\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[axis lines=middle,
xlabel=$x$,
ylabel=$y$,
enlargelimits,
ytick=\empty,
xtick={-2.19,2.19},
xticklabels={$x_1$,$x_2$}]
\addplot[name path=F,blue,domain={-4:4}] {-(1/6)*x^2+2} node[pos=1, below]{$f$};

\addplot[name path=G,green,domain={-4:4}] {0.25*x^2}node[pos=1, above]{$g$};

\addplot[pattern=north west lines, pattern color=brown!50]fill between[of=F and G, soft clip={domain=-2.19:2.19}]
;
\node[coordinate,pin=60:{$A$}] at (axis cs:1.1,1.6){};

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


Just for typing exercise with PSTricks so I leave the code as is without optimization.

## One

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}
\def\f{x -.5 sub x 2.5 sub mul 2 div }
\def\g{\f neg }

\begin{document}
\begin{pspicture}[dimen=m](-2,-2)(4.5,4.5)
\psaxes[ticks=none,labels=none]{->}(0,0)(-2,-2)(4,4)[$x$,0][$y$,90]
\psxTick(-1){a}\psxTick(3){b}
\rput(1,.5){$A$}
\uput[45](!3 /x ED x \f 2 add){$f$}\uput[-45](!3 /x ED x \g 1 sub){$g$}
\end{pspicture}
\end{document}


## Two

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}

\def\f{x -.5 sub x 2.5 sub mul 2 div }
\def\g{\f neg }

\begin{document}
\begin{pspicture}[dimen=m,saveNodeCoors,hatchsep=2pt](-2,-2)(4.5,4.5)
\bgroup
\psset{PointName=none,PointSymbol=none}
\pstInterFF{\g .5 sub}{0}{0}{A}
\pstInterFF{\g .5 sub}{0}{2}{B}
\egroup
\pscustom[fillstyle=vlines,linestyle=none,hatchcolor=red]{\psplot{N-A.x}{N-B.x}{\g .5 sub}\psplot{N-B.x}{N-A.x}{0}}
\psaxes[ticks=none,labels=none]{->}(0,0)(-2,-2)(4,4)[$x$,0][$y$,90]
\psxTick(-1){a}\psxTick(3){b}
\rput(1,1.25){$A$}
\uput[45](!3 /x ED x \f 3 add){$f$}\uput[-45](!3 /x ED x \g .5 sub){$g$}
\end{pspicture}
\end{document}


## Three

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}

\def\f{x -.5 sub x 2.5 sub mul 2 div }
\def\g{\f neg }

\begin{document}
\begin{pspicture}[dimen=m,saveNodeCoors,hatchsep=2pt](-2,-2)(4.5,4.5)
\bgroup
\psset{PointName=none,PointSymbol=none}
\egroup
\psaxes[ticks=none,labels=none]{->}(0,0)(-2,-2)(4,4)[$x$,0][$y$,90]
\psxTick(-1){a}\psxTick(3){b}
\rput(!N-A.x N-B.x add 2 div .6){$A$}
\uput[45](!3 /x ED x \f 1.2 add){$f$}\uput[-45](!3 /x ED x \g){$g$}
\end{pspicture}
\end{document}


## Latest update

If you want to use infix notation instead of postfix but at the same time you don't want to use algebraic option for unknown reason, consider the following. I just used the first case above as an example. The second and third cases are left for your exercises.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}
\def\f{((x-.5)*(x-2.5)/5)}
\def\g{(-\f)}

\pstVerb{/I2P {exec AlgParser cvx exec} def}
\begin{document}
\begin{pspicture}[dimen=m](-2,-2)(4.5,4.5)
\psaxes[ticks=none,labels=none]{->}(0,0)(-2,-2)(4,4)[$x$,0][$y$,90]
\pscustom[fillstyle=vlines,linestyle=none,hatchcolor=gray]{\psplot{-1}{3}{{(\f+2)} I2P}\psplot{3}{-1}{{(\g-1)} I2P}}
\psplot{-1}{3}{{(\f+2)} I2P}\psplot{3}{-1}{{(\g-1)} I2P}
\psxTick(-1){a}\psxTick(3){b}
\rput*(*1 .5){$A$}
\uput[45](*3 {\f+2}){$f$}\uput[-45](*3 {\g-1}){$g$}
\end{pspicture}
\end{document}


For the fun with MetaPost, making use of a macro of my own based upon the very handy buildcycle macro, in order to create the path bounding the area between the two curves.

Applied here on the OP's second example, which is a bit of particular case, since the two curves intersect. If I have more time this evening, I'll add a more general application.

input latexmp;
setupLaTeXMP(options="12pt", textextlabel=enable, mode = rerun);

vardef graph_of_function (suffix f) (expr xmin, xmax, xsep) =
for x = xmin step xsep until xmax: (x, f(x)) .. endfor (xmax, f(xmax))
enddef ;

vardef vline (suffix f, g) (expr x) = (x, min(f(x), g(x))) -- (x, max(f(x),g(x))) enddef;

vardef area_between_functions (suffix f, g)(expr a, b, xsep) =
buildcycle(graph_of_function(f, a, b, xsep), vline(f, g, b),
reverse graph_of_function(g, a, b, xsep), reverse vline(f, g, a))
enddef;

vardef xaxis (expr xmin, xmax) = (xmin, 0) -- (xmax, 0) enddef ;
vardef yaxis (expr ymin, ymax) = (0, ymin) -- (0, ymax) enddef ;

beginfig(1);
xmin:=-4.5; xmax := 4.5;
ymin := -1.5; ymax := 5;
u:=1cm;

vardef f(expr x)= (-1/6)*(x**2)+2 enddef;
vardef g(expr x)= 0.25*(x**2) enddef;
path C_f, C_g;
C_f = graph_of_function(f,xmin, xmax, .1);
C_g = graph_of_function(g, xmin, xmax, .1);

a := xpart(C_f intersectionpoint C_g); %a \approx -2.19;
b := xpart(reverse C_f intersectionpoint reverse C_g); %b \approx 2.19;

fill (area_between_functions(f, g, a, b, 0.1)) scaled u withcolor 0.8white;
draw C_f scaled u;
draw C_g scaled u;
draw ((a, 0)*u -- (a, g(a))*u) dashed evenly;
draw ((b, 0)*u -- (b, g(b))*u) dashed evenly;

drawarrow xaxis(xmin, xmax) scaled u ;
drawarrow yaxis(ymin, ymax) scaled u ;
label.bot("$a \approx" & decimal((round(100*a))/100) & "$", (a*u, 0));
label.bot("$b \approx" & decimal((round(100*b))/100) & "$", (b*u, 0));
label.bot("$x$", (xmax*u, 0));
label.lft("$y$", (0, ymax*u));
label.rt("$f$", (xmax, f(xmax))*u);
label.rt("$g$", (xmax, g(xmax))*u);
endfig;
end.


\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\def\f{(x-.5)*(x-2.5)/5+2}
\def\g{-\f-3}

\begin{document}
\begin{pspicture}[algebraic](-2,-2)(4.5,4.5)
\psaxes[ticks=none,labels=none]{->}(0,0)(-2,-2)(4,4)[$x$,0][$y$,90]
\pscustom[fillstyle=crosshatch,linestyle=none,hatchcolor=red!50]{
\psplot{-1}{3}{\f}\psplot{3}{-1}{\g}}
\psplot{-1}{3}{\f}\psplot{3}{-1}{\g}
\psxTick(-1){a}\psxTick(3){b}
\rput*(1,.5){$A$}
\uput[45](*3 {\f}){$f$}\uput[-45](*3 {\g}){$g$}
\end{pspicture}
\end{document}