6

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!

1 Answer 1

8

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

1
  • @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));} Commented Aug 18, 2015 at 7:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.