Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

enter image description here

share|improve this question

2 Answers 2

up vote 6 down vote accepted
\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>)

enter image description here

share|improve this answer
    
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

share|improve this answer
    
@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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.