Add an arrowhead in the middle of a function path in pgfplots

Here is my attempt:

\documentclass[a4paper,12pt]{book}

\usepackage{fontspec}
\usepackage{amsmath,empheq}
% \usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{patterns}
\usetikzlibrary{calc}

% Arrow style
\tikzset{decorated arrows/.style={
    postaction={
        decorate,
        decoration={
            markings,
            mark=at position 0.5 with {\arrow{stealth}}
            }
        },
    }
}

\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0,
xmax=4,
ymin=0,
ymax=4,
]
\path[name path=axis] (1,0)--(3,0);
\addplot[name path=f,smooth,domain=1:3,blue,thick]{12/(x+3)};
\addplot[smooth,domain=1:3,blue,thick,decorated arrows]{12/(x+3)};
\addplot[yellow] fill between[of=f and axis];
\draw[dashed] (1,3)--(1,0);
\draw[dashed] (3,2)--(3,0);
\fill (1,3) circle (1.5pt);
\fill (3,2) circle (1.5pt);
\end{axis}
\end{tikzpicture}
\caption{}
\label{}
\end{figure}

\end{document}

which yields:

enter image description here

I think there is an issue between fill between and markings. Check this post "How to draw an arrowhead in the middle of a path" for more details


With use of intersections library for determine coordinates of start and end of function, softh clip on reversed domain:

\documentclass[12pt]{article}
\usepackage{fontspec}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta,
                decorations.markings,
                intersections}
\tikzset{
    ->-/.style = {decoration={markings,         % Arrow style
                  mark=at position 0.5 with {\arrow{Stealth}}
                              },                      
                  postaction={decorate},
                  ultra thick, color=blue},
    dot/.style = {circle, fill, inner sep=2pt}  % black circle
        }
\newcommand\Ymax{\pgfkeysvalueof{/pgfplots/ymax}}

\begin{document}
   \begin{figure}[htbp]
    \centering
    \begin{tikzpicture}
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0, xmax=4,
ymin=0, ymax=4,
tick label style = {font=\footnotesize},
        domain=1:3, 
samples=61
            ]
\path[name path=sgmnt] (1,\Ymax) |- (3,0) -- (3,\Ymax);
\addplot[name path=f,postaction={decorate}] {12/(x+3)};
\addplot[yellow] fill between  [of=sgmnt and f, 
                                soft clip={domain=3:1}]; % <--- inverted domain!
\addplot[->-] {12/(x+3)};   % <--- function with arrow head on the middle
%
\draw   [name intersections={of=sgmnt and f, by={a,b}}]
        [dashed] (1,0) -- (a) node[dot] {}
                 (3,0) -- (b) node[dot] {};
\end{axis}
    \end{tikzpicture}
\caption{My beautiful Diagram}
\label{fig: Field under function}
    \end{figure}
\end{document}

enter image description here


You do neither need decorations nor intersections. (BTW, your dimension too large error may go away with /pgf/fpu/install only={reciprocal}, see pgfmanual 3.1.8 section 50.2 on p. 640.)

\documentclass[a4paper,12pt]{book}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}


\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[pics/arrow/.style={code={
    \draw[blue,-{Stealth[color=red,length=#1]}] (-#1/2,0) -- (#1/2,0);}},
    pics/arrow/.default=3mm,
    bullet/.style={circle,fill,inner sep=0pt,minimum size=3pt}]
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0,
xmax=4,
ymin=0,
ymax=4,
]
\path[name path=axis] (1,0)--(3,0);
\addplot[name path=f,domain=1:3,smooth,blue,thick]{12/(x+3)}
 node[pos=0,bullet](b1){} pic[pos=0.5,sloped]{arrow} node[pos=1,bullet](b2){};
\addplot[yellow] fill between[of=f and axis];
\draw[dashed] foreach \x in {1,2}{(b\x) -- (b\x|-0,0)};
\end{axis}
\end{tikzpicture}
\caption{No decorations.}
\label{fig:No}
\end{figure}

\end{document}

enter image description here