4

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?

  • @JPi ok, that's good. Do you want to contribute to the answer? – bru1987 Oct 11 '15 at 15:45
  • 1
    Starting point: Research through tex.stackexchange.com/search?q=riemann – Ignasi Oct 11 '15 at 15:49
  • Can be done with GeoGebra and exporting to TikZ as well – user31729 Oct 11 '15 at 15:59
12

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}
  • @bru1987 You're welcome. I updated my answer with some improvements that night be of interest for you. – Gonzalo Medina Oct 11 '15 at 20:00
  • thank you Gonzalo, I'm always impressed by your great LaTeX skills, specially when working on Tikz and pgfplots. Have a great day, Best Regards. – bru1987 Oct 12 '15 at 10:39

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.