I'm new to Tikz/pgfplots and am trying to understand something about the code snippet below. It works BUT Overleaf shows the brace after f(#1+0.1); in the line just above the commented out code to be unmatched; it doesn't seem like it should be unmatched to me.
What I'm actually trying to achieve is to have filled rectangles between the two curves as indicated in the commented out code block, but that doesn't seem to work at all, though it seems reasonable to me that it should.
My questions are:
1. Is there some way to make the method I'm attempting to achieve this result work?
2. If not, what would be the best way to achieve the result?
Thanks alot
Gary
Code: Select all
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\pgfplotsset{
tick label style={font=\scriptsize},
minor x grid style={gray, opacity=0.25}
}
\tikzstyle{every node}=[font=\scriptsize]
\begin{document}
\begin{tikzpicture}[
declare function={
f(\x)=0.73*pow(\x,3)-2*pow(\x,2)+\x+0.6;
g(\x)=0.17*pow(\x,2)-0.5*\x+1.1;},
]
\begin{axis}[
grid=both,
xmin=0, xmax=2, xtick distance=0.2,
minor x tick num=1,
ymin=0, ymax=1.2, ytick distance=0.1,
xlabel={$t$ (hours)}, ylabel={$r$ (inches/hour)}]
\addplot[thick, smooth, domain=0:2]
{0.73*x^3-2*x^2+x+0.6}
node[below left, pos=0.5] {$r = f(t)$};
\addplot[thick, smooth, domain=0:2]
{0.17*x^2-0.5*x+1.1}
node[above, pos=0.7] {$r = g(t)$};
\pgfplotsinvokeforeach{0.2,0.6,...,1.8}
{\draw[thick,fill=gray,opacity=0.5] (#1,0) rectangle (#1+0.2,f(#1+0.1);}
% \pgfplotsinvokeforeach{0.2,0.4,...,1.8}
% {\draw[thick,fill=gray,opacity=0.5]
% (#1,f(#1+0.1)) rectangle (#1+0.2,g(#1+0.1));
% }
\end{axis}
\end{tikzpicture}
\end{document}