错误:缺少数字,视为零

错误:缺少数字,视为零

我在代码中找不到问题,但是代码末尾写着“缺失数字,视为零”

\begin{tikzpicture}[line cap=round,line join=round,>=stealth,x=1.0cm,y=1.0cm]
\begin{axis}[
x=1.5cm,y=1.5cm,
axis lines=middle,
xlabel=$x$,
ylabel=$y$,
xmin=-0.5,
xmax=6.0,
ymin=-0.5,
ymax=4.5,
xticklabels={,,1,,,4},
ytick={\empty},]
\clip(-0.5,-0.5) rectangle (6.,4.5);
\fill[fill=black,fill opacity=0.20000000298023224] (1.0045836142718776,1.4243770120110515) -- (1.0045836142718776,0.) -- (4,0) -- (3.996318624435588,0.5559251076370629) -- (3.764797284337927,0.5803447447339725) -- (3.5176114060500536,0.609277926604037) -- (3.0369721982680775,0.676251412660451) -- (2.7554549479957773,0.7294953956208646) -- (2.535734167295445,0.7672558117999624) -- (2.309147112198228,0.8185829277750029) -- (2.0550949595134687,0.8866827343500797) -- (1.8010428068287099,0.9697221311676422) -- (1.5401243797470656,1.0769669337835905) -- (1.3135373246498483,1.1965218380095286) -- (1.1487467391245991,1.306084296440851) -- cycle;
\draw[line width=1.2,smooth,samples=100,domain=6-6:6.1] plot(\x,{10/(2*(\x)+5*sqrt((\x)))});
\draw [line width=1.2pt] (1.0045836142718776,1.4243770120110515)-- (1.0045836142718776,0);
\draw [line width=1.2pt] (3.996318624435216,0.5559251076932408)-- (4.,0.);
\draw (-0.4,0.02) node[anchor=north west] {$O$};
\draw (2.2424396505089295,0.6291874906703163) node[anchor=north west] {$\mathbf{R}$};
\end{axis}
\end{tikzpicture}

答案1

我不知道这应该是什么样子,也不知道你的设置是否不同。我对此只做两点声明。首先,它编译时没有错误。其次,它不会在开始时出现奇怪的奇点。

我不知道这是否能解决您报告的错误,因为我无法重现它。相反,我得到了

! Package PGF Math Error: You've asked me to divide `10' by `0.0', but I cannot divide any number by `0.0' (in '{10/(2*(0.0)+5*sqrt((0.0)))}')

这不是一个无理的抱怨。

domain=6-6:6.1

我承认我不知道这个的目的6-6是什么。它所做的只是在pgfmath计算时减慢编译速度0.0,并使发生的事情变得不那么清晰。

domain=0.01:6.1

有效。你可以从接近零开始,但你会得到我提到的奇怪的奇点效应。

我假设此代码是从其他程序导出的。这显然没问题,但如果您注意错误,它将帮助您找到错误。您可能还想看看是否可以限制输出的精度,因为将坐标计算为小数点后 16 位只会使代码更难被人类理解。即使人类可以辨别差异(我们显然做不到),pgfmath无法管理它们,并且这种细节水平在处理过程中会丢失。(实际上,其中很大一部分会因为储存点。然后计算会进一步减少它。

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=stealth,x=1.0cm,y=1.0cm]
  \begin{axis}[
    x=1.5cm,y=1.5cm,
    axis lines=middle,
    xlabel=$x$,
    ylabel=$y$,
    xmin=-0.5,
    xmax=6.0,
    ymin=-0.5,
    ymax=4.5,
    xticklabels={,,1,,,4},
    ytick={\empty},]
    \clip(-0.5,-0.5) rectangle (6.,4.5);
    \fill[fill=black,fill opacity=0.20000000298023224] (1.0045836142718776,1.4243770120110515) -- (1.0045836142718776,0.) -- (4,0) -- (3.996318624435588,0.5559251076370629) -- (3.764797284337927,0.5803447447339725) -- (3.5176114060500536,0.609277926604037) -- (3.0369721982680775,0.676251412660451) -- (2.7554549479957773,0.7294953956208646) -- (2.535734167295445,0.7672558117999624) -- (2.309147112198228,0.8185829277750029) -- (2.0550949595134687,0.8866827343500797) -- (1.8010428068287099,0.9697221311676422) -- (1.5401243797470656,1.0769669337835905) -- (1.3135373246498483,1.1965218380095286) -- (1.1487467391245991,1.306084296440851) -- cycle;
    \draw[line width=1.2,smooth,samples=100,domain=0.01:6.1] plot(\x,{10/(2*(\x)+5*sqrt((\x)))});
    \draw [line width=1.2pt] (1.0045836142718776,1.4243770120110515)-- (1.0045836142718776,0);
    \draw [line width=1.2pt] (3.996318624435216,0.5559251076932408)-- (4.,0.);
    \draw (-0.4,0.02) node[anchor=north west] {$O$};
    \draw (2.2424396505089295,0.6291874906703163) node[anchor=north west] {$\mathbf{R}$};
  \end{axis}
