辛普森规则图示

辛普森规则图示

我想要画这个图:在此处输入图片描述

我写了一个代码如下:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
    ‎\draw [<->] (0,2) node (yaxis) [above] {$y$}‎ ‎|‎- ‎(3,0) node (xaxis) [right] {$x$};‎
     ‎\node[‎‎scale=0.4]  at (0‎‎‎‎.3,-0.‎08‎‎‎‎‎) {‎$‎a$‎};‎‎
    \node[‎‎scale=0.4]  at (1‎‎‎‎.3,-0.‎1‎‎‎‎‎) {‎$\frac{‎a+b}{2}$‎};‎‎
    \node[‎‎scale=0.4]  at (2‎‎‎‎.3,-0.‎08‎‎‎‎‎) {‎$‎b$‎};‎‎
    \path[purple,rotate=360,draw,fill=gray!50] (2.3,1.2) parabola (0.3,0.5);
    \draw [black] (0.3,0.5) to [out=20,in=-160] (1.3,1.03) to [out=20,in=160] (2.3,1.2);
   ‎\node[‎‎scale=0.3]  at (0‎‎‎‎.65,0.‎82‎‎‎‎‎) {\rotatebox{30}{‎$‎y=p_2(x)‎$‎}};‎‎
     \draw[] (1.3,0) to (1.3,1.03);
      \draw[] (0.3,0) to (0.3,0.5);
      \draw[] (2.3,0) to (2.3,1.2);
     ‎\node[‎‎scale=0.3]  at (2,1.32) {\rotatebox{4}{‎$‎y=f(x)‎$‎}};‎‎
\end{tikzpicture}
\end{document}

输出为: 在此处输入图片描述 我该如何纠正?另外,$x$ 和 $y$ 太大了。

答案1

您可以使用交点来修复交叉点,也可以使用 fillbetween 库来修复灰色区域。不过,由于您使用的是 pgfplots,如果您有曲线的闭式公式,那么我们就不必手动找到中点了,这样会容易得多。

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=center, ytick=\empty, 
ymax=1.3, ymin=0, xmax=2.6,xmin=0,
xtick={0.3,1.3,2.3}, xticklabels={$a$,$\frac{a+b}{2}$,$b$},
axis line style={-}, xlabel=$x$, ylabel=$y$]
\draw[name path=A, purple] (2.3,1.2) parabola (0.3,0.5);
\node[] at (1.6,1.2){$y=f(x)$};
\path[name path=B] (\pgfkeysvalueof{/pgfplots/xmin},0) 
                   --(\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot[gray!50] fill between[of=A and B,soft clip={domain=0.3:2.3}];
\draw[name path=C] (1.3,0) -- (1.3,1.025);
\path[name intersections={of=A and C}] coordinate (midpoint) at (intersection-1);
\draw (0.3,0.5) ..controls ++(0.1,0.3) and ++(-0.2,-0.05) .. (midpoint)
                node[pos=0.5,above left]{$y=p_2(x)$} 
                ..controls ++(0.2,0.05) and ++(-0.1,-0.05) .. (2.3,1.2);
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容