# Plot a function for a range of values

How do I use pspicture to plot the function y = a sin(bx) for a = 1,2,3,4 and b = 1,2,3,4 over -2π ≤ x ≤ 2π? I have tried the following code but it is very tedious.

\documentclass[a4paper]{article}
\usepackage{pstricks,amsmath,amssymb,pst-plot,pst-math}

\begin{document}
\begin{pspicture}(-5,-2)(5,5)
\psaxes(0,0)(-5,-2)(5,5)
\psplot[algebraic,plotpoints=200,linecolor=violet]{-6.28319}{6.28319}{sin(x)}
\psplot[algebraic,plotpoints=200,linecolor=blue]{-6.28319}{6.28319}{2*sin(x)}
\psplot[algebraic,plotpoints=200,linecolor=green]{-6.28319}{6.28319}{3*sin(x)}
\psplot[algebraic,plotpoints=200,linecolor=red]{-6.28319}{6.28319}{4*sin(x)}
\psplot[algebraic,plotpoints=200,linecolor=violet]{-6.28319}{6.28319}{sin(2*x)}
\psplot[algebraic,plotpoints=200,linecolor=blue]{-6.28319}{6.28319}{2*sin(2*x)}
\psplot[algebraic,plotpoints=200,linecolor=green]{-6.28319}{6.28319}{3*sin(2*x)}
\psplot[algebraic,plotpoints=200,linecolor=red]{-6.28319}{6.28319}{4*sin(2*x)}
\psplot[algebraic,plotpoints=200,linecolor=violet]{-6.28319}{6.28319}{sin(3*x)}
\psplot[algebraic,plotpoints=200,linecolor=blue]{-6.28319}{6.28319}{2*sin(3*x)}
\psplot[algebraic,plotpoints=200,linecolor=green]{-6.28319}{6.28319}{3*sin(3*x)}
\psplot[algebraic,plotpoints=200,linecolor=red]{-6.28319}{6.28319}{4*sin(3*x)}
\psplot[algebraic,plotpoints=200,linecolor=violet]{-6.28319}{6.28319}{sin(4*x)}
\psplot[algebraic,plotpoints=200,linecolor=blue]{-6.28319}{6.28319}{2*sin(4*x)}
\psplot[algebraic,plotpoints=200,linecolor=green]{-6.28319}{6.28319}{3*sin(4*x)}
\psplot[algebraic,plotpoints=200,linecolor=red]{-6.28319}{6.28319}{4*sin(4*x)}
\end{pspicture}
\end{document}


This is what I get. I don't know if this is correct though. Is there a simpler way to plot the function for a range of values using pspicture and psplot?

## 2 Answers

\documentclass[a4paper]{article}
\usepackage{pstricks,pst-plot,multido}

\begin{document}
\begin{pspicture}(-7,-4)(7,5)
\psaxes(0,0)(-6.5,-4)(6.5,5)
\psset{plotpoints=200,algebraic}
\multido{\iA=1+1}{4}{%
\psplot[linecolor=violet]{-6.28319}{6.28319}{sin(\iA*x)}%
\psplot[linecolor=blue]{-6.28319}{6.28319}{2*sin(\iA*x)}%
\psplot[linecolor=green]{-6.28319}{6.28319}{3*sin(\iA*x)}
\psplot[linecolor=red]{-6.28319}{6.28319}{4*sin(\iA*x)}}
\end{pspicture}
\end{document}


There are TwoPi (in Postscript) and \psPiTwo (in TeX) to replace a literal 6.28319.

\documentclass[pstricks]{standalone}
\usepackage{pst-plot,multido}
\psset{plotpoints=1000,algebraic}
\begin{document}
\begin{pspicture}(-6.5,-4.5)(6.5,4.5)
\psframe*(-6.5,-4.5)(6.5,4.5)
\multido{\iA=1+1}{4}{%
\psplot[linecolor=white]{TwoPi neg}{TwoPi}{sin(\iA*x)}
\psplot[linecolor=blue]{TwoPi neg}{TwoPi}{2*sin(\iA*x)}
\psplot[linecolor=green]{TwoPi neg}{TwoPi}{3*sin(\iA*x)}
\psplot[linecolor=red]{TwoPi neg}{TwoPi}{4*sin(\iA*x)}}
\color{lightgray}%
\psaxes[linecolor=lightgray](0,0)(-\psPiTwo,-4.2)(\psPiTwo,4.2)
\end{pspicture}
\end{document}