[Tex/LaTex] Showing the area between two curves using rectangles (Riemann Sum)

pgfplotstikz-pgf

I've been looking for a solution to draw the area between two curves using infinitesimal rectangles, like the image below:

Image 1

It would be great also to use only one rectangle to show the difference between heights:

Image 2

Has anyone ever done something similar that I may use as a starting point?

Best Answer

Here's one option using pgfplots:

enter image description here

The code:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\pgfmathdeclarefunction{curvei}{1}{%
  \pgfmathparse{0.05*#1*(#1-7)*(#1-12)}%
}
\pgfmathdeclarefunction{curveii}{1}{%
  \pgfmathparse{-0.03*(#1)*(#1-6)*(#1-18)}%
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  xmin=-0.5,
  xmax=8,
  ymin=-5,
  ymax=7,
  domain=0.5:7.5,
  xtick={0.75,6.25},
  xticklabels={$a$,$b$},
  ytick={0},
  axis on top
]

\addplot[ybar, domain=1:6,samples=12, fill=blue!50!cyan,fill opacity=0.3,draw=cyan] 
  {curvei(x)};
\addplot[ybar, domain=1:6,samples=12, fill=blue!50!cyan,fill opacity=0.3, draw=cyan] 
  {curveii(x)};

\addplot[ycomb, domain=1:6,samples=12,densely dashed,draw=cyan!70!black] 
  {curvei(x)};
\addplot[ycomb, domain=1:6,samples=12,densely dashed,draw=cyan!70!black] 
  {curveii(x)};

\addplot[thick,no marks,red!70!black,samples=200]   
  {curvei(x)};
\addplot[thick,no marks,cyan,samples=200] 
  {curveii(x)};
\end{axis}
\end{tikzpicture}

\end{document}

And the second one:

enter image description here

The code:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{decorations.pathreplacing}
\pgfplotsset{compat=1.12}

\pgfmathdeclarefunction{curvei}{1}{%
  \pgfmathparse{0.05*#1*(#1-7)*(#1-12)}%
}
\pgfmathdeclarefunction{curveii}{1}{%
  \pgfmathparse{-0.03*(#1)*(#1-6)*(#1-18)}%
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  xmin=-0.5,
  xmax=8,
  ymin=-5.5,
  ymax=7,
  domain=0.5:7.5,
  xtick={0.75,6.25},
  xticklabels={$a$,$b$},
  ytick={0},
]

\addplot[thick,no marks,red!70!black,samples=200,name path=curveA]   
  {curvei(x)};
\addplot[thick,no marks,cyan,samples=200,name path=curveB] 
  {curveii(x)};
\addplot[cyan!60,fill opacity=0.4] 
  fill between[of=curveA and curveB,soft clip={domain=0.75:6.25}];

\addplot[name path=line,draw=none,forget plot] 
  coordinates {(3,-7) (3,7)};
\path[name intersections={of=curveA and line,by={top}}];  
\path[name intersections={of=curveB and line,by={bottom}}];

\filldraw[gray!100!cyan,opacity=0.5]
  ([xshift=-10pt]top) rectangle ([xshift=10pt]bottom);  
\draw[decorate,decoration=brace]
  ([xshift=12pt]top) -- node[right=4pt] {$y_{T} - y_{B}$} ([xshift=12pt]bottom);  
\draw[decorate,decoration={brace,raise=3pt}]
  ([xshift=10pt]bottom) -- node[below=4pt] {$\Delta x$} ([xshift=-10pt]bottom);  
\node[above,red!70!black] at (1.5,4.8) {$y_{T}$};
\node[above,cyan] at (1.5,-4.8) {$y_{B}$};
\end{axis}
\end{tikzpicture}

\end{document}
Related Question