# What is the correct RPN expression of a piecewise-defined function passed to \psplot?

My objective is to pass a piecewise-defined function in RPN to \psplot. The complete code is given as follows. The function is sin x for x<0 and cos x for other x.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-\psPi,-2)(\psPi,2)
\psplot{Pi neg RadtoDeg}{Pi RadtoDeg}{x dup lt 0 {sin} {cos} ifelse}
\end{pspicture}
\end{document}


The code above does not compile. So what is the correct syntax?

## Edit

When I tried (based on David Carlisle's answer)

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{plotpoints=3000}
\begin{document}
\begin{pspicture}(-\psPi,-2)(\psPi,2)
\psplot{Pi neg RadtoDeg}{Pi RadtoDeg}{x dup 0 lt {sin} {cos} ifelse}
\end{pspicture}
\end{document}


I got an output (that is strange) as follows.

-

## 2 Answers

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}

\begin{document}
\begin{pspicture}(-\psPi,-2)(\psPi,2)
\psplot[plotpoints=1000]{Pi neg}{Pi}{x RadtoDeg dup 0 lt { sin } {cos} ifelse}
\end{pspicture}
\end{document}


or in algebraic mode:

\begin{pspicture}(-\psPi,-2)(\psPi,2)
\psplot[plotpoints=1000,algebraic]{Pi neg}{Pi}{IfTE(x<0,sin(x),cos(x))}
\end{pspicture}

IfTE(<expression>,<true>,<false>)


-
The source of confusion is: why must RadtoDeg be moved while you explained here (click) that appending RadtoDeg in both {Pi Neg} and {Pi} is necessary? –  stalking is prohibited Nov 7 '13 at 20:11
in a polarplot the x value must be in degrees. You need a RadtoDeg. But your function needs the x in radian, the reason why we have to convert it back. In algebraic mode the x values must be in radian. –  Herbert Nov 7 '13 at 20:27
I think the explanation (that x value is in degrees when using polarplot=true with postfix and x value is in radian when using plotplot=true with infix) is necessary in the pst-plot documentation as there is no such an explanation right now. –  stalking is prohibited Nov 12 '13 at 6:29
In the previous comment, plotplot is a typo, it should be polarplot. I am sorry for bothering you. –  stalking is prohibited Nov 12 '13 at 7:12
How to remove any vertical lines when plotting a piecewise function using IfTE? –  stalking is prohibited Jul 26 at 6:41

It's R PN :-)

    \psplot{Pi neg RadtoDeg}{Pi RadtoDeg}{x dup 0 lt {sin} {cos} ifelse}


0 lt not lt 0

-
@Marienplatz yes but I get similar if I just use x sin without any if block, it's a long time since I looked at ps-plot, is the synatx right? –  David Carlisle Nov 7 '13 at 19:22
@Marienplatz looks like you can, see this automatically linked question (in the list on the right) tex.stackexchange.com/questions/53948/… I think your scaling is out, as I say you similarly get a more or less straight line if you use just x sin –  David Carlisle Nov 7 '13 at 19:38