在 foreach 中使用计数器,动画

在 foreach 中使用计数器,动画

我正在尝试创建一个矩形动画,该动画近似于曲线下方的面积 (x^2)。我尝试使用计数器来指定矩形的长度,但 Latex 不允许我在 \foreach 中的计数器上使用 \divide 或 \multiply。

有没有办法做类似的事情

\frac{1}{\then}

使用

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

到目前为止我已经尝试过:

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

答案1

这是一个动画解决方案,用于近似计算曲线下的面积。请剪切并粘贴代码并进行编译以显示近似值。animate需要包。

首先,App定义一个带 2 个参数的宏。#1=帧数,最后一帧也是 的总帧数\multiframe{total frame}。至于#2,它被设计为 1/#2 是近似矩形的水平/底边长度。例如,#2=2 表示矩形长度=0.5cm,因此#2=32 表示长度为 0.03125cm。

在此处输入图片描述

在此处输入图片描述 代码

\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

关键问题是主循环中缺少\xtext和部分。我已将这些部分从扩展到。在这种情况下,每个循环只能使用一个变量作为,并且只复制它们的和对应项。\ytext\foreach0.2,0.40.2/0.2,0.4/0.4\xtext\ytext\x\y

下一个问题是如何使用\divide它本身。我们使用常规斜线来表示除法 ( /)。好吧,我认为应该用 Rather\x+0.\then来代替\x+0/\then。请您决定!

我附上了一个例子,说明我在您的源代码中取得了多大的进展以及两个动画预览。

%! *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}

1 2

相关内容