5

I am trying to plot a piecewise defined function, but somehow I don't manage to get it right. See below.

The function to be plotted above, the failed plot below.

I use the following code

\begin{tikzpicture}[
declare function={
func(\x)=(\x >8/10) * (6/10* \x) +
 and(\x > 0.6,\x <= 0.8)* (6/9* \x )    + 
            (\x<=0.6) * (\x*6/8);
}
]
\begin{axis}[xmin=0,xmax=1,
              ymin=0,ymax=1,
              x dir=reverse]
\addplot[]{func(x)};
\end{axis}
\end{tikzpicture}

My understanding was that and(cond,cond) gives me 1 when both conditions hold and zero otherwise. Then, isn't the function I declared correct? I see that the first part is correct. But I don't understand why it isn't followed by a jump as supposed. I also don't understand the kink at 0.4 instead of 0.6.

I expected a function that decreases (because I reversed the x axis) and has upward jumps at 0.8 and 0.6. What am I doing wrong?

I am having the same problem with

\pgfmathdeclarefunction{func}{1}{% \pgfmathparse{...

Thanks in advance!

6

You are not doing anything wrong. It's just that you are using the default sample number and the default domain. Adjusting them gives you the result.

\begin{tikzpicture}[
declare function={func(\x)=(\x>0.8)*(0.6*\x)+and(\x>0.6,\x<=0.8)*(2/3*\x)+(\x<=0.6)*(\x*0.75);}
]
\begin{axis}[xmin=0,xmax=1,samples=351,domain=0:1,
              ymin=0,ymax=1,
              x dir=reverse]
\addplot[]{func(x)};
\end{axis}
\end{tikzpicture}

enter image description here

  • @Bayesian And you can use a ternary operator to declare your function if you want, like this declare function={func(\x)=\x>.8?(6*\x/10):(\x>.6?(6*\x/9):(6*\x/8));} – Kpym Aug 18 '15 at 7:56

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.