0

I am a beginner of Latex and I have searched for some Pgfplots or Tikz examples but still don't know how can I draw a graph I want for showing the rectangles under the curve. The idea of the graph is to show that I am trying to approximate ln x by upper sum. Also, I want my graph to show x from 1/2 to n and the middle values are skipped since n is orbitary. The hand-written graph is something like below. Sorry for my bad drawing. Thanks a lot! enter image description here

3

3 Answers 3

5
\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}

enter image description here

2
  • Masterpiece! Thanks a lot!
    – Kenny Hin
    Dec 29, 2019 at 6:51
  • 1
    +1: You are very fast :) Dec 29, 2019 at 6:59
5

Or with 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}

enter image description here

4

With tkz-fct. You need gnuplot Little bug when I ask for only one rectangle, I get two !

\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}

enter image description here

1
  • 1
    With fp the syntax is log with pgfmath it's ln Dec 29, 2019 at 7:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .