11

I read about piecewise defined functions today and saw this post which was actually about something else:

Label plots in pgfplots without entering coordinates manually

This is taken from there.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\thispagestyle{empty}   

\begin{tikzpicture}
 \begin{axis}[
        axis y line=center,
        axis x line=middle, 
        axis on top=true,
        xmin=-7,
        xmax=7,
        ymin=-4,
        ymax=4,
        clip=false
] 

\addplot[
    mark=none,
    domain=-4:6,
    samples=80,
    red,
    thick,
] {(x<-2)*-2 + (!(x<-2) && (x<3))*x + (!(x<3)) * 3}
    node[pos=0.1,pin=135:{\color{purple}$f(x)=-2$}] {}
    node[pos=0.6,pin=135:{\color{blue}$f(x)=x$}] {}
    node[pos=0.9,pin=135:{\color{green!70!black}$f(x)=3$}] {}
;
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

I get the syntax and(<condition1>,<condition2>) which is explained here for example but I do not understand the syntax in the example above

(x<-2)*-2 + (!(x<-2) && (x<3))*x + (!(x<3)) * 3

Can somebody explain it to me or am I just being blind? There is no and like in and(a,b) and what does the ! do? I guess it's a negation.

Update

Can I find this somewhere in a documentation or is this just common knowledge?

9

In pgfmath a true expression has the value 1, and a false expression has the value 0. That is, an expression such as (x<-2) is equivalent to a function f(x):

enter image description here

Hence, this works because:

  1. (x<-2): This condition is ONLY true for x < -2. Hence this only contributes to the expression for x < -2. Hence (x<-2)*-2=-2 for x<-2, 0 otherwise.

  2. (!(x<-2) && (x<3)): This has the value 1 for -2 <= x <= 2. Hence, in this range, this has the value of x.

  3. !(x<3): This is 1 only for x >=3. Hence (!(x<3)) * 3=3 for x >=3, and zero otherwise.

If there is still any confusion, this table that computes the values of the various conditions should help:

enter image description here

  • Thanks. So (x < 2) is something like if(x < 2)? No keyword needed? Can I find this somewhere in a documentation or is this just common knowledge? – Dr. Manuel Kuehner Jun 1 '14 at 10:06
  • 1
    @ManuelKuehner you could think of it as if(x<2) then that expression is 1 otherwise it is 0. – Trefex Jun 1 '14 at 11:36
  • 1
    In case anyone else is wondering, you can find documentation about this in the pgf manual, section "Mathematical Expressions" in part VIII. – RAnders00 Nov 17 '16 at 17:49
1

This seems to be boolean logic.

So the && is the operator for and and the ! is a negation operator.

So put x = 1, then

(x<-2)*-2

is 1 < -2 ? No, OK then it's 0.

0*-2 = 0.

And the !

!(x<-2)

same thing as above but this time negate the result, so a 0 becomes a 1 and a 1 becomes a 0

and so on...

Here is some information on how boolean logic works

http://www.mathworks.nl/help/matlab/matlab_prog/truth-table-for-logical-operations.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.