# Question regarding piecewise syntax with pgfplot?

The piece function is defined as following:

Here is the code snippet:

\pgfmathdeclarefunction{func}{1}{%
\pgfmathparse{%
(and(#1>=0 , #1<=500)   * (300 + #1*(12/10))   +%
(and(#1>500, #1<=1000)  *  (600 + #1*(12/10))    %
}%
}


What does the asterisk "*" right after the (and.. actually do? and the "+" sign at the end. I reused some the examples that I found in this site, but the syntax is not really intuitive. Could anyone explain this syntax? Thanks in advance.

\documentclass[10pt,letterpaper]{article}

\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{polynomial}
\usepackage{layouts}
\usepackage{enumerate}
\usepackage{syntax}
\usepackage{gensymb}
\usepackage{cancel}
\usepackage{calc}
\usepackage{xcolor}

\usepackage[version=0.96]{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,automata,backgrounds,petri,positioning}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.shapes}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.fractals}
\usetikzlibrary{decorations.footprints}
\usetikzlibrary{calc}
\usetikzlibrary{spy}
\usetikzlibrary{matrix}

\usepackage{tikz-qtree}
\usepackage{pgfplots}

\begin{document}
\pgfmathdeclarefunction{func}{1}{%
\pgfmathparse{%
(and(#1>=0    ,#1<=500)  * (300 + #1*(12/10))   +%
(and(#1>=500  ,#1<=1000) * (600 + #1*(12/10))   %
}%
}

\begin{tikzpicture}[scale=0.8]
\begin{axis}
[title={$C(x)$},
ylabel=$y$,
xlabel=$x$,
grid=both,
minor xtick={0,100,...,1000},
xtick={0,200,...,1000},
ytick={0,400,...,3200}]

\end{axis}
\end{tikzpicture}
\end{document}

-
I guess that and(...) returns 1 when the condition inside is true and 0 when false; * denotes multiplication. So, when #1 is for instance 100, you're doing 1*(300+100*(12/10))+0*(600+100*(12/10)) which is what's expected. When #1 is 700, the zero and one are reversed. –  egreg Oct 21 '12 at 22:19
@egreg: Thanks a lot. It's really cryptic. –  Chan Oct 21 '12 at 22:22
@Chan, I think you'd better use two different plot commands for the two pieces of line in order to show the discontinuity. So, you could also set samples=2 improving the computation time and the file size. –  Luigi Oct 22 '12 at 8:41

It's simple math. :) But one should know that

and(<condition1>,<condition2>)


returns 1 when both conditions are true and 0 when at least one is false. So, when pgf is computing the value of func at 150 it does

(and(150>=0 , 150<=500) * (300 + 150*(12/10)) + (and(150>500, 150<=1000) * (600 + 150*(12/10))


which becomes

1 * (300 + 150*(12/10)) + 0 * (600 + 150*(12/10))


that is,

(300 + 150*(12/10))


When it's evaluating at 725, the same applies, but the result is

0 * (300 + 725*(12/10)) + 1 * (600 + 725*(12/10))


that is,

(600 + 725*(12/10))


In both cases the value is what's requested.

You need to correct the input if you want a graph which has an actual step at the discontinuity:

\begin{tikzpicture}[scale=0.8]
\begin{axis}
[title={$C(x)$},
ylabel=$y$,
xlabel=$x$,
grid=both,
minor xtick={0,100,...,1000},
xtick={0,200,...,1000},
ytick={0,400,...,3200},
samples=1000]
\end{axis}
\end{tikzpicture}


but the computation time will be longer. Also the function definition should be changed:

\pgfmathdeclarefunction{func}{1}{%
\pgfmathparse{%
(and(#1>=0  , #1<500)   * (300 + #1*(12/10)) +
(and(#1>=500 , #1<=1000) * (600 + #1*(12/10))
}%
}


so that the correct value is computed at 500 (you decide where the = should go).

-
Thanks again. By the way, when I modify the first condition to and(#1>=0 , **#1<500**) , the graph still looks the same, which is quite odd. Could you help me with this? –  Chan Oct 21 '12 at 23:14
@Chan Please, add a complete example of code. –  egreg Oct 21 '12 at 23:16
Added. Thanks in advance ;) –  Chan Oct 21 '12 at 23:20