6

I'm trying to create a piecewise plot in tikzpicture. Trying to emulate the example from this answer Plotting a piecewise function didn't work for me, and I don't understand why this code isn't working.

\documentclass[11pt, letterpaper]{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[
  declare function={
            func(\x)= (\x<=0) * ((0.5)*exp(\x))   +
            and(\x>0) * (1-(0.5)*exp(-\x))
            ;
        }
        ]
        \begin{axis}[
        axis x line=middle, axis y line=middle,
        ymin=0, ymax=1, ytick={0,0.5,1}, ylabel=$F_{X-Y}(t)$,
        xmin=-5, xmax=5, xtick={-5,...,5}, xlabel=$t$,
        ]
        \addplot[blue, domain=-5:5, smooth]{func(x)};
\end{axis}
\end{tikzpicture} 
\end{document}

Any help would be greatly appreciated!

Stefan Pinnow
  • 29,535
Nico
  • 162
  • 3
    Welcome! You have an excess and. Use func(\x)= (\x<=0) * ((0.5)*exp(\x)) + (\x>0) * (1-(0.5)*exp(-\x));. –  Feb 19 '20 at 03:51

1 Answers1

10

Welcome! You call and with only one argument, and it seems to me (but I may be wrong) that you do not want an and at all. (You may also change the function to only use abs and sign functions, see the afunc definition.)

\documentclass[11pt, letterpaper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[
  declare function={
            func(\x)= (\x<=0) * ((0.5)*exp(\x))   +
            (\x>0) * (1-(0.5)*exp(-\x));
            afunc(\x)=0.5*(1+sign(\x)-sign(\x)*exp(-abs(\x)));
        }
        ]
        \begin{axis}[
        axis x line=middle, axis y line=middle,
        ymin=0, ymax=1, ytick={0,0.5,1}, ylabel=$F_{X-Y}(t)$,
        xmin=-5, xmax=5, xtick={-5,...,5}, xlabel=$t$,
        ]
        \addplot[blue, domain=-5:5, smooth]{func(x)};
        \addplot[red, domain=-5:5, smooth,dashed,thick]{afunc(x)};
\end{axis}
\end{tikzpicture} 
\end{document}

enter image description here

Of course, the function may be approximated by a tanh.

\documentclass[11pt, letterpaper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[
  declare function={
            func(\x)= (\x<=0) * ((0.5)*exp(\x))   +
            (\x>0) * (1-(0.5)*exp(-\x));
        }
        ]
        \begin{axis}[
        axis x line=middle, axis y line=middle,
        ymin=0, ymax=1, ytick={0,0.5,1}, ylabel=$F_{X-Y}(t)$,
        xmin=-5, xmax=5, xtick={-5,...,5}, xlabel=$t$,
        ]
        \addplot[blue, domain=-5:5, smooth]{func(x)};
        \addplot[red, domain=-5:5, smooth,dashed,thick]{0.5*tanh(x/1.5)+0.5};
\end{axis}
\end{tikzpicture} 
\end{document}

enter image description here

  • 2
    afunc(\x)=0.5*(1+sign(\x)*(1-exp(-abs(\x)))); works too :-) . Nice answer (+1) – Zarko Feb 19 '20 at 04:19
  • 1
    @Zarko Thanks! Yes, this is even simpler. –  Feb 19 '20 at 04:21
  • 1
    BTW, such function (without offset) is used as approximation of `sign˙ function in a realization of the sliding mode control (SMC). With it theoretical infinity switching frequency of the SMC is set to some reachable value of a used hardware. – Zarko Feb 19 '20 at 04:32
  • @Zarko Thanks again! I am more familiar with tanh, which has an analytic continuation. It appears often in domain walls, kinks and quantum mechanics. –  Feb 19 '20 at 04:34
  • @Schrodinger's cat This works perfectly, thank you! – Nico Feb 19 '20 at 04:36
  • @Zarko good to know! Can Fourier Transform be used here to approximate tanh, too? – manooooh Feb 19 '20 at 05:31
  • @Zarko Yes, it gives some hyperbolic cosecant function. Of course, if you have exp(-|x|) you get something like 1/(1+k^2), i.e. the propagator of a massive particle, which may be advantageous to use. –  Feb 19 '20 at 05:38
  • ups, now we are off-topic :-) ... Fourier transform in limits is possible (so far I din't try calculate it). Regarding particle, I'm little bit familiar only with particle filters in signal processing (but no longer work on this domain) ;-) – Zarko Feb 19 '20 at 05:48