使用矩形显示两条曲线之间的面积(黎曼和)

使用矩形显示两条曲线之间的面积(黎曼和)

我一直在寻找一种解决方案,使用无穷小矩形绘制两条曲线之间的区域,如下图所示:

图片 1

只使用一个矩形来显示高度差异也是很好的:

图片 2

有没有人做过类似的事情可以作为我的起点?

答案1

这是一个使用选项pgfplots

在此处输入图片描述

代码:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\pgfmathdeclarefunction{curvei}{1}{%
  \pgfmathparse{0.05*#1*(#1-7)*(#1-12)}%
}
\pgfmathdeclarefunction{curveii}{1}{%
  \pgfmathparse{-0.03*(#1)*(#1-6)*(#1-18)}%
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  xmin=-0.5,
  xmax=8,
  ymin=-5,
  ymax=7,
  domain=0.5:7.5,
  xtick={0.75,6.25},
  xticklabels={$a$,$b$},
  ytick={0},
  axis on top
]

\addplot[ybar, domain=1:6,samples=12, fill=blue!50!cyan,fill opacity=0.3,draw=cyan] 
  {curvei(x)};
\addplot[ybar, domain=1:6,samples=12, fill=blue!50!cyan,fill opacity=0.3, draw=cyan] 
  {curveii(x)};

\addplot[ycomb, domain=1:6,samples=12,densely dashed,draw=cyan!70!black] 
  {curvei(x)};
\addplot[ycomb, domain=1:6,samples=12,densely dashed,draw=cyan!70!black] 
  {curveii(x)};

\addplot[thick,no marks,red!70!black,samples=200]   
  {curvei(x)};
\addplot[thick,no marks,cyan,samples=200] 
  {curveii(x)};
\end{axis}
\end{tikzpicture}

\end{document}

第二个:

在此处输入图片描述

代码:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{decorations.pathreplacing}
\pgfplotsset{compat=1.12}

\pgfmathdeclarefunction{curvei}{1}{%
  \pgfmathparse{0.05*#1*(#1-7)*(#1-12)}%
}
\pgfmathdeclarefunction{curveii}{1}{%
  \pgfmathparse{-0.03*(#1)*(#1-6)*(#1-18)}%
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  xmin=-0.5,
  xmax=8,
  ymin=-5.5,
  ymax=7,
  domain=0.5:7.5,
  xtick={0.75,6.25},
  xticklabels={$a$,$b$},
  ytick={0},
]

\addplot[thick,no marks,red!70!black,samples=200,name path=curveA]   
  {curvei(x)};
\addplot[thick,no marks,cyan,samples=200,name path=curveB] 
  {curveii(x)};
\addplot[cyan!60,fill opacity=0.4] 
  fill between[of=curveA and curveB,soft clip={domain=0.75:6.25}];

\addplot[name path=line,draw=none,forget plot] 
  coordinates {(3,-7) (3,7)};
\path[name intersections={of=curveA and line,by={top}}];  
\path[name intersections={of=curveB and line,by={bottom}}];

\filldraw[gray!100!cyan,opacity=0.5]
  ([xshift=-10pt]top) rectangle ([xshift=10pt]bottom);  
\draw[decorate,decoration=brace]
  ([xshift=12pt]top) -- node[right=4pt] {$y_{T} - y_{B}$} ([xshift=12pt]bottom);  
\draw[decorate,decoration={brace,raise=3pt}]
  ([xshift=10pt]bottom) -- node[below=4pt] {$\Delta x$} ([xshift=-10pt]bottom);  
\node[above,red!70!black] at (1.5,4.8) {$y_{T}$};
\node[above,cyan] at (1.5,-4.8) {$y_{B}$};
\end{axis}
\end{tikzpicture}

\end{document}

相关内容