I have a tikz-pgf-question. I'd like to draw an arbitrary Riemann sum for simple function to illustrate the definition of \int_{a}^{b} f(x) dx. My MWE is here


    declare function={
        f(\x)=2+sin(deg(\x-2))+sin(deg(3*\x))/2+sin(deg(5*\x))/8 + sin(deg(7*\x))/28;
    axis lines = middle,
    xtick ={1,1.5,2,2.5,3,3.5,4},
    ytick ={0},
    xticklabels = {$a=x_0$,$x_1$,$x_2$,$x_3$, $\ldots$, $x_{n-1}$,$x_n=b$},
    ymin = -0.2,
    ymax = 3.7,
    xmin = -0.2,
    xmax = 5.2,
    axis line style = thick,
    extra x ticks={1.3,1.85,2.2,2.7,3.2,3.75},
extra x tick labels={$\xi_1$, $\xi_2$, $\xi_3$, $\xi_4$, $\xi_{n-1}$, $\xi_n$},

\addplot [
    line width=1pt,
    fill=red, draw=none,
    fill opacity=0.1
] {f(x)} \closedcycle;

\addplot [
    line width = 1pt, red] {f(x)};

\addplot [
    ycomb, thick, red,
    no markers,
    samples at={1,1.5,...,4}
] {f(x)};

\addplot [
    ycomb, thick, blue,
    no markers,
    samples at={1.3,1.85,2.2,2.6,3.2,3.65}
] {f(x)};


How can I fill my rectangles with light-blue? riemann

After 1 hour of thinking - Edits rsum Added

\addplot[ybar, bar width=30pt, domain=1:4,samples at={1.3,1.85,2.2,2.6,3.2,3.65}, fill=blue!50!cyan,fill opacity=0.3, draw=cyan]

BUT still want to correct width of all blue bars. Please, any idea?


Here is it. Totally overwritten) enter image description here


\tikzset{>=stealth',inner sep=0pt,outer sep=2pt}

\def\L{0.5} % width of interval

\pgfmathsetmacro{\Va}{2*sin(\a r+1)+4} \pgfmathresult
\pgfmathsetmacro{\Vb}{2*sin(\b r+1)+4} \pgfmathresult
\pgfmathsetmacro{\Vc}{2*sin(\c r+1)+4} \pgfmathresult

\draw[->,thick] (-0.5,0) -- (7,0) coordinate (x axis) node[below] {$x$};
\draw[->,thick] (0,-0.5) -- (0,7) coordinate (y axis) node[left] {$y$};
\foreach \f in {1.7,2.2,...,6.2} {\pgfmathparse{2*sin(\f r+1)+4} \pgfmathresult
\draw[fill=blue!20] (\f-\L/2,\pgfmathresult |- x axis) -- (\f-\L/2,\pgfmathresult) -- (\f+\L/2,\pgfmathresult) -- (\f+\L/2,\pgfmathresult |- x axis) -- cycle;}
\node at (\a-\L/2,-5pt) {\footnotesize{$a=x_0$}};
\node at (\b+\L/2+\L,-5pt) {\footnotesize{$b=x_n$}};
\draw[blue] (\c-\L/2,0) -- (\c-\L/2,\Vc) -- (\c+\L/2,\Vc) -- (\c+\L/2,0);
\draw[dashed] (\c,0) node[below] {\footnotesize{$\xi_i$}} -- (\c,\Vc) -- (0,\Vc) node[left] {$f(\xi_i)$};
\node at (\a+5*\L/2,-5pt) {\footnotesize{$x_{i-1}$}};
\node at (\a+7*\L/2,-5pt) {\footnotesize{$x_i$}};
\node at (\a+5*\L,-5pt) {\footnotesize{$x_{i+1}$}};
\draw[blue,thick,smooth,samples=100,domain=1.45:6.2] plot(\x,{2*sin(\x r+1)+4});
\filldraw[black] (\c,\Vc) circle (.03cm);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.