Does anyone know where I can find a template that will show Riemann sums converging to the definite integral, dynamically for a beamer presentation?
Asked
Active
Viewed 3,950 times
16
Svend Tveskæg
- 31,033
rjalli
- 191
2 Answers
19
With PSTricks.
GIF version:

\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:

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}
kiss my armpit
- 36,086
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

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.

-
3
-
-
Oh my ghost. I meant that floating does not make sense in a frame. Try adding
\lipsum[1-20]before thefigureand adding[allowframebreaks]to theframe; 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-escapein\immediate\write18{pdflatex -shell-escape tikzintegral}because there is no external application invocation intikzintegral.tex. :-) – kiss my armpit Mar 31 '13 at 07:22 -
One more: "Starred" version of
filecontentsenvironment that you placed above\documentclassis useless because it is the same as the unstarred one. As a result, it will not override the existingtikzintegral.texif you make changes in the body offilecontents*. If you want the changes to override the existing file, then you have to move the starredfilecontentsenvironment to the preamble and you have to loadfilecontentspackage explicitly. – kiss my armpit Mar 31 '13 at 08:48 -
-
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
integral.pdf, you can add\makeatletter\@for\x:={tex,dvi,ps,log,aux}\do{\immediate\write18{cmd /c del integral.\x}}\makeatotherbelow the comment% do cleaning here if necessary!. – kiss my armpit Mar 31 '13 at 07:37