16

Does anyone know where I can find a template that will show Riemann sums converging to the definite integral, dynamically for a beamer presentation?

rjalli
  • 191

2 Answers2

19

With PSTricks.

GIF version:

enter image description here

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pstricks-add}

\def\f(#1){(#1+1)*(#1-1)*(#1-3.5)}

\psset{algebraic,plotpoints=100}

\begin{document}
\multido{\i=1+1}{20}{%
\begin{pspicture}(-1.5,-5.5)(4.5,4.5)
    \psStep[linewidth=0.4pt,linecolor=gray,fillstyle=solid,fillcolor=orange](-0.5,3.0){\i}{\f(x)}
    \psplot[linecolor=blue]{-1.0}{3.5}{\f(x)}
    \psaxes[linecolor=gray]{->}(0,0)(-1.5,-5.5)(4,4)[$x$,0][$y$,90]
\end{pspicture}}
\end{document}

Beamer version:

enter image description here

Read the given comments at the top!

% please compile it with pdflatex -shell-escape main.tex
% Filename for this code is main.tex
\documentclass{beamer}
\usepackage{filecontents}
\begin{filecontents*}{integral.tex}
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pstricks-add}

\def\f(#1){(#1+1)*(#1-1)*(#1-3.5)}

\psset{algebraic,plotpoints=100}

\begin{document}
\multido{\i=1+1}{20}{%
\begin{pspicture}(-1.5,-5.5)(4.5,4.5)
    \psStep[linewidth=0.4pt,linecolor=gray,fillstyle=solid,fillcolor=orange](-0.5,3.0){\i}{\f(x)}
    \psplot[linecolor=blue]{-1.0}{3.5}{\f(x)}
    \psaxes[linecolor=gray]{->}(0,0)(-1.5,-5.5)(4,4)[$x$,0][$y$,90]
\end{pspicture}}
\end{document}
\end{filecontents*}

\immediate\write18{latex integral}
\immediate\write18{dvips integral}
\immediate\write18{ps2pdf integral.ps}
% do cleaning here if necessary!

\usepackage{animate}
\begin{document}
\begin{frame}{Definite Integral}
\begin{center}
    \animategraphics[controls,loop,autoplay,scale=0.5]{5}{integral}{}{}
\end{center}
\end{frame}
\end{document}
  • If you are using Windows operating system and you want to clean the auxiliary files generated for integral.pdf, you can add \makeatletter\@for\x:={tex,dvi,ps,log,aux}\do{\immediate\write18{cmd /c del integral.\x}}\makeatother below the comment % do cleaning here if necessary!. – kiss my armpit Mar 31 '13 at 07:37
18

Let me borrow Karl's Students's idea.

Here is a tkz-fct demonstration. I borrowed the integration from Alain Matthes' answer in Tikz-PGF: Draw integral test plot. For this to work, you should have gnuplot installed and compile with pdflatex with -shell-escape enabled.

The .gif version

enter image description here

To create the .gif, you should install imagemagick first. After compiling

pdflatex -shell-escape tikzintegral

run

convert -delay 1 -loop 0 tikzintegral.pdf tikzintegral.gif

tikzintegral.tex:

\documentclass[tikz]{standalone}
\usepackage{tkz-fct}   
\usepackage{multido}

\begin{document}
\multido{\i=2+1}{20}{%
\begin{tikzpicture}[scale=1.25]
\tkzInit[xmax=8,ymax=4]
\tkzAxeXY[ticks=false]
\tkzGrid   
\tkzFct[color = red, domain =0.125:8]{4./x}
\tkzDrawRiemannSumInf[fill=green!60,
                     opacity=.2,
                     color=green,
                     line width=1pt,
                     interval=1:8,
                     number=\i]
 %\foreach \x/\t in {1.5/$a_1$,2.5/$a_2$,3.5/$a_3$,7.5/$a_7$}
 %\node[green!50!black] at (\x,{4/(\x+1)-0.25}){\t};
\end{tikzpicture}}
\end{document} 

For the beamer presentation

\documentclass{beamer}
\usetheme{Warsaw}
\usepackage{filecontents}

\begin{filecontents*}{tikzintegral.tex}
\documentclass[tikz]{standalone}
\usepackage{tkz-fct}   
\usepackage{multido}
\begin{document}
\multido{\i=2+1}{20}{%
\begin{tikzpicture}[scale=1.25]
\tkzInit[xmax=8,ymax=4]
\tkzAxeXY[ticks=false]
\tkzGrid   
\tkzFct[color = red, domain =0.125:8]{4./x}
\tkzDrawRiemannSumInf[fill=green!60,
                     opacity=.2,
                     color=green,
                     line width=1pt,
                     interval=1:8,
                     number=\i]
\end{tikzpicture}}
\end{document}
\end{filecontents*}

\immediate\write18{pdflatex --shell-escape tikzintegral}

\usepackage{animate}

\begin{document}
\begin{frame}{Riemann Sum}
    \begin{center}
  \animategraphics[controls,loop,autoplay,scale=0.5]{5}{tikzintegral}{}{}
    \end{center}
\end{frame}
\end{document}

Note here that since you have compiled tikzintegral.tex earlier, if you are saving these codes in the same directory, you can just write:

\documentclass{beamer}
\usetheme{Warsaw}

\usepackage{animate}

\begin{document}
\begin{frame}{Riemann Sum}
    \begin{center}
        \animategraphics[controls,loop,autoplay,scale=0.5]{5}{tikzintegral}{}{}
    \end{center}
\end{frame}
\end{document}

Here is how the first page will look like.

enter image description here

hpesoj626
  • 17,282
  • 3
    Doesn't it make sense to use figure in a frame? – kiss my armpit Mar 31 '13 at 06:49
  • @Karl'sstudents I am used to putting \caption every time. :) – hpesoj626 Mar 31 '13 at 07:00
  • Oh my ghost. I meant that floating does not make sense in a frame. Try adding \lipsum[1-20] before the figure and adding [allowframebreaks] to the frame; you will see that floating is not useful. – kiss my armpit Mar 31 '13 at 07:11
  • Hmmm... Yes. You are right. I learned something new today, thanks to you. – hpesoj626 Mar 31 '13 at 07:12
  • One more: You don't need -shell-escape in \immediate\write18{pdflatex -shell-escape tikzintegral} because there is no external application invocation in tikzintegral.tex. :-) – kiss my armpit Mar 31 '13 at 07:22
  • One more: "Starred" version of filecontents environment that you placed above \documentclass is useless because it is the same as the unstarred one. As a result, it will not override the existing tikzintegral.tex if you make changes in the body of filecontents*. If you want the changes to override the existing file, then you have to move the starred filecontents environment to the preamble and you have to load filecontents package explicitly. – kiss my armpit Mar 31 '13 at 08:48
  • @Karl'sstudents Wow. Thanks. I learned a lot from you today. – hpesoj626 Mar 31 '13 at 08:58
  • You can save one byte more by removing one - (either the first or the second one) of -- in --shell-escape. :-) – kiss my armpit Apr 12 '13 at 05:58