3

I'm plotting a graph with the following code: (also, I don't think centering is working either)

Before I made a modification to the give code:

enter image description here

The code:

\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} 

The error:

/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)};]
  • 2
    Well, your function has several issues. For instance, you suppressed all multiplication signs, have a trailing + at the end, and in the present form the and statement does not make sense. And whether or not \centering works will depend on the ambient document, which you do not disclose. –  Nov 10 '18 at 00:59
  • I'm using the exact same code as the answer given to the question posted here: https://tex.stackexchange.com/questions/373512/plotting-a-piecewise-function. I only altered the function definition. – Rafael Vergnaud Nov 10 '18 at 01:01
  • Hey marmot! I was not going after you. Sorry if it seemed that way. I just wanted to refer you to the post that I got the code from, so I posted a link in the comment. – Rafael Vergnaud Nov 10 '18 at 01:05
  • 2
    No worries! It's just that there are subtle but important differences between the two function declarations. You can't write 2(\x), use 2*\x instead, and has two arguments and you can't have a trailing +. –  Nov 10 '18 at 01:08
  • Hey, marmot! Thanks. I fixed and edited the post accordingly. However, the computer is still returning the build with errors! – Rafael Vergnaud Nov 10 '18 at 01:13
  • Unfortunately "and" still has only one argument. I will be back at my computer in less than an hour and will give more feedback if no one else did so beforehand. –  Nov 10 '18 at 01:15
  • ok. Thanks marmot. :) Sorry again for the misunderstanding earlier. – Rafael Vergnaud Nov 10 '18 at 01:16

1 Answers1

2

This is an attempt to answer your question.

  • Even though it is usual in math to write 2x, the common LaTeX parsers, which include the ones that come with TikZ and pstricks, do not understand that syntax, so you need to use 2*\x (or 2*x in the case of pfplots instead, but not for declare function).
  • You forgot a closing ] after declare function.
  • and has two arguments but I actually would not use it here. Rather I'd use ifthenelse.

However, I am unsure which function you want to plot, so I added both.

\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}

enter image description here