Rompiendo un bucle en Tikz

Rompiendo un bucle en Tikz
\documentclass{article}
\usepackage{tikz}

\begin{document}


 \foreach \N in {8}
 {\begin{tikzpicture}[scale=1.2,declare function={f(\x)=((1/3)*(\x)^(3)-3* 
            (\x)^(2)+8*\x-3;},
       lnode/.style={fill=white,font=\normalsize,inner sep=0pt,text height=1.5em}]
   \pgfmathtruncatemacro{\M}{1}
    \coordinate (start) at (.8,{f(.8)});
    \foreach \X [remember=\X as \LastX (initially 0)] in {1,2,3,4,...,8}
     \ifnum\X<5
     {\draw[fill=orange!40!white] (1+\LastX*4/\N,0) rectangle (1+\X*4/\N,{f(1+\LastX*4/\N)});
    \draw[red,fill=red] (1+\LastX*4/\N,{f(1+\LastX*4/\N)}) circle (1.4pt) ;
   \path  (1+\LastX*4/\N,0pt) coordinate (x\X);
    \ifnum\X=1
   \draw (1+\LastX*4/\N,3pt) -- (1+\LastX*4/\N,0pt) coordinate (x\X)
   node[anchor=north east,xshift=2pt,lnode]  {$a=x_{\X}$};
  \else
   \pgfmathtruncatemacro{\itest}{mod(\X,\M)}
   \ifnum\itest=0
      \pgfmathsetmacro{\dist}{4-\LastX*4/\N}
      \ifdim\dist cm>5pt
        \draw (1+\LastX*4/\N,3pt) -- (1+\LastX*4/\N,0pt)
         node[anchor=north,lnode] {$x_{\X}$}; 
                \else
        \ifnum\X=8
            \fi
          \fi  
        \fi
      \fi
    \fi
   }
 \draw (5,3pt) -- (5,0pt)
 node[anchor=north west,xshift=-2pt,lnode]{$b$};
 %\draw (1,3pt) -- (1,0pt)
 % node[anchor=north west,xshift=-2pt,lnode]{$a$};
 \node at (5.3,5) [text=red] {$f(x)$};
 \coordinate (end) at (5.05,{f(5.05)});
 \draw (5,3pt) -- (5,0pt) node[anchor=north west,xshift=-2pt,lnode]{$b$};
 \draw (5,0)--(5,{f(5)});
 \draw [-latex] (-0.5,0) -- (6,0) node (xaxis) [below] {$x$};
 \draw [-latex] (0,-0.5) -- (0,5) node [left] {$y$};            
 \draw[domain=.5:5.3,samples=200,variable=\x,red,<->,thick] plot ({\x},{f(\x)});
\node at (5.3,5) [text=red] {$f(x)$};             
\end{tikzpicture}}

\end{document}

Esto produce un error pero genera:

ingrese la descripción de la imagen aquí

Estoy intentando utilizar un programa existente escrito por Marmot para crear

ingrese la descripción de la imagen aquí

Respuesta1

¿Quieres algo como esto?

\documentclass{article}
\usepackage{tikz}

\begin{document}
 \foreach \N in {8}
 {\begin{tikzpicture}[scale=1.2,declare function={f(\x)=((1/3)*(\x)^(3)-3* 
            (\x)^(2)+8*\x-3;},
       lnode/.style={fill=white,font=\normalsize,inner sep=0pt,text height=1.5em}]
   \pgfmathtruncatemacro{\M}{1}
    \coordinate (start) at (.8,{f(.8)});
    \foreach \X [remember=\X as \LastX (initially 0)] in {1,2,3,4,...,\N}     
     {\ifnum\X<5 %<-level 1
       \draw[fill=orange!40!white] (1+\LastX*4/\N,0) rectangle (1+\X*4/\N,{f(1+\LastX*4/\N)});
       \draw[red,fill=red] (1+\LastX*4/\N,{f(1+\LastX*4/\N)}) circle (1.4pt) ;
       \path  (1+\LastX*4/\N,0pt) coordinate (x\X);
       \ifnum\X=1 %<-level 2
        \draw (1+\LastX*4/\N,3pt) -- (1+\LastX*4/\N,0pt) coordinate (x\X)
         node[anchor=north east,xshift=2pt,lnode]  {$a=x_{\X}$};
       \else
        \pgfmathtruncatemacro{\itest}{mod(\X,\M)}
        \ifnum\itest=0 %<-level 3
         \pgfmathsetmacro{\dist}{4-\LastX*4/\N}
         \ifdim\dist cm>5pt %<-level 4
          \draw (1+\LastX*4/\N,3pt) -- (1+\LastX*4/\N,0pt)
          node[anchor=north,lnode] {$x_{\X}$}; 
         \fi %<-level 4 
        \fi %<-level 3
       \fi %<-level 2
      \else
       \ifnum\X=\N %<-level 2
        \draw[fill=orange!40!white] (1+\LastX*4/\N,0) rectangle (1+\X*4/\N,{f(1+\LastX*4/\N)});
        \draw[red,fill=red] (1+\LastX*4/\N,{f(1+\LastX*4/\N)}) circle (1.4pt) ;
        \path (1+4*4/\N,{f(1+\LastX*4/\N)/2}) -- (1+\LastX*4/\N,{f(1+\LastX*4/\N)/2}) 
            node[midway,font=\huge\bfseries]{\dots};
       \fi %<-level 2
      \fi %<-level 1
   }
 \draw (5,3pt) -- (5,0pt)
 node[anchor=north west,xshift=-2pt,lnode]{$b$};
 %\draw (1,3pt) -- (1,0pt)
 % node[anchor=north west,xshift=-2pt,lnode]{$a$};
 \node at (5.3,5) [text=red] {$f(x)$};
 \coordinate (end) at (5.05,{f(5.05)});
 \draw (5,3pt) -- (5,0pt) node[anchor=north west,xshift=-2pt,lnode]{$b$};
 \draw (5,0)--(5,{f(5)});
 \draw [-latex] (-0.5,0) -- (6,0) node (xaxis) [below] {$x$};
 \draw [-latex] (0,-0.5) -- (0,5) node [left] {$y$};            
 \draw[domain=.5:5.3,samples=200,variable=\x,red,<->,thick] plot ({\x},{f(\x)});
\node at (5.3,5) [text=red] {$f(x)$};             
\end{tikzpicture}}
\end{document}

ingrese la descripción de la imagen aquí

información relacionada