# Plotting multiple partial sums on the same graph

I wonder if it's possible to plot multiple partial sums like in the picture below:

where H is the harmonic series and particular sums are connected with lines. Unfortunately, I'm a tikz beginner and it seems the problem lies well outside my abilities as of right now.

Here is a start using pgfplots

\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymin=0,
ymax=7,
xmin=0,
xmax=100,
domain=0:100,
samples=200,
axis lines=left,
clip=true,
clip mode=individual]
\addplot [black] {ln(x) + 1} node (plot1) {};
\node [right] at (plot1) {$\ln(n) + 1$};
\addplot [black] {ln(x) +(1/x)} node (plot2) {};
\node [right] at (plot2) {$\ln(n) + \frac{1}{n}$};
\end{axis}
\end{tikzpicture}
\end{document}


Note that there is only one partial sum here.

This is an illustration using Asymptote. I think that a small value of n gives better visualization.

For a sequence of real numbers a[i], we can calculate an associated sequence of partial sum H[i] with

real[] H=partialsum(a);


and then take an array pointsH of corresponding points (i,H[i]), then joining them by straight lines operator-- as OP desired

path pH=operator--(...pointsH);


where three dots ...pointsH meaning all points of pointsH.

Full code

// http://asymptote.ualberta.ca/
usepackage("amsmath");
unitsize(1cm,2.5cm);
size(8cm);
import graph;
import math;
int n=10;
real[] a; a[0]=0;
for(int i=1; i<n; ++i) a[i]=1/i;
real[] H=partialsum(a);
pair[] pointsH;            // points of the partial sum
for(int i=1; i<H.length; ++i) pointsH.push((i,H[i]));

path pH=operator--(...pointsH);  // joining by straight segments
draw(Label("$H_n=\sum\limits_{k=1}^n\dfrac{1}{k}$",align=3E,EndPoint),pH);

real f(real x){return log(x) + 1;}      // the upper function
real g(real x){return log(x) + 1/x;}    // the lower function
real s=.25;
path pf=graph(f,s,n-1);
path pg=graph(g,s,n-1);
draw(Label("$1+ \ln x$",EndPoint,align=NE),pf,blue);
draw(Label("$\dfrac{1}{x}+ \ln x$",EndPoint,align=SE),pg,purple);

dot(pointsH,red);
axes("$x$","$y$");
draw((1,1)--(1,0)^^(1,1)--(0,1),dashed+gray);
label("$1$",(1,0),S);
label("$1$",(0,1),W);
//label("$1+\ln n <1+\dfrac{1}{2}+\dfrac{1}{3}+\codts + \dfrac{1}{n}<\dfrac{1}{n}+\ln n$",truepoint(S)+(0,-1));


Another way of illustration:

// http://asymptote.ualberta.ca/
usepackage("amsmath");
unitsize(1cm,3cm);
size(8cm);
import graph;
import math;
real f(real x){return log(x) + 1;}      // the upper function
real g(real x){return log(x) + 1/x;}    // the lower function

int n=20;
real[] h,A,B; h[0]=0; A[0]=0; B[0]=0;
for(int i=1; i<n; ++i) {
h[i]=1/i;
A[i]=f(i);
B[i]=g(i);
}
real[] H=partialsum(h);
pair[] pointsH, pointsA, pointsB;  // points of the partial sum
for(int i=1; i<H.length; ++i){
pointsH.push((i,H[i]));
pointsA.push((i,A[i]));
pointsB.push((i,B[i]));
}

path pH=operator--(...pointsH);  // joining by straight segments
path pA=operator--(...pointsA);
path pB=operator--(...pointsB);
draw(Label("$H_n=\sum\limits_{k=1}^n\dfrac{1}{k}$",align=3E,EndPoint),pH);
draw(Label("$\ln n+1$",align=3NE,EndPoint),pA);
draw(Label("$\ln n+\dfrac1x$",align=3SE,EndPoint),pB);

dot(pointsH,red);
dot(pointsA,blue);
dot(pointsB,purple);

axes("$x$","$y$");
draw((1,1)--(1,0)^^(1,1)--(0,1),dashed+gray);
label("$1$",(1,0),S);
label("$1$",(0,1),W);