帮助在曲线图下绘制矩形

帮助在曲线图下绘制矩形

我是 Latex 的初学者,我搜索了一些 Pgfplots 或 Tikz 示例,但仍然不知道如何绘制我想要的图形来显示曲线下的矩形。该图的目的是表明我正试图通过上限和来近似 ln x。此外,我希望我的图表显示 x 从 1/2 到 n,并且由于 n 是轨道,因此跳过中间值。手写图表如下所示。抱歉我的画得不好。非常感谢! 在此处输入图片描述

答案1

\documentclass[tikz,margin=5]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[>=Stealth]
  \draw[<->] (0,5) -- (0,0) -- (6.5,0);
  \draw[thick] plot[domain=1/2:6,samples=50] (\x,{2*ln(\x)});
  \foreach \i/\j in {2/2,3/3,5.5/n} {
    \draw (\i-1,0) rectangle (\i,{2*ln(\i)}) node[midway] {$\ln\j$};
    \draw[dashed] (0,{2*ln(\i)}) -- ++ (\i-1,0) node[at start,left] {$\ln\j$};
  }
  \foreach \i [count=\j] in {1,3,5} 
    \draw (\j,.1) -- ++ (0,-.2) node[right,rotate=-90] {$\j$} 
      (\j-1/2,.1) -- ++ (0,-.2) node[right,rotate=-90] {$\i/2$};
  \draw (4.5,.1) -- ++ (0,-.2) node[right,rotate=-90] {$n-1$}
    (5,.1) -- ++ (0,-.2) node[right,rotate=-90] {$n-\frac{1}{2}$} 
    (5.5,.1) -- ++ (0,-.2) node[right,rotate=-90] {$n$};
  \path (3.75,1) node {$\ldots$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

或者用pgfplots

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,width=11cm,
  xmin=0,xmax=6.8,xtick={0.5,1,...,4,5.5,6.5},
  xticklabels={$\frac{1}{2}$,$1$,$\frac{3}{2}$,$2$,$\frac{5}{2}$,$3$,$\frac{7}{2}$,$4$,
  $n$,$n+1$},xticklabel style={text height=1em},
  ytick={{ln(2)},{ln(3)},{ln(4)},{ln(6.5)}},ymax=2.2,
  yticklabels={$\ln2$,$\ln3$,$\ln4$,$\ln n$}]
\addplot[ybar interval,samples at={1,2,3,4}]{ln(x+1)};
\addplot[ybar interval,samples at={5.5,6.5}]{ln(x+1)};
\addplot[domain=0.3:6.8,smooth]{ln(x)};
 \path foreach \X in {2,3,4}
  {(\X-0.5,{ln(\X)/2})  node{$\ln \X$} 
  (\X-1,{ln(\X)}) edge[dashed] (0,{ln(\X)})}
   (4.75,{ln(5.75)/2}) node{$\cdots$}
  (6,{ln(6.5)/2}) node{$\ln n$}
  (5.5,{ln(6.5)}) edge[dashed](0,{ln(6.5)});
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

使用tkz-fct。你需要格努普特 当我仅要求一个矩形时,出现了一个小错误,我得到了两个!

\documentclass[border=5mm]{standalone}
\usepackage{tkz-fct}

\begin{document}
\begin{tikzpicture}[scale=2]
\begin{scope}
    \tkzInit[xmin=0,xmax=3,ymin=-1,ymax=2]   
    \tkzLabelX
\end{scope}

 \tkzInit[xmin=0,xmax=6,ymin=-1,ymax=2]   
 \tkzDrawX \tkzDrawY 
\tkzFct[domain =0.5:6, color=red,line width=1]{log(x)}
\tkzDrawRiemannSumSup[fill=green!40,opacity=.5,interval=1:3,number=2]
\tkzDrawRiemannSumSup[fill=green!40,opacity=.5,interval=4:5,number=1]
 \path foreach \X in {2,3}
  {(\X-0.5,{ln(\X)/2})  node{$\ln \X$}
  (\X-1,{ln(\X)}) edge[dashed] (0,{ln(\X)})}
   (3.5,{ln(4.75)/2}) node{$\cdots$}
  (4.5,{ln(6)/2}) node{$\ln n$}
  (5.5,{ln(6.5)/2}) node{$\ln (n+1)$};

\foreach \X in {2,3}{\draw  (0,{ln(\X)}) node [left]{$\ln \X$};}
\path   (0,{ln(5)}) node [left]{$\ln n$} edge[dashed] (5,{ln(5)});
\path   (0,{ln(6)}) node [left]{$\ln (n+1)$}edge[dashed] (6,{ln(6)}) ;
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容