# psplot: square root of negative number

Is there a possibility to have psplot automatically ignore an invalid range?

Example:

x 2 sub sqrt


This cann't be plotted requesting \psplot{0}{10}, but only when using \psplot{2}{10}.

Of course my formula is more complicated than this and there can be the case that there are several intervals where there can be (negative number) sqrt.

On top of that I'm using \multido, so it would be really annoying if I would have to find every point where there would be the case of -1 sqrt...

What I would like, how it should behave in my opinion: It should only plot the real value of the function and skip the imaginary ones.

edit: MWE (not working because of the negative square root):

\documentclass[pstricks]{standalone}
\standaloneconfig{border=1cm}
\usepackage{pstricks,multido,pst-func}

\begin{document}
\begin{pspicture}(10,6)

\psaxes[labels=none]{->}(0,0)(10,6)
\multido{\n=1.1+.1}{20}{%
\psplot[linecolor=red,linewidth=.5mm]{0}{9}{5 2 3.1416 mul div \n\space 2 exp x 10 mul sin 2 exp mul 1 sub sqrt mul}
}

\end{pspicture}
\end{document}

• If you posted a MWE which shows how you're encountering this, it'd be much easier for us to help you. – A.Ellett Jun 19 '13 at 15:29
• Added MWE. I thought that this would not be needed, because my question is more general and not just on the solution of my specific problem. – MrD Jun 19 '13 at 15:41
• If you have not seen my update, this comment is given to notify you. – kiss my armpit Jun 20 '13 at 12:41
• That's what I was looking for. – MrD Jun 20 '13 at 12:56
• @DL6ER: there is also a Sqrt instead of sqrt. It gives 0 back for negative values, see my example. – user2478 Jun 20 '13 at 15:12

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\pstVerb{/mySQRT { dup 0 lt { 0 }{ sqrt } ifelse } bind def }%

\begin{document}
\begin{pspicture}(10,6)
\psaxes[labels=none]{->}(0,0)(10,6)
\multido{\n=1.1+.3}{20}{%
\psplot[linecolor=red]{0}{9}{5 2 Pi mul div \n\space 2 exp x 10 mul sin 2 exp mul 1 sub mySQRT mul}}
\end{pspicture}
\end{document}


## Edit:

I just got the following revelation.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\pstVerb{/mySQRT { dup 0 lt { neg sqrt neg }{ sqrt } ifelse } bind def }%
\psset{plotpoints=200}
\begin{document}
\begin{pspicture}(10,6)
\psclip{\psframe[linestyle=none,linewidth=0](9,6)}
\multido{\n=1.1+.3}{20}{%
\psplot[linecolor=red]{0}{9}{5 2 Pi mul div \n\space 2 exp x 10 mul sin 2 exp mul 1 sub mySQRT mul}}
\endpsclip
\psaxes[labels=none]{->}(0,0)(10,6)
\end{pspicture}
\end{document}


Note that the mySQRT can also be invoked in algebraic form as follows.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\pstVerb{/mySQRT { dup 0 lt { neg sqrt neg }{ sqrt } ifelse } bind def }%
\psset{plotpoints=200,algebraic}
\begin{document}
\begin{pspicture}(10,6)
\psclip{\psframe[linestyle=none,linewidth=0](9,6)}
\multido{\n=1.1+.3}{20}{%
\psplot[linecolor=red]{0}{9}{5*mySQRT((\n*sin(10*x/180*Pi))^2-1)/(2*Pi)}}
\endpsclip
\psaxes[labels=none]{->}(0,0)(10,6)
\end{pspicture}
\end{document}


But don't forget to convert degree to radian.

• This doesn't look nice...if there is no other solution I will go for gnuplot. – MrD Jun 19 '13 at 15:59
• @DL6ER: Herbert might be able to hack psplot to skip plotting the negative parts. I cannot do it. :-) – kiss my armpit Jun 19 '13 at 16:07
\documentclass[pstricks]{standalone}
\standaloneconfig{border=1cm}
\usepackage{pst-plot,multido}

\begin{document}
\psset{yunit=2}
\begin{pspicture}(10,3)
\psaxes[labels=none]{->}(0,0)(10,3)
\multido{\n=1.1+.1}{20}{%
\psplot[linecolor=red,linewidth=.5mm,plotpoints=500]{0}{9}{5 2 3.1416 mul div \n\space 2 exp x 10 mul sin 2 exp mul 1 sub
dup 0 lt % negative value??
{ pop pop 0 /L /moveto load def } % if yes then use moveto
{ sqrt mul /L /lineto load def }  % if not then go on
ifelse }%
}
\end{pspicture}
\end{document}


• +1: It will be nicer if you can also make it possible in algebraic. :-) – kiss my armpit Jun 20 '13 at 15:11
• This also looks nice – MrD Jun 20 '13 at 18:44
• multido is automatically loaded by pst-plot. – kiss my armpit Jun 22 '13 at 17:57