13

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.

2
  • 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, 2012 at 22:10
  • 2
    @Physicsworks: you can use Sqrt(x) with an uppercase S, then it returns 0 for negative values.
    – user2478
    Sep 26, 2012 at 10:21

4 Answers 4

13

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])

enter image description here

\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}
3
  • Oh, thanks, now atan(x) works. P.S. So it actually does matter if I go beyond the domain. Sep 25, 2012 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, 2012 at 22:24
  • @cmhughes Hello how does one plot cube root function. Like from where did you know sqrt(x) is the command to be given for square function?? Thanks
    – C.S.
    Jul 4, 2015 at 11:43
7

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}

enter image description here

1
  • 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? Sep 26, 2012 at 16:06
5
\documentclass[pstricks,border=0pt,12pt,dvipsnames]{standalone}
\usepackage{amsmath}
\usepackage{pst-plot}
\usepackage{pst-math}
\usepackage[nomessages]{fp}

\FPeval\XMin{0-5}
\FPeval\XMax{5}
\FPeval\YMin{0-pi}
\FPeval\YMax{pi}

\FPeval\XOL{0-1/3} % of DeltaX
\FPeval\XOR{1/3} % of DeltaX
\FPeval\YOB{0-1/3} % of DeltaY
\FPeval\YOT{1/3} % of DeltaY

\FPset\TrigLabelBase{4}
\FPeval\DeltaX{1}
\FPeval\DeltaY{pi/TrigLabelBase}

\FPeval\AxisL{XMin+DeltaX*XOL}
\FPeval\AxisR{XMax+DeltaX*XOR}
\FPeval\AxisB{YMin+DeltaY*YOB}
\FPeval\AxisT{YMax+DeltaY*YOT}

\newlength\Width\Width=12cm
\newlength\Height\Height=8cm

\newlength\llx\llx=-5pt
\newlength\urx\urx=15pt
\newlength\lly\lly=-5pt
\newlength\ury\ury=15pt


\psset
{
    llx=\llx,
    lly=\lly,
    urx=\urx,
    ury=\ury,
    xtrigLabels=false,
    ytrigLabels=true,
    trigLabelBase=\TrigLabelBase,
    labelFontSize=\scriptstyle,
    xAxisLabel=$x$,
    yAxisLabel=$y$,
    algebraic,
    plotpoints=500,
}

\def\f{ATAN(x)}
\def\g{sqrt(x)}

\begin{document}
\pslegend[lt]{%
    \color{NavyBlue}\rule{12pt}{1pt} & \color{NavyBlue} $y=\tan^{-1} x$\\
    \color{Red}\rule{12pt}{1pt} & \color{Red} $y=\sqrt x$
}
\begin{psgraph}
    [
        dx=\DeltaX,
        dy=\DeltaY,
        linecolor=lightgray,
        tickcolor=gray,
        ticksize=-3pt 3pt,
        axespos=top,
    ]{<->}(0,0)(\AxisL,\AxisB)(\AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}%{\dimexpr\Height-\ury+\lly}
    \psaxes
    [
        dx=\DeltaX,
        dy=\DeltaY,
        labels=none,
        subticks=5,
        tickwidth=.4pt,
        subtickwidth=.2pt,
        tickcolor=Orange!20,
        subtickcolor=ForestGreen!20,
        xticksize=\YMin\space \YMax,
        yticksize=\XMin\space \XMax,
        subticksize=1,
    ](0,0)(\XMin,\YMin)(\XMax,\YMax)
    \psplot[linecolor=NavyBlue]{-5}{5}{\f}
    \psplot[linecolor=Red]{0}{5}{\g}
\end{psgraph}
\end{document}

enter image description here

Documentation

enter image description here

Need to load pst-math as pst-plot has only defined the following functions.

  • sin, cos, tan, acos, asin are in radians
  • log, ln
  • ceiling, floor, truncate, round
  • sqrt (square root)
  • abs (absolute value)
  • fact (factorial)
  • Sum
  • IfTE (case structure)
4

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.

screenshot

\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]
            \addplot[red]expression[domain=0:5]{sqrt(x)};
            \addplot[blue]expression[domain=-5:5]{rad(atan(x))};
    \end{axis}
\end{tikzpicture}

\end{document}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .