PGF 数学在 \foreach 循环中中断

PGF 数学在 \foreach 循环中中断

跟随我的上一个问题我试图使我的例子更加通用和复杂。(注意:我并不想让它更复杂,但这就是我希望我的图形所做的事情。)

当我有以下内容时,我收到错误:

NOTE: coordinate (2Y1.2575e-1],3Y0.0e0]) has been dropped because it is unbounded (in y). (see also unbounded coords=jump).
NOTE: coordinate (2Y7.55e-2],3Y0.0e0]) has been dropped because it is unbounded (in y). (see also unbounded coords=jump).
NOTE: coordinate (2Y2.525e-2],3Y0.0e0]) has been dropped because it is unboundd (in y). (see also unbounded coords=jump).
! Undefined control sequence.
<argument> \Xmid
                 , XS{\Xmid }
l.38     \end{axis}

?

有问题的部分是一条\foreach语句;如果我注释掉\foreach如下所示的内容,一切都会编译正常。不幸的是,我需要这条\foreach语句来完成我的图形。

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}

\begin{frame}[fragile]{Frame title}
  \tikzset{declare function={XS(\x)=1/sqrt(\x);}}
  \begin{tikzpicture}
      \begin{axis}[
        ticks = none,
        axis x line = bottom,
        axis y line = left,
        xmin = -0.5,
        ymin = 0.0,
        ymax = 5.0,
      ]
      \addplot[ black, samples=200] {XS(x)};

      \xdef\Xmin{0.1}
      \xdef\Xmax{4.0}

      \pgfmathsetmacro\startIndex{3}
      % \foreach \n in {0,...,4}{
      %   \pgfmathsetmacro{\onlyIndex}{int(\n+\startIndex)}
        \pgfmathsetmacro{\Xmid}{(\Xmax-\Xmin)/2}

        % \only<\onlyIndex->{
        \only<3->{
          \addplot[only marks, color=black, fill=white, samples at={\Xmid}]{XS(x)};
          \draw[thick, dashed, color=red] (\Xmid, XS{\Xmid}) -- (\Xmax, XS{\Xmax});
        }

        \xdef\Xmax{\Xmid}
      % }

      \end{axis}
  \end{tikzpicture}
\end{frame}
\end{document}

我的循环出了什么问题\foreach,导致了问题\Xmid

我试图在曲线上最左边的两个点之间添加点,并在它们之间画一条线。我试图创建一个动画来说明如何创建曲线的线性近似。最左边的两个点在每次迭代时都会发生变化(当然)。这是我的上一个问题

答案1

存在一些问题。

  1. 最好将情节限制在一个范围内以关闭警告。

  2. 使用{XS(\Xmid)}而不是XS{\Xmid}XS是声明的函数,并且评估函数的正确语法使用普通括号。结果周围的花括号使 TiZ 评价该事物。

  3. 如果您想要一个整数索引,请使用\pgfmathtruncatemacro。(这实际上不是问题。)

  4. 我补充道\xdef\Xmid{\Xmid} 循环,使得它是全局的并且在循环内部已知。

我还在 处添加了一个标记\Xmax

\documentclass{beamer}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}

\begin{frame}[fragile]{Frame title}
  \tikzset{declare function={XS(\x)=1/sqrt(\x);}}
  \begin{tikzpicture}
      \begin{axis}[
        ticks = none,
        axis x line = bottom,
        axis y line = left,
        xmin = -0.5,
        ymin = 0.0,
        ymax = 5.0,
      ]
      \addplot[ black, samples=200,domain={1/25}:5] {XS(x)};

      \xdef\Xmin{0.04}
      \xdef\Xmax{4.0}

      \pgfmathtruncatemacro\startIndex{3}
      \only<\startIndex->{
      \addplot[only marks, color=black, fill=white, samples at={\Xmax},
          domain={1/25}:5,clip=false]{XS(x)};
      }
      \foreach \n in {0,...,6}{
        \pgfmathtruncatemacro{\onlyIndex}{int(\n+\startIndex)}
        \pgfmathsetmacro{\Xmid}{\Xmin+(\Xmax-\Xmin)/2}
        \xdef\Xmid{\Xmid}
         \only<\onlyIndex->{
          \addplot[only marks, color=black, fill=white, samples at={\Xmid},
          domain={1/25}:5,clip=false]{XS(x)};
          \addplot[thick, dashed, color=red, samples at={\Xmid,\Xmax}]{XS(x)};
       }
        \xdef\Xmax{\Xmid}
       }

      \end{axis}
  \end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

相关内容