[Tex/LaTex] Trapezium rule for integration using TikZ

tikz-pgf

I have problem to draw graphic with LaTeX (especially TikZ)?

Like this:

enter image description here

Best Answer

You could also use PGFPlots for plotting your function and the integral. The trapeziums can be generated by plotting the function twice with a low sampling frequency: Once using the ycomb style for the vertical lines, and once using the default sharp plot for the connecting lines.

If you need to do this for several plot, you can define some styles to make it easier to keep everything consistent. That way, you can get the following image

using the following code:

\begin{axis}[
    integral axis,
    ymin=0,
    xmin=0.75, xmax=11.25,
    domain=1.5:10.5,
    xtick={2,...,10},
    xticklabels={$a=x_0$, $x_1$,,,$x_{j-1}$,$x_j$,,$x_{n-1}$,$b=x_n$},
]
% The function
\addplot [very thick, cyan!75!blue] {f} node [anchor=south] {$y=f(x)$};

% The filled area under the approximate integral
\addplot [integral fill=cyan!15] {f} \closedcycle;

% The approximate integral
\addplot [integral line=black] {f};

% The vertical lines between the segments
\addplot [integral, ycomb] {f};

% The highlighted segment
\addplot [integral fill=cyan!35, domain=6:7, samples=2] {f} \closedcycle;
\end{axis}

Here's the complete document with all the styles:

\documentclass{article}

\usepackage{pgfplots} %http://www.ctan.org/pkg/pgfplots
\pgfplotsset{compat=newest, set layers=standard}

\begin{document}

\pgfplotsset{
    integral axis/.style={
        axis lines=middle,
        enlarge y limits=upper,
        axis equal image, width=12cm,
        xlabel=$x$, ylabel=$y$,
        ytick=\empty,
        xticklabel style={font=\small, text height=1.5ex, anchor=north},
        samples=100
    },
    integral/.style={
            domain=2:10,
            samples=9
    },
    integral fill/.style={
            integral,
            draw=none, fill=#1,
            on layer=axis background
        },
        integral fill/.default=cyan!10,
        integral line/.style={
            integral,
            very thick,
            draw=#1
        },
        integral line/.default=black
}


\begin{tikzpicture}[
    % The function that is used for all the plots
    declare function={f=x/5-cos(deg(x*1.85))/2+2;}
]
\begin{axis}[
    integral axis,
    ymin=0,
    xmin=0.75, xmax=11.25,
    domain=1.5:10.5,
    xtick={2,...,10},
    xticklabels={$a=x_0$, $x_1$,,,$x_{j-1}$,$x_j$,,$x_{n-1}$,$b=x_n$},
]
% The function
\addplot [very thick, cyan!75!blue] {f} node [anchor=south] {$y=f(x)$};

% The filled area under the approximate integral
\addplot [integral fill=cyan!15] {f} \closedcycle;

% The approximate integral
\addplot [integral line=black] {f};

% The vertical lines between the segments
\addplot [integral, ycomb] {f};

% The highlighted segment
\addplot [integral fill=cyan!35, domain=6:7, samples=2] {f} \closedcycle;
\end{axis}
\end{tikzpicture}

\end{document}

In case you're not using the newest version of PGFPlots (1.8), the set layers and on axis keys will not be defined. In that case, you can remove those keys and rearrange the \addplot command to make sure everything's drawn in the right order:

\documentclass{article}

\usepackage{pgfplots} %http://www.ctan.org/pkg/pgfplots
\pgfplotsset{compat=newest}

\begin{document}

\pgfplotsset{
    integral axis/.style={
        axis lines=middle,
        enlarge y limits=upper,
        axis equal image, width=12cm,
        xlabel=$x$, ylabel=$y$,
        ytick=\empty,
        xticklabel style={font=\small, text height=1.5ex, anchor=north},
        samples=100
    },
    integral/.style={
            domain=2:10,
            samples=9
    },
    integral fill/.style={
            integral,
            draw=none, fill=#1,
            %on layer=axis background
        },
        integral fill/.default=cyan!10,
        integral line/.style={
            integral,
            very thick,
            draw=#1
        },
        integral line/.default=black
}


\begin{tikzpicture}[
    % The function that is used for all the plots
    declare function={f=x/5-cos(deg(x*1.85))/2+2;}
]
\begin{axis}[
    integral axis,
    ymin=0,
    xmin=0.75, xmax=11.25,
    domain=1.5:10.5,
    xtick={2,...,10},
    xticklabels={$a=x_0$, $x_1$,,,$x_{j-1}$,$x_j$,,$x_{n-1}$,$b=x_n$},
    axis on top
]
% The filled area under the approximate integral
\addplot [integral fill=cyan!15] {f} \closedcycle;

% The highlighted segment
\addplot [integral fill=cyan!35, domain=6:7, samples=2] {f} \closedcycle;

% The function
\addplot [very thick, cyan!75!blue] {f} node [anchor=south] {$y=f(x)$};

% The approximate integral
\addplot [integral line=black] {f};

% The vertical lines between the segments
\addplot [integral, ycomb] {f};

\end{axis}
\end{tikzpicture}

\end{document}
Related Question