3

I´m trying to create an animation of rectangles approximating the area below a curve (x^2). I´ve tried to use a counter to specify the lenght of the rectangles but Latex won`t let me use \divide or \multiply on counters in \foreach.

Is there a way to do something like

\frac{1}{\then}

to use in

\foreach \x in {0,1\divide\then,...,1}

What I´ve tried so far:

\documentclass[10pt]{scrartcl}
%%%


\usepackage{geometry}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{color}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{animate}
\usetikzlibrary{shapes, arrows, shadows,trees, decorations.markings, 
positioning, patterns, plotmarks,         
matrix,decorations.pathmorphing,backgrounds,fit,shapes.symbols,chains, 3D, 
calc}


\geometry{a4paper,left=20mm,right=20mm, top=15mm, bottom=5mm }

\definecolor{mred}{rgb}{0.8,0,0}

 %  
 %
\newcounter{n}
\setcounter{n}{01}

%

\begin{document}

\begin{center}
\begin{animateinline}[loop,poster = first, controls]{1}
\whiledo{\then < 4}{
\begin{tikzpicture}[scale=6]
                \draw[->] (-0.1,0) -- (1,0)node[above right]{$x$};
                \draw[->] (0,-0.1) -- (0,1)node[above right]{$y$};
                \draw[style=help lines, step=0.2] (-0.01,-0.01) grid (1,1);
                \draw[domain=-0.1:1, smooth, very thick] plot(\x,{\x*\x});
                \foreach \x/\xtext in {0.2,0.4,0.6/0.6,0.8/0.8,1/1}
                                            \draw[shift={(\x,0)},scale=1/6] (0pt,2pt) -- (0pt,-2pt)node[below] {\small{$\xtext$}};
                \foreach \y/\ytext in {0.2,0.4,0.6/0.6,0.8/0.8,1/1}
                                            \draw[shift={(0,\y)},scale=1/6] (2pt,0pt) -- (-2pt,0pt)node[left] {\small{$\ytext$}};

                \foreach \x in {0,0.\then,...,1} 
                                        \draw[thick, mred] (\x,0) rectangle (\x+1\divide\then,\x^2);        
                \node[below] at (0.5,-0.1) {$n=$\then};                 
            \end{tikzpicture}   
\stepcounter{n}
\ifthenelse{\then < 4}{\newframe}
{\end{animateinline}\relax
}}
\end{center}

\end{document}
3

This is an animation solution, approximating the area under the curve. Please cut and paste the code and compile to show that approximation. animate package is required.

First, a App macro taking 2 arguments is defined. #1=number of frame and the last frame is also the total frame of \multiframe{total frame}. As to #2, it is designed such that 1/#2 is the horizontal/base length of approximating rectangles. For example, #2=2 means the rectangle length=0.5cm, so #2=32 would means the length is 0.03125cm.

enter image description here

enter image description here Code

\documentclass[10pt]{scrartcl}
%%%
\usepackage{geometry}
%\usepackage[T1]{fontenc}
%\usepackage{amsmath}
\usepackage{color}
%\usepackage{pgfplots}
\usepackage{tikz}
%\usepackage{ifthen}
\usepackage{animate}
%\usetikzlibrary{shapes, arrows, shadows,trees, decorations.markings, 
%positioning, patterns, plotmarks,         
%matrix,decorations.pathmorphing,backgrounds,fit,shapes.symbols,chains, 3D, 
%calc}

\geometry{a4paper,left=20mm,right=20mm, top=15mm, bottom=5mm }

\definecolor{mred}{rgb}{0.8,0,0}

\newcounter{n}
\setcounter{n}{01}
%
\newcommand{\App}[2]{
\whiledo{\value{n}=#1 \and \icount=#1}{
 \stepcounter{n}
 \foreach \y in {#2}{
 \pgfmathsetmacro{\step}{1/\y}
 \pgfmathsetmacro{\evalue}{1-\step}
      \foreach \x in {0,\step,...,\evalue}{
      \draw[thick, mred] (\x,0) rectangle (\x+\step,\x^2);        
      \node[below] at (0.5,-0.1) {$n=\step$};}}} 
}

\begin{document}

\begin{center}
\begin{animateinline}[loop,poster = first, controls]{1}
\multiframe{6}{icount=1+1}
{
\begin{tikzpicture}[scale=6]
      \draw[->] (-0.1,0) -- (1,0)node[above right]{$x$};
      \draw[->] (0,-0.1) -- (0,1)node[above right]{$y$};
      \draw[style=help lines, step=0.2] (-0.01,-0.01) grid (1,1);
      \draw[domain=-0.1:1, smooth, very thick] plot(\x,{\x*\x});
      \foreach \x/\xtext in {0.2,0.4,0.6/0.6,0.8/0.8,1/1}{
      \draw[shift={(\x,0)},scale=1/6] (0pt,2pt) -- (0pt,-2pt)node[below] {\small{$\xtext$}};}
      \foreach \y/\ytext in {0.2,0.4,0.6/0.6,0.8/0.8,1/1}{
      \draw[shift={(0,\y)},scale=1/6] (2pt,0pt) -- (-2pt,0pt)node[left] {\small{$\ytext$}};}

     \App{1}{2}
     \App{2}{4}
     \App{3}{5}
     \App{4}{8}
     \App{5}{16}
     \App{6}{32}   % multiframe=6
\end{tikzpicture}  
}
\end{animateinline}
\end{center}
\end{document}
2

The key problem is that you are missing the \xtext and \ytext parts in the main \foreach cycles. I have extended those parts from 0.2,0.4 to 0.2/0.2,0.4/0.4. In this case, you could use only one variable per cycle as \xtext and \ytext only duplicate their \x and \y counterparts.

The next problem is the use of \divide itself. We use regular slash for division (/). Well, I believe that there should probably be rather \x+0.\then instead of \x+0/\then. Please, you decide that!

I enclose an example of how far I have got in your source code and two previews of the animation.

%! *latex tikz-animate.tex
\documentclass[10pt]{scrartcl}
\usepackage{geometry}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{color}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{animate}
\usetikzlibrary{shapes, arrows, shadows,trees, decorations.markings, 
  positioning, patterns, plotmarks, matrix, decorations.pathmorphing,
  backgrounds,fit,shapes.symbols,chains, 3D, calc}
\usetikzlibrary{external} \tikzexternalize
\geometry{a4paper,left=20mm,right=20mm, top=15mm, bottom=5mm }
\definecolor{mred}{rgb}{0.8,0,0}
\newcounter{n}
\setcounter{n}{01}

\begin{document}
\begin{center}
\begin{animateinline}[loop, poster = first, controls]{1}
\whiledo{\then < 4}{
\begin{tikzpicture}[scale=6]
                \draw[->] (-0.1,0) -- (1,0)node[above right]{$x$};
                \draw[->] (0,-0.1) -- (0,1)node[above right]{$y$};
                \draw[style=help lines, step=0.2] (-0.01,-0.01) grid (1,1);
                \draw[domain=-0.1:1, smooth, very thick] plot(\x,{\x*\x});
                \foreach \x/\xtext in {0.2/0.2,0.4/0.4,0.6/0.6,0.8/0.8,1/1}
                                            \draw[shift={(\x,0)}, scale=1/6] (0pt,2pt) -- (0pt,-2pt) node [below] {\small{$\xtext$}};
                \foreach \y/\ytext in {0.2/0.2,0.4/0.4,0.6/0.6,0.8/0.8,1/1}
                                            \draw[shift={(0,\y)}, scale=1/6] (2pt,0pt) -- (-2pt,0pt) node [left] {\small{$\ytext$}};
                \foreach \x in {0,0.\then,...,1} 
                                        \draw[thick, mred] (\x,0) rectangle (\x+0.\then, \x^2); % or 0/\then?
                \node[below] at (0.5,-0.1) {$n=$\then};                 
            \end{tikzpicture}   
\stepcounter{n}
\ifthenelse{\then < 4}{\newframe}
{\end{animateinline}\relax}
}% End of \whiledo...
\end{center}
\end{document}

mwe 1 mwe 2

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.