リーマン和は曲線の下の面積に近づく

リーマン和は曲線の下の面積に近づく

私は、サブ区間の数 n(\def\n\some number を使用) を変更できるプログラムを作成し、出力でリーマン和の特定の数の長方形を表示しようとしています。可能であればアニメーションがあると便利ですが、n が大きい場合、黒い線と塗りつぶされた赤い円によって問題が発生する可能性があります。私の意図が明確になれば幸いです。これが私の MWE です。楽しんでください! すべての回答に興味があります!

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{center}
\begin{tikzpicture}[scale=1.2,declare function={f(\x)=((1/3)*(\x)^(3)-3*(\x)^(2)+8*\x-3;}]
\coordinate (start) at (.8,{f(.8)});
\coordinate (x0) at (1,{f(1)});
\coordinate (x1) at (2,{f(2)});
\coordinate (x2) at (3,{f(3)});
\coordinate (x3) at (4,{f(4)});
\coordinate (x4) at (5,{f(5)});
\coordinate (end) at (5.05,{f(5.05)});
\draw[fill=orange!40!white] (1,0) rectangle (2,{f(1)});
\draw[fill=orange!40!white] (2,0) rectangle (3,{f(2)});
\draw[fill=orange!40!white] (3,0) rectangle (4,{f(3)});
\draw[fill=orange!40!white] (4,0) rectangle (5,{f(4)});
\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$};
\foreach \x/\xtext in {1/a=x_{1} ,2/x_{2}, 3/x_{3} , 4/x_{4} , 5/b }
 \draw[xshift=\x cm] (0pt,3pt) -- (0pt,0pt) 
node[below=2pt,fill=white,font=\normalsize]
  {$\xtext$};
\draw[domain=.5:5.3,samples=200,variable=\x,red,<->,thick] plot ({\x},{f(\x)});                 
\foreach \n in {0,1,2,3}
\draw[red,fill=red] (x\n) circle (2pt) node[font=\normalsize] {$$};    
\draw[<->] (2,-1)--(3,-1) node[above,midway] {$\Delta x$};      
\end{tikzpicture}
\end{center}
\end{document}

出力は次のようになります:

ここに画像の説明を入力してください

答え1

アニメーションはこちらです。ラベルの改善を後押ししてくれた JouleV に大いに感謝します。(今では pgfplots がすぐに実行できることにさらに感謝しています。)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}

\begin{document}
\foreach \N in {4,5,...,21}
{\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}{\N/4}
 \coordinate (start) at (.8,{f(.8)});
 \foreach \X [remember=\X as \LastX (initially 0)] in {1,...,\N}
 {\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 (2pt) ;
 \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}$};  
        \fi  
     \fi
  \fi
 }
 \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)});                 
 \draw[<->] (x2|- 0,-1)--(x3|- 0,-1) node[above,midway] {$\Delta x$};      
\end{tikzpicture}}
\end{document}

ここに画像の説明を入力してください

追加のリクエストについては、

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}

\begin{document}
\foreach \N in {4,5,...,25}
{\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}{\N/4}
 \coordinate (start) at (.8,{f(.8)});
 \ifnum\N<22
   \foreach \X [remember=\X as \LastX (initially 0)] in {1,...,\N}
   {\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 (2pt) ;
   \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}$};  
          \fi  
       \fi
    \fi
   }
   \draw[<->] (x2|- 0,-1)--(x3|- 0,-1) node[above,midway] {$\Delta x$};      
 \else
   \draw[fill=orange!40!white]
   plot[domain=1:5,samples=167,variable=\x] ({\x},{f(\x)})
   -- (5,0) -| cycle;
 \fi
 \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)});                 
\end{tikzpicture}}
\end{document}

ここに画像の説明を入力してください

関連情報