4

I am new to tikz/pgfplots. I just wanted to make two figures side by side showing Riemann and Lebesgue sampling. Riemann sampling here implies that sampling is uniform on horizontal axis and non-uniform on vertical axis. Lebesgue sampling is its vice versa. I have made Riemann sampling so far but I am facing difficulties drawing Lebesgue sampling.

Here is the plot I made for Riemann sampling. Can anyone suggest how I could plot Lebesgue sampling?

Riemann sampling

The code for this plot is

\begin{tikzpicture}[scale=0.9,
declare function={
f(\x)=2+sin(deg(\x-2))+sin(deg(3*\x))/2+sin(deg(5*\x))/8 + 
sin(deg(7*\x))/28;
}
]
\begin{axis}[
axis lines = middle,
xtick ={1,1.5,2,2.5,3,3.5,4},
ytick ={1,1.5,2,2.5,3,3.5,4},
xticklabels = {$x_0$,$x_1$,$x_2$,$x_3$, $\ldots$, $x_{n-1}$,$x_n$},
yticklabels = {$y_0$,$y_1$,$y_2$,$y_3$, $\ldots$, $y_{n-1}$,$y_n=b$},
ymin = -0.2,
ymax = 3.7,
xmin = -0.2,
xmax = 5.2,
x=3cm,y=2cm,
axis line style = thick,
xlabel={$x$},
ylabel={$y$},
]

\addplot [  
domain=1:4,
samples=300,
line width=1pt,
fill=none, draw=none,
fill opacity=0.1
] {f(x)} \closedcycle;

\addplot [
domain=0:5,
samples=300,
line width = 1pt, red] {f(x)};

\addplot [
ycomb, thick, blue,
no markers,
samples at={1,1.5,...,4}
] {f(x)};

\addplot [
xcomb, thick, blue,
no markers,
samples at={1,1.5,...,4}
] {f(x)};

\end{axis}
\end{tikzpicture}

We can see that intervals on y-axis are non-uniform and those on x-axis are uniform. I need something opposite to that (equispaced samples on vertical axis, making the x-axis intervals non-uniform).

Fig (a) is Riemann sampling and fig (b) is Lebesgue sampling

2
  • 1
    Welcome! Could you please describe in some more detail what you want? Do you want horizontal lines at $y_0$, $y_1$ etc. and wonder how you can compute the intersections with f(x)?
    – user121799
    Commented Feb 9, 2018 at 5:50
  • I want something like the example figure I just added in the edit.
    – Scholar
    Commented Feb 9, 2018 at 5:53

1 Answer 1

5

You can perhaps use the intersections library:

output of code

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[scale=0.9,
declare function={
f(\x)=2+sin(deg(\x-2))+sin(deg(3*\x))/2+sin(deg(5*\x))/8 + 
sin(deg(7*\x))/28;
}
]
\begin{axis}[
axis lines = middle,
xtick ={1,1.5,2,2.5,3,3.5,4},
ytick ={1,1.5,2,2.5,3,3.5,4},
xticklabels = {$x_0$,$x_1$,$x_2$,$x_3$, $\ldots$, $x_{n-1}$,$x_n$},
yticklabels = {$y_0$,$y_1$,$y_2$,$y_3$, $\ldots$, $y_{n-1}$,$y_n=b$},
ymin = -0.2,
ymax = 3.7,
xmin = -0.2,
xmax = 5.2,
x=3cm,y=2cm,
axis line style = thick,
xlabel={$x$},
ylabel={$y$},
]


\addplot [
name path=plot, % <-- added
domain=0:5,
samples=100,
line width = 1pt, red] {f(x)};

\pgfplotsinvokeforeach{1,1.5,...,4}{%
 % draw (invisible) horizontal path at the y-value given by 1,1.5,...,4
 \path[name path=a] (axis cs:0,#1) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},#1);

 \draw[thick,blue,
   % find intersections of the plot and the horizontal path
   name intersections={of=plot and a, total=\t,name=i}]
   % only draw line if an intersection was found
   \ifnum \t > 0
   (axis cs:0,#1) -| (i-1 |- {axis cs:0,0})
   plot[mark=*] coordinates {(i-1)}
   \fi 
;
}
\end{axis}
\end{tikzpicture}
\end{document}
4
  • Thanks for the answer. There is just one thing. I do not need extra intersections. I mean when the horizontal line first intersects $f(x)$, it should terminate. At the point of termination, there should be a vertical line. e.g. In the solution, the horizontal line from $y_1$ intersects $f(x)$ at three points. However, only first intersection is required. Further extension of the horizontal line and the extra vertical lines due to further intersections are not needed.
    – Scholar
    Commented Feb 9, 2018 at 10:41
  • @AbhinavSinha Then the code can be made a bit simpler, see update. Commented Feb 9, 2018 at 10:48
  • Thank you so much for your help. Works for me. One final thing..how can I add markers at each intersection?
    – Scholar
    Commented Feb 9, 2018 at 10:56
  • @AbhinavSinha See update. Commented Feb 9, 2018 at 11:27

You must log in to answer this question.

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