绘制分段函数-不起作用,并且不知道为什么!

绘制分段函数-不起作用,并且不知道为什么!

我正在用以下代码绘制图表:(另外,我不认为定心正在工作)

在我对给出的代码进行修改之前:

在此处输入图片描述

代码:

\centering
\begin{tikzpicture}[
    declare function={
    func(\x) = (\x < 0) * (2*\x) +
           and (\x >= 0) * (2*\x+1)
    ;}

\begin{axis}[
    axis x line=middle, axis y line=middle,
    ymin=-5, ymax=5, ytick={-5,...,5}, ylabel=$y$,
    xmin=-5, xmax=5, xtick={-5,...,5}, xlabel=$x$,
    domain=-5:5,samples=101, % added
]

\addplot [blue,thick] {func(x)};
\end{axis}
\end{tikzpicture} 

错误:

/Users/rafaelvergnaud/Desktop/hw10.tex:103: Package PGF Math Error: Sorry, an internal routine of the floating point unit got an ill-formatted floating point number `Y'. The unreadable part was near 'Y'. (in '(2Y5.0e0]<0)*(2(2Y5.0e0]))+and(2Y5.0e0]>=0)*(2(2Y5.0e0])+1)+'). [\addplot [blue,thick] {func(x)};]
/Users/rafaelvergnaud/Desktop/hw10.tex:103: Package PGF Math Error: Sorry, an internal routine of the floating point unit got an ill-formatted floating point number `0'. The unreadable part was near '0'. (in '(2Y5.0e0]<0)*(2(2Y5.0e0]))+and(2Y5.0e0]>=0)*(2(2Y5.0e0])+1)+'). [\addplot [blue,thick] {func(x)};]
/Users/rafaelvergnaud/Desktop/hw10.tex:103: Missing number, treated as zero. [\addplot [blue,thick] {func(x)};]
/Users/rafaelvergnaud/Desktop/hw10.tex:103: Illegal unit of measure (pt inserted). [\addplot [blue,thick] {func(x)};]
/Users/rafaelvergnaud/Desktop/hw10.tex:103: Missing = inserted for \ifdim. [\addplot [blue,thick] {func(x)};]
/Users/rafaelvergnaud/Desktop/hw10.tex:103: Missing number, treated as zero. [\addplot [blue,thick] {func(x)};]
/Users/rafaelvergnaud/Desktop/hw10.tex:103: Illegal unit of measure (pt inserted). [\addplot [blue,thick] {func(x)};]

答案1

这是为了尝试回答您的问题。

  • 虽然在数学中写 是很常见的2x,但常见的 LaTeX 解析器(包括 Ti 附带的解析器)Z 和 pstricks 不理解该语法,因此您需要使用2*\x(或者2*x在 pfplots 的情况下使用,但不是declare function)。
  • ]您忘了之后的结束语declare function
  • and有两个参数,但实际上我不会在这里使用它。相反,我会使用ifthenelse

但是,我不确定您想要绘制哪个函数,因此我添加了两个函数。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[
    declare function={
    func(\x) = (abs(\x) < 1) * (2*(1-abs(\x)));}] % added ]

\begin{axis}[
    axis x line=middle, axis y line=middle,
    ymin=-5, ymax=5, ytick={-5,...,5}, ylabel=$y$,
    xmin=-5, xmax=5, xtick={-5,...,5}, xlabel=$x$,
    domain=-5:5,samples=101, % added
]
\addplot [blue,thick] {func(x)};
\end{axis}
\end{tikzpicture} 
\caption{\texttt{\textbackslash centering} works in document classes like \texttt{article}, but
not (easily) in \texttt{standalone} because there is (a priori) nothing to be
centered against.}
\end{figure}

\begin{figure}
\centering
\begin{tikzpicture}[
    declare function={
    func(\x) = ifthenelse(\x<0,2*\x,2*\x+1);}] 

\begin{axis}[
    axis x line=middle, axis y line=middle,
    ymin=-5, ymax=5, ytick={-5,...,5}, ylabel=$f'(x)$,
    xmin=-5, xmax=5, xtick={-5,...,5}, xlabel=$x$,
    domain=-5:5,samples at={-5,-0.0001,0,5}, % added
]
\addplot [blue,thick] {func(x)};
\end{axis}
\end{tikzpicture} 
\caption{This is the function from your screen shot. I recommend using
\texttt{samples at} here because otherwise you will have to use a very large
number of samples. Alternatively, you could let the function jump by just
plotting two functions, i.e.\ having two \texttt{\textbackslash addplot}
commands, but probably you want to have the stretch with infinite slope.}
\end{figure}
\end{document}

在此处输入图片描述

相关内容