[Tex/LaTex] About \psplot and domain of functions

pst-plotpstricks

I would like to know if i really have to calculate the domain of functions like sqrt(x-7) and log(x-7)/log(2) and tell to \psplot to work only inside that interval, e.g., for the interval [6,16] and functions above for example, i'm getting error from ghostscript. But not for the code below.

\documentclass[12pt,pstricks,dvipsnames]{standalone}
\usepackage{pstricks-add}

\begin{document}

\def\azul{\color{Cerulean}}
\def\cinza{\color{gray}}

\psset{xunit=0.25cm,yunit=0.5cm,linecolor=Cerulean}

\begin{pspicture}(-4,-4.3)(20,4.4)
  \psaxes[Dx=7,
          Dy=3,
          labelFontSize=\azul,
          tickcolor=Cerulean]{->}(0,0)(-1.5,-3.7)(18.2,3.9)

  \psplot[algebraic,
          plotpoints=500,
          linecolor=blue,
          linewidth=1.5pt,
          yMinValue=-10,
          yMaxValue=10]{7.1}{16}{(log(x-7))/(log(2))}

  \psplot[algebraic,
          plotpoints=500,
          linecolor=green,
          linewidth=1.5pt,
          yMaxValue=5]{7.1}{16}{sqrt(x-7)}

\end{pspicture}

\end{document}

I ask because we can have a lot of work with complicated functions.

Best Answer

You cannot automatically determine the domain of arbitrary functions you plot. In some cases you can use special functions (in your case Log and Sqrt), which check the validity of their operand and return 0 otherwise. So, the plot isn't necessarily correct on all parts of the plot, but at least you don't get a Postscript error and you can check and correct the used domains for the respective functions.

With the algebraic option you can basically use any Postscript procedure which takes one number as input and evaluates to a number. As example, instead of using Log(x-7)/log(2) you can define your own function Foobar as follows:

\pstVerb{%
    tx@AddMathFunc begin % here is where the procedure must be defined
        /Foobar { Log 2 Log div } def
    end }%

As full example:

\documentclass[12pt,pstricks,dvipsnames]{standalone}
\usepackage{pstricks-add}
\begin{document}

\psset{xunit=0.25cm,yunit=0.5cm,linecolor=Cerulean}
\pstVerb{%
  tx@AddMathFunc begin
   /Foobar { Log 2 Log div } def
  end
}%
\begin{pspicture}(-4,-4.3)(20,4.4)
  \psaxes[Dx=7,
          Dy=3,
          tickcolor=Cerulean]{->}(0,0)(-1.5,-3.7)(18.2,3.9)

  \psplot[algebraic,
          plotpoints=500,
          linecolor=blue,
          linewidth=1.5pt,
          yMinValue=-10,
          yMaxValue=10]{6}{16}{(Log(x-7))/(log(2))}

  \psplot[algebraic,
          plotpoints=500,
          linecolor=green,
          linestyle=dotted,
          linewidth=1.5pt,
          yMaxValue=5]{6}{16}{Foobar(x-7)}
\end{pspicture}

\end{document}

enter image description here

So it is quite difficult to give a full list of possible functions, especially when taking into account the functions defined by various packages like pst-func, pst-math, etc.

Related Question