为什么绘制图形时会出现错误?

为什么绘制图形时会出现错误?

我正在尝试在一个图中绘制两个函数。第一个图生成时没有错误。但是当我添加第二个函数的代码时,我得到了错误

 Package PGF Math Error: Unknown operator `x' or `x@' (in '.3+.8x').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.22 {.3+.8x};
              
This error message was generated by an \errmessage
command, so I can't give any explicit help.
Pretend that you're Hercule Poirot: Examine all clues,
and deduce the truth by order and method.
 graph/Expected, line 22

Package PGF Math Error: Unknown operator `x' or `x@' (in '.3+.8x').

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.22 {.3+.8x};
              
(That was another \errmessage.)

我正在使用以下代码来获取这两个图:

\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = \(N\),
    ylabel = {\(V\)},
]

%v1 being defined
\addplot[domain=2:10, 
    samples=100, 
    color=red,
]
{2-.9/x};
\addlegendentry{\(2-.9/N\)}

%v0 being defined
\addplot[domain=2:10, 
    samples=100, 
    color=blue,
]
{.3+.8x};
\addlegendentry{\(.3+.8N\)}


\end{axis}
\end{tikzpicture}
\end{document}

在我看来,第一个和第二个图的代码是相同的,但仍然一起使用它们会出现错误。

我是 Latex 的新手,不知道哪里出了问题。任何帮助我都感激不尽!

谢谢

答案1

乘法不是隐式的,您必须使用*来表示乘法。因此,您需要.3+.8*x,而不是.3+.8x

在第二个中,您有一个明确的除法运算符,/因此它工作正常。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = \(N\),
    ylabel = {\(V\)},
]

%v1 being defined
\addplot[domain=2:10, 
    samples=100, 
    color=red,
]
{2-.9/x};
\addlegendentry{\(2-.9/N\)}

%v0 being defined
\addplot[domain=2:10, 
    samples=2, % <-- you're plotting a straight line, so strictly speaking two samples is enough
    color=blue,
]
{.3+.8*x};
\addlegendentry{\(.3+.8N\)}


\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容