借助 pgfplot 计算下黎曼和上黎曼和

借助 pgfplot 计算下黎曼和上黎曼和

在此回答我们有以下代码

    \documentclass{article}
\usepackage{pstricks-add}
\begin{document}

\psset{plotpoints=200,algebraic}
\begin{pspicture}(-0.5,-2.5)(10,3)
\psStep[linecolor=magenta,StepType=upper,
  fillstyle=solid,fillcolor=cyan!50](0,9){20}{sqrt(x)*sin(x)}
\psStep[linecolor=blue,fillstyle=solid,fillcolor=blue!30,
  opacity=0.4](0,9){20}{sqrt(x)*sin(x)}
\psaxes[labelFontSize=\scriptstyle]{->}(0,0)(0,-2.25)(10,3)
\psplot[linewidth=1.5pt]{0}{10}{sqrt(x)*sin(x)}
\end{pspicture}
\end{document}

它产生了下图

enter image description here

第一个。但是,它使用 PSTRICKS,必须与 XeLaTeX 一起运行,或者在 PDFLATEX 中使用一些其他配置。由于我不想在我的文档中包含 pstricks 代码,有人能帮我将代码转换为 pgfplot 吗?

我自己不太习惯使用 pgfplot!谢谢。

答案1

enter image description here

\documentclass[tikz,border=3.14pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\tikzset{declare function={f(\x)=sqrt(\x)*sin(deg(\x));}}
\xdef\LstMain{(0,0)}
\xdef\LstPlus{(0,0)}
\xdef\LstMinus{(0,0)}
\foreach \X in {1,...,20}
{\pgfmathsetmacro{\myx}{\X*9/20}
\pgfmathsetmacro{\myy}{f(\X)}
\xdef\LstMain{\LstMain (\myx,\myy)}
\pgfmathsetmacro{\myx}{(\X-1)*9/20}
\pgfmathsetmacro{\myyplus}{max(f(\X),0)}
\xdef\LstPlus{\LstPlus (\myx,\myyplus)}
\pgfmathsetmacro{\myx}{(\X-1)*9/20}
\pgfmathsetmacro{\myyminus}{min(f(\X),0)}
\xdef\LstMinus{\LstMinus (\myx,\myyminus)}
}
\pgfmathsetmacro{\myx}{21*9/20}
\pgfmathsetmacro{\myyplus}{max(f(21),0)}
\xdef\LstPlus{\LstPlus (\myx,\myyplus)}
\typeout{\LstMinus}
\pgfplotsset{every linear axis/.append style={ymin=-4.5,ymax=4.5,xmin=0,xmax=9.5,axis lines=center}}
\begin{tikzpicture}
\begin{axis}[ybar, xtick={2,4},
        xticklabels={$a$,$b$},axis on top]
\addplot[ybar interval,fill=red,opacity=0.15] coordinates \LstPlus;
\addplot[ybar interval,fill=green,opacity=0.15] coordinates \LstMinus;
\addplot[ybar interval,fill=blue!20] coordinates \LstMain;
\end{axis}
\begin{axis}[axis x line=none,axis y line=none]
\addplot[domain=0:9,no marks,samples=100,thick] {f(x*20/9)};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容