[Tex/LaTex] Question regarding piecewise syntax with pgfplot

pgfplots

The piece function is defined as following:
enter image description here

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.

Edit (Add complete example)

\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{shadows}
\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}]

        \addplot[blue,domain=0:1000]{func(x)};
    \end{axis}
\end{tikzpicture}   
\end{document}

Best Answer

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]
  \addplot[blue,domain=0:1000]{func(x)};
\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).

enter image description here

Related Question