使用 tikz 绘制 $y=f(x)$ 曲线下方的面积

使用 tikz 绘制 $y=f(x)$ 曲线下方的面积

在此处输入图片描述

我想知道是否有一种简单的方法可以使用 Tikz 包在乳胶中生成以下图像。

答案1

为了好玩!(如果有更多将会更新,:-))

使用以下方式编译渐近线或者Asymptote Web 应用程序

扎科的代码

import graph;
size(10cm,8cm,false);
real f(real x){return x^2;}
path F=graph(f,0,5,350);

picture pic;
fill(pic,box((2,0),(4,25)),orange);
clip(pic,F--(5,0)--cycle);
add(pic);

label("$S$",(3,3));
draw(Label("$a$",BeginPoint),(2,0)--(2,f(2)),dashed);
draw(Label("$a$",BeginPoint),(4,0)--(4,f(4)),dashed);
draw(Label("$f(x)$",Relative(.99),LeftSide),F);
draw(Label("$x$",Relative(.99)),(0,0)--(5,0),Arrow);
draw(Label("$y$",Relative(.99),LeftSide),(0,0)--(0,5^2),Arrow);

在此处输入图片描述

CFG 的代码

unitsize(1cm);
path f=(-0.3,3.7) .. controls (2.3,0.9) and (3.1,3.9) .. (5.1,4)
                  .. controls (6.5,4.1) and (6.5,3) .. (8.2,2);

picture pic;
fill(pic,box((0.5,0),(6,5)),pink);
draw(pic,(0.5,0)--(0.5,5),dashed);
draw(pic,(6,0)--(6,5),dashed);
clip(pic,f--(8.2,0)--(-0.3,0)--cycle);
add(pic);

label("$S$",(3.2,1.5));
label("$a$",(0.5,0),S);
label("$b$",(6,0),S);
draw(Label("$f(x)$",Relative(.95),LeftSide),f,red+1bp);
draw(Label("$y$",Relative(.99)),(0,-0.5)--(0,6),Arrow);
draw(Label("$x$",Relative(.99),LeftSide),(-0.5,0)--(8.3,0),Arrow);

在此处输入图片描述

不是我的!

答案2

有点尴尬但还好:

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip (-0.3,3.7) .. controls (2.3,0.9) and (3.1,3.9) .. (5.1,4) .. controls (6.5,4.1) and (6.5,3) .. (8.2,2)--(6,-1)--(-.3,-1)--cycle;
\fill[pink] (0.5,0)node [below=5pt, black] {$a$}--(6,0)node [below=5pt, black] {$b$}--(6,5)--(0.5,5)--(0.5,0)--cycle ;
\draw[thick, red, dashed] (0.5,0)--(0.5,5);
\draw[thick, red, dashed] (6,0)--(6,5);
\node at (3.2,1.5) {$\Huge S$};
\end{scope}
\draw[ultra thick, red] (-0.3,3.7) .. controls (2.3,0.9) and (3.1,3.9) .. (5.1,4) .. controls (6.5,4.1) and (6.5,3) .. (8.2,2)node[black, above=15pt,pos=.9]{$f(x)$};
\draw [thick, ->] (0,-.5)--(0,6) node[left]{$y$};
\draw [thick, ->] (-.5,0)--(8.3,0) node[below]{$x$};
\end{tikzpicture}
\end{document} 

输出:

在此处输入图片描述

答案3

pgfplots

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{fillbetween}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xlabel=$x$, ylabel=$y$,
xlabel style = {anchor=north east},
ylabel style = {anchor=north east},
xtick=\empty, ytick=\empty,
clip=false
            ]
\addplot [name path=A,domain=0:5] {x^2} node[left] {$f(x)$};
\path [name path=B]
    (\pgfkeysvalueof{/pgfplots/xmin},0) --
    (\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot [orange] 
    fill between [of=A and B,
                  soft clip={(2,0) rectangle (4,25)},];
\node at (3,3) {$S$};
\draw[dashed]   (2,2^2) -- (2,0) node[below] {$a$}
                (4,4^2) -- (4,0) node[below] {$b$};
\end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

笔记:MWE 基于pgfplots手册、章节中的示例5.7 填充

相关内容