Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

So I'm doing a report on convergence tests in LaTeX, and would like to produce a diagram demonstrating the integral test for convergence. I am somewhat new to LaTeX, and a total n00b when it comes to Tikz, so any help would be much appreciated.

I am essentially trying to produce something like this:

enter image description here

You might ask why not just use this image, right? Truthfully, I would like to learn TikZ, and would very much appreciate any help in coming up with this plot.

Furthermore, I would like to place inside each bar the corresponding term of the series: a_1, a_2, a_3, ..., a_n

Any assistance would be much appreciated!

share|improve this question
1  
Related post/duplicate?: Timing and integration with TikZ PGFlots –  cmhughes Jan 11 '12 at 17:22
    
Ha! I actually saw that thread and spent a couple of hours trying to tweak the code to match what I was trying to produce. I couldn't however, as the plots are Riemann sums, with the function hitting the midpoints of the boxes, rather than the min/max. Thanks though. –  AlmostSurely Jan 11 '12 at 17:28
add comment

2 Answers

up vote 14 down vote accepted

With tkz-fct and gnuplot

\documentclass[]{scrartcl}
\usepackage{tkz-fct}   

\begin{document}
\begin{tikzpicture}[scale=1.25]
\tkzInit[xmax=8,ymax=4]
\tkzAxeXY[ticks=false]
\tkzGrid   
\tkzFct[color = red, domain =0.125:8]{4./x}
\tkzDrawRiemannSumInf[fill=green!60,
                     opacity=.2,
                     color=green,
                     line width=1pt,
                     interval=1:8,
                     number=7] 
 \foreach \x/\t in {1.5/$a_1$,2.5/$a_2$,3.5/$a_3$,7.5/$a_7$}
 \node[green!50!black] at (\x,{4/(\x+1)-0.25}){\t};  
\end{tikzpicture}   
\end{document} 

enter image description here

share|improve this answer
    
Wow, thank you so much. Beautiful! –  AlmostSurely Jan 11 '12 at 18:10
    
Just a couple questions for you: It doesn't seem to be showing the function's red line. Could you possibly briefly outline the process you used to make this in Gnuplot? Thanks! –  AlmostSurely Jan 11 '12 at 18:54
1  
I use only --shell-escape with pdflatex but sometimes you need to use --enable-write18. This is the same option but the form depends of your tools ( system, distribution etc. ). Look at the .log file, sometimes TeX doesn't find gnuplot (problem of path) and then with --shell-escape or with --enable-write18, TeX authorizes gnuplot to write a file. –  Alain Matthes Jan 11 '12 at 20:00
add comment

The code from Jake's answer to Timing and integration with TikZ PGFlots discusses the mid-point rule. It can be tweaked to use right hand sums, and left hand sums.

% right hand sums
\pgfplotsset{
    right segments/.code={\pgfmathsetmacro\rightsegments{#1}},
    right segments=3,
    right/.style args={#1:#2}{
        ybar interval,
        domain=#1+((#2-#1)/\rightsegments):#2+((#2-#1)/\rightsegments),
        samples=\rightsegments+1,
        x filter/.code=\pgfmathparse{\pgfmathresult-((#2-#1)/\rightsegments)}
    }
}

rhsums

% left hand sums
\pgfplotsset{
    left segments/.code={\pgfmathsetmacro\leftsegments{#1}},
    left segments=3,
    left/.style args={#1:#2}{
        ybar interval,
        domain=#1:#2,
        samples=\leftsegments+1,
        x filter/.code=\pgfmathparse{\pgfmathresult}
       }
}

lhsums

and, finally, a complete MWE

\documentclass{article}

\usepackage{pgfplots}

% mid-point rule
\pgfplotsset{
    midpoint segments/.code={\pgfmathsetmacro\midpointsegments{#1}},
    midpoint segments=3,
    midpoint/.style args={#1:#2}{
        ybar interval,
        domain=#1+((#2-#1)/\midpointsegments)/2:#2+((#2-#1)/\midpointsegments)/2,
        samples=\midpointsegments+1,
        x filter/.code=\pgfmathparse{\pgfmathresult-((#2-#1)/\midpointsegments)/2}
    }
}

% right hand sums
\pgfplotsset{
    right segments/.code={\pgfmathsetmacro\rightsegments{#1}},
    right segments=3,
    right/.style args={#1:#2}{
        ybar interval,
        domain=#1+((#2-#1)/\rightsegments):#2+((#2-#1)/\rightsegments),
        samples=\rightsegments+1,
        x filter/.code=\pgfmathparse{\pgfmathresult-((#2-#1)/\rightsegments)}
    }
}

% left hand sums
\pgfplotsset{
    left segments/.code={\pgfmathsetmacro\leftsegments{#1}},
    left segments=3,
    left/.style args={#1:#2}{
        ybar interval,
        domain=#1:#2,
        samples=\leftsegments+1,
        x filter/.code=\pgfmathparse{\pgfmathresult}
       }
}



\begin{document}

% left hand sums
\begin{tikzpicture}[/pgf/declare function={f=4/x;}]
\begin{axis}[
        xmin=0,xmax=9,ymin=0,ymax=4,
    domain=0:10,
    samples=100,
    axis lines=middle
]
\addplot [thick, red] {f};
\addplot [
    black!80,fill=green,opacity=.3,
    left segments=7,
    left=1:8
] {f};
\end{axis}
\end{tikzpicture}

% right hand sums
\begin{tikzpicture}[/pgf/declare function={f=4/x;}]
\begin{axis}[
        xmin=0,xmax=9,ymin=0,ymax=4,
    domain=0:10,
    samples=100,
    axis lines=middle
]
\addplot [thick, red] {f};
\addplot [
    black!80,fill=green,opacity=.3,
    right segments=7,
    right=1:8,
] {f};
\end{axis}
\end{tikzpicture}

% mid point
\begin{tikzpicture}[/pgf/declare function={f=4/x;}]
\begin{axis}[
        xmin=0,xmax=8,ymin=0,ymax=4,
    domain=0:10,
    samples=100,
    axis lines=middle
]
\addplot [thick,red] {f};
\addplot [
    black!80,fill=green,opacity=.3,
    midpoint segments=7,
    midpoint=1:8,
] {f};
\end{axis}
\end{tikzpicture}


\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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