What I'm trying to do is plot a function (defined using \newcommand{\f}[1]{expression}) using pgfplots Then draw N+1 lines. Each going from (axis cs: x,0) to (axis cs: x,f(x)), where x is in (0,1/N,2/N,...,1).
I tried using a \foreach loop but it didn't work in pgfplots. So I tried replacing it with \pgfplotsinvokeforeach, but I still had to find someway to calculate f(x) before putting it inside the coordinate.
I tried using \pgfmathsetmacro\foo{\f{#1}}, but it seemed like only the last value stuck, as if it waited until the loop was over before drawing the lines. So instead of getting the points
(0,f(0)), (1/N,f(1/N)),..., (1,f(1)),
I got (0,f(1)), (1/N,f(1)),... ,(1,f(1)).
Is there a simple way of doing this?
Code:
\begin{tikzpicture}
\begin{axis}[
xlabel = ,
ylabel = ,
axis lines = middle,
xtick ={1,4},
ytick ={0},
xticklabels = {$a$,$b$},
ymin = -0.2,
ymax = 3.7,
xmin = -0.2,
xmax = 5.2,
x=2cm,y=2cm,
axis line style = thick,
]
\newcommand{\f}[1]{2+sin(deg(#1-2))+sin(deg(3*#1))/2+sin(deg(5*#1))/8 + sin(deg(7*#1))/28}
\addplot[domain=0:5, samples = 300, line width = 1pt, colorOne, name path = f]{\f{x}};
\path [name path = axis] (axis cs:1,0) -- (axis cs:4,0);
\addplot [thick, color = colorOne, fill = colorOne, fill opacity=0.2]
fill between[of=f and axis, soft clip = {domain=1:4}];
\pgfplotsinvokeforeach{1,1.2,...,4}{
\pgfmathsetmacro\fAtPoint{\f{#1}}
\draw[line width = 1pt, colorOne] (axis cs:#1,0) -- (axis cs: #1,\fAtPoint);
}
\end{axis}
\end{tikzpicture}
Note: Instead of going from 0 to 1, I'm trying to go from 1 to 4 with 1/N = 0.2
I ended up using this solution at the end. But I'd like to know how I could do it using for loops.

axis. The suggested method in section 8.1 of the manual (the Remark under the description of\pgfplotsforeachungrouped) doesn't seem to work here. I'll note though that the simplest way of making those lines is aycombplot, i.e.\addplot [ycomb,blue!20,samples at={1,1.2,...,4}] {\f{x}};– Torbjørn T. Dec 30 '15 at 18:13\documentclass, ending with\end{document}, and containing all the necessary packages, libraries, colour definitions and similar. In this case it's not a big deal to make it workable, but sometimes it can be.) – Torbjørn T. Dec 30 '15 at 18:20