1

When I try to compile this code:

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{pgfplots}

\pgfplotsset{compat=1.9}
\pgfplotsset{ownstyle/.style={width=4cm,height=4cm,xmin=-0.5,xmax=2,ymin=0,ymax=2,axis lines=center,axis line style={->},xlabel=t/s,ylabel=x(t),xtick={0,1},ytick={0,1}}}

\begin{document}
    \begin{tikzpicture}[declare function={func(\x)=((\x)<0) * (0) + and ((\x)>=0) * (1);}]
        \begin{axis}[ownstyle]
            \addplot+[no marks,black, line width=1pt,domain=-0.5:2]{func(x)};
        \end{axis}
    \end{tikzpicture}
\end{document}

I get the following error message:

/home/max/.test.tex.swp:12: 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.0e-1])<0)(0)+and((2Y5.0e-1])>=0)(1)').

Although the error message from here and here is the same I can't find out how to fix this issue and it doesn't seem the same to me.

I would guess that the issue arises because addplot supplies the function with some weird input but I don't know why and how to change/fix that.

Max Matti
  • 121

1 Answers1

0

I think you just need to remove and from your func.

You might also prefer using samples at as in the code below, instead of just defining the domain.

enter image description here

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{pgfplots}

\pgfplotsset{compat=1.9}
\pgfplotsset{ownstyle/.style={width=4cm,height=4cm,xmin=-0.5,xmax=2,ymin=0,ymax=2,axis lines=center,axis line style={->},xlabel=t/s,ylabel=x(t),xtick={0,1},ytick={0,1}}}

\begin{document}
    \begin{tikzpicture}[declare function={func(\x)=((\x)<0) * (0) + ((\x)>=0) * (1);}]
        \begin{axis}[ownstyle]
            \addplot+[no marks,black, line width=1pt,samples at={-0.5,-0.0001,0.0001,2}]{func(x)};
        \end{axis}
    \end{tikzpicture}
\end{document}    
Torbjørn T.
  • 206,688
  • That worked for me, is there a way to define the "samples at" property globally? I need to draw a lot graphs. – Max Matti Feb 06 '17 at 14:51
  • @MaxMatti Yes, you can say \pgfplotsset{every axis plot/.append style={samples at={-0.5,-0.0001,0.0001,2}}}, and it will apply to all \addplots in the current scope. But will the same x-values be appropriate for all your plots? – Torbjørn T. Feb 06 '17 at 15:07
  • Well I'll be using \pgfplotsset{every axis plot/.append style={samples at={-0.5,-0.4,...,0.1,-0.0001,0.0001,0.1,0.2,...,0.9,0‌​.9999,1.0001,1.1,1.2‌​,...}}} to be exact but it'll work just fine. It looks like this in the end: imgur.com/a/EA0CX – Max Matti Feb 06 '17 at 20:52