\end{tikzpicture}
\end{document}

编撰的情节,尽管这是否是正确的情节是另一个问题

答案2

@cfr 的回答 (+1) 很好地解决了您的问题,但是,只需删除域定义中的错误并使用pdfplots绘制函数的方法即可避免此问题。通过使用fillbetween包库,pgfplots图表代码会短得多:

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}  % <---
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
    x=1.5cm,y=1.5cm,
    axis lines=middle,
    xlabel=$x$,
    ylabel=$y$,
    label style = {anchor=north east},
    xmin=-0.5,  xmax=6.0,
    ymin=-0.5,  ymax=4.5,
    xticklabels={,,1,,,4},
    ytick={\empty},
    domain=0:6,   % <---
    samples=100,  
    every axis plot post/.append style={line width=1.2pt},
            ]
    
\addplot[name path =A]  {10/(2*x+5*sqrt(x))};                 % <---
\path    [name path=B]  (\pgfkeysvalueof{/pgfplots/xmin},0) --
                        (\pgfkeysvalueof{/pgfplots/xmax},0);  % <---
\addplot [gray!30] fill between [of=A and B, soft clip={domain=1:4}];

\draw [line width=1.2pt]    (1,0)-- (1,{10/(2*1+5*sqrt(1))})
                            (4,0)-- (4,{10/(2*4+5*sqrt(4))});
\node[below left]   at (0,0)        {0};
\node               at (2.5,0.4)    {$\mathbf{R}$};
  \end{axis}
\end{tikzpicture}
\end{document}

在上面的 MWE 中,与您的相比,主要的区别是% <---

在此处输入图片描述

附录:
关于标签,我认为使用在图表原点处用节点写的标签来xticks显示勾选和删除标签是有意义的:x=0extra x tick

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
    x=1.5cm,y=1.5cm,
    axis lines=middle,
    xlabel=$x$,
    ylabel=$y$,
    label style = {anchor=north east},
    xmin=-0.5,  xmax=6.0,
    ymin=-0.5,  ymax=4.5,
    xticklabels={,,1,,,4},
    extra x ticks={0},   % <---
    extra x tick style={tick label style = {fill=white, inner sep=2pt}},    % <---
    ytick={\empty},
    domain=0:6, samples=100,
    every axis plot post/.append style={line width=1.2pt},
            ]

\addplot[name path =A]  {10/(2*x+5*sqrt(x))};
\path    [name path=B]  (\pgfkeysvalueof{/pgfplots/xmin},0) --
                        (\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot [gray!30] fill between [of=A and B, soft clip={domain=1:4}];

\draw [line width=1.2pt]    (1,0)-- (1,{10/(2*1+5*sqrt(1))})
                            (4,0)-- (4,{10/(2*4+5*sqrt(4))});
\node   at (2.5,0.4)    {$\mathbf{R}$};
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容