# How to find and draw the maximum value of a curve [duplicate]

I would like to find the maximum of a curve and show the value as I have indicated in the picure below. I can't find any example in this forum that is applicable to my case.

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{units}

\begin{document}

\begin{tikzpicture}
\begin{axis}[change y base,
x unit= ,
% y SI prefix=kilo,
y unit= ,
xmin=0,   xmax=1,
ymin=0,   ymax=1.1,
%     ytick={0,1000,...,6000},
xlabel=Degree,
ylabel=Force,
xlabel={$S_r$},
ylabel={$F_x/F_z$},
xlabel style={below right},
ylabel style={above left},
height=8cm,
width=9cm,
grid=minor,
samples=99,
]

\newcommand\SV{80.1}
\newcommand\SH{0}
\newcommand\B{0.210}
\newcommand\C{1.67}
\newcommand\D{6090}
\newcommand\E{0.686}
\newcommand\Fz{6000}

+( atan(\B*(x*100+\SH))
)
)
)
)/\Fz
};
\addlegendentry{$F_x(F_z=6kN)$}
\end{axis}

\end{tikzpicture}

\end{document}



• Hm, if you recall Analysis it's not a simple problem with many possible outcomes ranging from none to many, depending on ranges etc. ... What's the background or purpose of your question? Why don't you calculate at least the first derivative on paper, which is not too hard to do for your function? Commented Sep 28, 2021 at 13:15
• A good pocket calculator could do it as well. Commented Sep 28, 2021 at 14:10
• I could surely do calculate the derivative. I already have, but I am sure that there must be a way just to take the highest value (of the 99 points of the curve) and get the corresponding x value. Commented Sep 28, 2021 at 15:13
• Is your update the solution to your problem? If yes, you may want to post it as an answer to your own question and accept it. Commented Sep 28, 2021 at 19:39
• @JohnW.deBurgh It is probably more appropriate to post your "update" as an answer. It is ok to self-answer questions (and accept the answer). Commented Sep 28, 2021 at 19:39

With the help from this post I have now been able to plot what I wanted (without doing the derivative).

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{units}

\makeatletter
\pgfplotsset{
/tikz/max node/.style={
anchor=south,
},
/tikz/min node/.style={
anchor=north,
name=minimum
},
mark min/.style={
point meta rel=per plot,
visualization depends on={x \as \xvalue},
scatter/@pre marker code/.code={%
\ifx\pgfplotspointmeta\pgfplots@metamin
\def\markopts{}%
\coordinate (minimum);
\node [min node] {
\pgfmathprintnumber[fixed]{\xvalue},%
\pgfmathprintnumber[fixed]{\pgfplotspointmeta}
};
\else
\def\markopts{mark=none}
\fi
\expandafter\scope\expandafter[\markopts,every node near coord/.style=green]
},%
scatter/@post marker code/.code={%
\endscope
},
scatter,
},
mark max/.style={
point meta rel=per plot,
visualization depends on={x \as \xvalue},
scatter/@pre marker code/.code={%
\ifx\pgfplotspointmeta\pgfplots@metamax
\def\markopts{}%
\coordinate (maximum);
\node [max node] {
\pgfmathprintnumber[fixed]{\xvalue},%
\pgfmathprintnumber[fixed]{\pgfplotspointmeta}
};
\else
\def\markopts{mark=none}
\fi
\expandafter\scope\expandafter[\markopts]
},%
scatter/@post marker code/.code={%
\endscope
},
scatter
}
}
\makeatother

\begin{document}

\begin{tikzpicture}
\begin{axis}[change y base,
x unit= ,
% y SI prefix=kilo,
y unit= ,
xmin=0,   xmax=1,
ymin=0,   ymax=1.1,
%     ytick={0,1000,...,6000},
xlabel=Degree,
ylabel=Force,
xlabel={$S_r$},
ylabel={$F_x/F_z$},
xlabel style={below right},
ylabel style={above left},
height=8cm,
width=9cm,
grid=minor,
samples=199,
domain=0:1, blue,
axis lines*=middle,after end axis/.code={
\draw [thick, dashed, gray] (maximum) --({axis cs:0,0}-|maximum);}
]

\newcommand\SV{80.1}
\newcommand\SH{0}
\newcommand\B{0.210}
\newcommand\C{1.67}
\newcommand\D{6090}
\newcommand\E{0.686}
\newcommand\Fz{6000}

\addlegendentry{$F_x(F_z=6kN)$}