在同一張圖上繪製多個部分和

在同一張圖上繪製多個部分和

我想知道是否可以繪製多個部分和,如下圖所示:

其中 H 是調和級數,特定的和用線連接。不幸的是,我是 tikz 初學者,目前看來問題遠遠超出了我的能力範圍。

答案1

這是開始使用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}

答案2

請注意,這裡只有一個部分和。

這是使用 Asymptote 的插圖。我認為較小的值可以n提供更好的視覺化效果。

對於實數序列,我們可以計算部分和的a[i]關聯序列:H[i]

real[] H=partialsum(a);

然後取一組pointsH對應的點 ,然後根據OP的需要(i,H[i])用直線連接它們operator--

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

其中三個點...pointsH表示 的所有點pointsH

完整程式碼

在此輸入影像描述

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

另一種說明方式:

在此輸入影像描述

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

相關內容