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.
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
returns 1 when both conditions are true and 0 when at least one is false. So, when
pgf
is computing the value offunc
at 150 it doeswhich becomes
that is,
When it's evaluating at 725, the same applies, but the result is
that is,
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:
but the computation time will be longer. Also the function definition should be changed:
so that the correct value is computed at 500 (you decide where the
=
should go).