获取在 tikz 环境中声明的值 f(x)

获取在 tikz 环境中声明的值 f(x)

我在 tikzpicture 环境中声明了我的 f(x),并且 tikz 绘制了它的图形。好的,现在我想在函数图上标记一个点。

我想,我可以将点 M 坐标定位在 (x, myrandomf(x)) 处,然后对 M 进行我想要的操作。例如,在那里画一个小圆圈。

但是这里出现错误“未知函数‘x’”:

\coordinate (M) at (3, myrandomf(3));

我该如何修复它?

整个代码:

\begin{tikzpicture}[
          declare function ={ myrandomf(\x) = 5 + (x-4)^3/10 - x^2/10; 
                            },
        ]
          \begin{axis}[
              axis lines = middle,
              axis line style = {-Latex[round],very thick},
              enlargelimits = true,
              xlabel = {$x$}, 
              ylabel = {$f(x)$}, 
              xmin = -1,
              ymin = -1,
              xmax = 10,
              ymax = 10,
              domain = -1:10,
              xtick = \empty,
              ytick = \empty,
              extra x ticks={0},
              xlabel style={below right},
              ylabel style={above left},
              x tick label style={below right},
              samples = 100
          ]
            \addplot[very thick, color=red] {myrandomf(x)};
            \coordinate (O) at (0, 0);
            \coordinate (M) at (3, myrandomf(3));
            % \filldraw (M) circle[radius=1.5pt];
          \end{axis}
\end{tikzpicture}

答案1

除了 Rmano 在评论中提到的内容之外,您不需要在函数声明的所有地方使用\xx所以你有

declare function ={ myrandomf(\x) = 5 + (\x-4)^3/10 - \x^2/10; },

然后

\coordinate (M) at (3, {myrandomf(3)});

附注:如果您只想在图表上的某些点添加点,您可以这样做\addplot [only marks, samples at={3}] {myrandomf(x)};

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[
          declare function ={ myrandomf(\x) = 5 + (\x-4)^3/10 - \x^2/10; 
                            },
        ]
          \begin{axis}[
              axis lines = middle,
              axis line style = {-Latex[round],very thick},
              enlargelimits = true,
              xlabel = {$x$}, 
              ylabel = {$f(x)$}, 
              xmin = -1,
              ymin = -1,
              xmax = 10,
              ymax = 10,
              domain = -1:10,
              xtick = \empty,
              ytick = \empty,
              extra x ticks={0},
              xlabel style={below right},
              ylabel style={above left},
              x tick label style={below right},
              samples = 100
          ]
            \addplot[very thick, color=red] {myrandomf(x)};
            
%            \addplot [only marks, samples at={3}] {myrandomf(x)};
            
            \coordinate (O) at (0, 0);
            \coordinate (M) at (3, {myrandomf(3)});
            \filldraw (M) circle[radius=1.5pt];
          \end{axis}
\end{tikzpicture}

\end{document}

相关内容