Usando tikz para trazar el área debajo de la curva de $y=f(x)$

Usando tikz para trazar el área debajo de la curva de $y=f(x)$

ingrese la descripción de la imagen aquí

Me preguntaba si existe una manera sencilla de generar la siguiente imagen en látex usando el paquete Tikz.

Respuesta1

¡Por diversión! (se actualizará si hay más, :-))

Compilar conAsíntotaoAplicación web asíntota.

El código de Zarko.

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);

ingrese la descripción de la imagen aquí

código de 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);

ingrese la descripción de la imagen aquí

¡No es mio!

Respuesta2

Un poco incómodo pero bien:

\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} 

Producción:

ingrese la descripción de la imagen aquí

Respuesta3

Con 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}

ingrese la descripción de la imagen aquí

Nota: MWE se basa en ejemplos del pgfplotsmanual, sección5.7 Rellenar entre.

información relacionada