# How to plot atan(x), sqrt(x) using PSTricks?

For example, this code works:

\documentclass{minimal}

\usepackage{pstricks}
\usepackage{pst-plot}

\begin{document}

\begin{pspicture}(-5,-5)(5,5)
\psgrid[griddots=10,gridlabels=0pt, subgriddiv=0, gridcolor=black!20]
\psaxes(0,0)(-5,-5)(5,5)
\psplot[algebraic,xunit=0.5cm,linewidth=1pt]{-5}{5}{x*cos(x)}
\end{pspicture}

\end{document}


But putting atan(x), sqrt(x) (this list is not full I guess) instead of x*cos(x) gives me nothing at all.

-
For sqrt(x) you must ensure that the domain doesn't include negative numbers: \psplot[...]{0}{5}{sqrt(x)} works. The atan function in Postscript has two variables. – egreg Sep 25 '12 at 22:10
@Physicsworks: you can use Sqrt(x) with an uppercase S, then it returns 0 for negative values. – Herbert Sep 26 '12 at 10:21

The pst-math package provides the ATAN function for the inverse tangent function. The sqrt function has domain [0,\infty) which is why your code didn't work (you were supplying it with a domain of [-5,5])

\documentclass{article}

\usepackage{pst-plot}
\usepackage{pst-math}

\begin{document}

\psset{algebraic,unit=0.5cm,linewidth=1pt}

\begin{pspicture}(-5,-5)(5,5)
\psgrid[griddots=10,gridlabels=0pt, subgriddiv=0, gridcolor=black!20]
\psaxes(0,0)(-5,-5)(5,5)
\psplot{0}{5}{sqrt(x)}
\psplot[linecolor=red]{-5}{5}{ATAN(x)}
\end{pspicture}

\end{document}

-
 Oh, thanks, now atan(x) works. P.S. So it actually does matter if I go beyond the domain. – Physicsworks Sep 25 '12 at 22:22 @Physicsworks you're welcome :) It does matter if you go beyond the domain in both PSTricks and pgfplots. I think we're spoiled these days, as most CAS such as maple, mathematica let you get away with too much :) – cmhughes Sep 25 '12 at 22:24

it is also possible to draw the root function as a parametric plot, then you do not have to take care of negative values:

\documentclass{article}
\usepackage{pst-plot}
\usepackage{pst-math}
\begin{document}

\begin{pspicture}(-5,-3)(5,3)
\psgrid[griddots=10,gridlabels=0pt, subgriddiv=0, gridcolor=black!40]
\psaxes[axesstyle=frame,Dx=2,Dy=2](0,0)(-5,-3)(5,3)
\psset{algebraic,linewidth=1.5pt}
\psparametricplot{-2.2}{2.2}{t^2 | t}
\psplot[linecolor=red]{-5}{5}{ATAN(x)}
\end{pspicture}

\end{document}


-
 Is it possible to automatically simplify \frac{2\pi}{4} and move the negative sign to the most left in the vertical axis label in my answer? – Click Me Sep 26 '12 at 16:06

\documentclass[pstricks,border={0pt -5mm 0pt -1mm}]{standalone}
\usepackage{pst-plot,pst-math}
\usepackage[nomessages]{fp}
\FPeval\PiQ{round(pi/4:3)}

\psset{algebraic,plotpoints=100}

\newpsstyle{gridstyle}
{
gridwidth=0.4pt,% 0.8pt
gridcolor=red!30,% black
griddots=0,% 0
%=====================================
gridlabels=0pt,% 10pt
gridfont=NimbusSanL-Regu,
gridlabelcolor=blue!50,% black, must not use dvipsnames
%=====================================
subgriddiv=5,% 5
subgridwidth=0.2pt,% 0.4pt
subgridcolor=green!30,% gray
subgriddots=0,% 0
}

\begin{document}

\begin{pspicture*}[showgrid=bottom](-5.2,-\psPi)(5.7,\psPi)
\psplot[linecolor=blue]{0}{5}{sqrt(x)}
\psplot[linecolor=red]{-5}{5}{ATAN(x)}
\psaxes
[
ytrigLabels=true,
linewidth=\pslinewidth,
labelFontSize=\scriptscriptstyle,
tickcolor=gray,
ticksize=-1.5pt 1.5pt,
xlabelsep=3pt,
arrowscale=1,
trigLabelBase=4,
dy=\PiQ,% must come before yunit to avoid getting a strange output!
yunit=\PiQ,
]{->}(0,0)(-5.1,-\psPi)(5.3,\psPi)[$x$,0][$y$,90]
\end{pspicture*}

\end{document}

-

And because we're always encouraged to do things differently, here's a version that uses pgfplots. Note that this package uses degrees by default, so we need to convert to radians.

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{every axis/.append style={
axis x line=middle,    % put the x axis in the middle
axis y line=middle,    % put the y axis in the middle
axis line style={<->}, % arrows on the axis
xlabel={$x$},          % default put x on x-axis
ylabel={$y$},          % default put y on y-axis
}}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xmin=-5,xmax=5,
ymin=-5,ymax=5,
grid=both]