Использование tikz для построения области под кривой $y=f(x)$

Использование tikz для построения области под кривой $y=f(x)$

введите описание изображения здесь

Мне было интересно, есть ли простой способ создать следующее изображение в латексе с помощью пакета Tikz.

решение1

Для развлечения! (буду обновлять, если будет больше, :-) )

Компилировать сАсимптотаилиВеб-приложение Asymptote.

Код Зарко

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 Заполнить между.

Связанный контент