# Plotting sin(1/x) associated functions

I need to plot x, -x, x^2, -x^2, sin(1/x), x*sin(1/x), x^2*sin(1/x) and sin(1/x). But the functions containing sin(1/x) looks somehow bad. How can I fix it. I also do not know how to label the graphs (writing y=sin(1/x) next to the curve y=sin(1/x).

\documentclass{article}
\usepackage{pst-func}
\begin{document}
\begin{pspicture}*(-5,-2)(5,2)
\SpecialCoor % For label positionning
\psaxes[labels=y,Dx=\pstPI2]{->}(0,0)(-5,-2)(5,2)
\uput[-90](!PI 0){$\pi$} \uput[-90](!PI neg 0){$-\pi$} 5 \uput[-90](!PI 2 div 0){$\frac{\pi}2$}
\uput[-90](!PI 2 div neg 0){$-\frac{\pi}2$}
\psplot[linewidth=1.5pt,linecolor=blue,algebraic]{-5}{5}{sin(1/x)}
\psplot[linewidth=1.5pt,linecolor=red,algebraic]{-5}{5}{x*sin(1/x)}
\psplot[linewidth=1.5pt,linecolor=green,algebraic]{-5}{5}{x^2*sin(1/x)}
\psplot[algebraic,linestyle=dashed]{-5}{5}{x}
\psplot[algebraic,linestyle=dashed]{-5}{5}{-x}
\psplot[algebraic,linestyle=dashed]{-5}{5}{x^2}
\psplot[algebraic,linestyle=dashed]{-5}{5}{-x^2}
\end{pspicture}
\end{document}

-
Look up the usage of plotpoints in the pst-func manual. You need to give it some more points. – Thruston Mar 27 '14 at 22:56
Either that or stay FAR away from x=0. – John Kormylo Mar 28 '14 at 3:03
None of the suggestion helps. Still, the plot looks cramped. – user48793 Mar 28 '14 at 4:48
@user48793 I'm quite close to downvoting the question. You seriously remind me of companies wanting new web design: They do not know what they do want, and they do want it now. Please show a non-cramped image of the result and we can see what can be done. At this moment, I don's see anything wrong with either of the two answers. – yo' Mar 28 '14 at 10:43

I do not think that you get a better result with the current tools. The following uses always the same units for all functions:

\documentclass[pstricks, margin=5pt]{standalone}
\begin{document}

\def\xLeft{-0.5} \def\xRight{0.5}

\psset{xunit=8,yunit=2}
\begin{pspicture}(\xLeft,-1.2)(0.55,1.3)
labelFontSize=\scriptstyle,Dy=0.5]{->}(0,0)(\xLeft,-1.1)(\xRight,1.2)
\psset{algebraic,linewidth=0.5\pslinewidth}
\psplot[linestyle=dashed]{\xLeft}{\xRight}{x}
\psplot[linestyle=dashed]{\xLeft}{\xRight}{-x}
\psplot[linestyle=dashed]{\xLeft}{\xRight}{x^2}
\psplot[linestyle=dashed]{\xLeft}{\xRight}{-x^2}
%
\psplot[linecolor=blue,plotpoints=500]{\xLeft}{-0.07}{sin(1/x)}
\psplot[linecolor=blue,VarStep,VarStepEpsilon=1.e-8]{-0.07}{-0.001}{sin(1/x)}
\psplot[linecolor=blue,VarStep,VarStepEpsilon=1.e-8]{0.001}{0.07}{sin(1/x)}
\psplot[linecolor=blue,plotpoints=500]{0.07}{\xRight}{sin(1/x)}
%
\psplot[linecolor=red,VarStep,VarStepEpsilon=1.e-9]{\xLeft}{\xRight}{x*sin(1/x)}
%
\psplot[linecolor=green,VarStep,VarStepEpsilon=1.e-9]{\xLeft}{\xRight}{x^2*sin(1/x)}
\end{pspicture}
\end{document}


If you want it similar to what Spivak had, then use different units for the different curves (from the mathematical view it is wrong):

\documentclass[pstricks, margin=5pt]{standalone}
\usepackage{pst-plot}
\begin{document}
\def\xLeft{-0.5} \def\xRight{0.5}

\psset{xunit=8,yunit=2}
\begin{pspicture}(\xLeft,-1.2)(0.55,1.3)
labelFontSize=\scriptstyle,Dy=0.5]{->}(0,0)(\xLeft,-1.1)(\xRight,1.2)
\psset{algebraic,linewidth=0.5\pslinewidth}
%
\psplot[linecolor=blue!50,VarStep,VarStepEpsilon=1.e-8]{\xLeft}{-0.01}{sin(1/x)}
\psplot[linecolor=blue!50,VarStep,VarStepEpsilon=1.e-8]{0.01}{\xRight}{sin(1/x)}
%
\psplot[yunit=3,linecolor=red,VarStep,VarStepEpsilon=1.e-9]{\xLeft}{\xRight}{x*sin(1/x)}
\psplot[yunit=3,linestyle=dashed]{\xLeft}{\xRight}{x}
\psplot[yunit=3,linestyle=dashed]{\xLeft}{\xRight}{-x}
%
\psplot[yunit=8,linecolor=green,VarStep,VarStepEpsilon=1.e-9]{\xLeft}{\xRight}{x^2*sin(1/x)}
%
\psplot[yunit=8,linestyle=dashed]{\xLeft}{\xRight}{x^2}
\psplot[yunit=8,linestyle=dashed]{\xLeft}{\xRight}{-x^2}
\end{pspicture}
\end{document}


-
Obviously you know pstricks better than anyone else; if you say it can not be done, then that is a fact. Herbert any news about the new edition of your pstricks book in english? i was wondering if I should buy the 5th edition or wait for the new edition? – user48793 Mar 28 '14 at 10:44
there will be no edition in the future, means not before 2016 or maybe 17 or ... ;-) – Herbert Mar 28 '14 at 11:45
the x xin(1/x) plot looks very strange near (0,0), in both images. Perhaps the second one would look better simply drawing a filled red triangle (and its symmetric across the origin). – jfbu Mar 30 '14 at 6:57
sure, that's the problem with the oscillating equation. One can decrease the intervall as already done with the sin(1/x). – Herbert Mar 30 '14 at 9:18

For plotting those functions properly, you can use the VarStep parameter. The pstricks-add documentation even has an example for plotting sin(1/x) (Section 24.4 Sine of the inverse of x).

And you must split the plot for sin(1/x) in order to skip the 0:

\documentclass[pstricks, margin=5pt]{standalone}
\usepackage{pst-func}
\begin{document}
\begin{pspicture}*(-5,-2.2)(5,2)
\psaxes[labels=y,Dx=\pstPI2]{->}(0,0)(-5,-2)(5,2)
\uput[-90](!PI 0){$\pi$}\uput[-90](!PI neg 0){$-\pi$}\uput[-90](!PI 2 div 0){$\frac{\pi}2$}
\uput[-90](!PI 2 div neg 0){$-\frac{\pi}2$}
%
\psset{algebraic, VarStep, VarStepEpsilon=0.000001, linejoin=1}
%
\psplot[linestyle=dashed]{-5}{5}{x}
\psplot[linestyle=dashed]{-5}{5}{-x}
\psplot[linestyle=dashed]{-5}{5}{x^2}
\psplot[linestyle=dashed]{-5}{5}{-x^2}
%
\psplot[linecolor=blue]{-5}{-0.04}{sin(1/x)}
\psplot[linecolor=blue]{0.04}{5}{sin(1/x)}
%
\psplot[linecolor=red]{-5}{5}{x*sin(1/x)}
%
\psplot[linecolor=green]{-5}{5}{x^2*sin(1/x)}
\end{pspicture}
\end{document}


-
This still looks ugly. Your plot is too cramped. – user48793 Mar 28 '14 at 9:29
I guess in that case you'll need to plot a different function ;) Seriously, you could stretch the plot a bit, but that's it. – Christoph Mar 28 '14 at 9:39
In Spivak calculus, the same plot looks absolutely beautiful. I need to generate such plot. – user48793 Mar 28 '14 at 9:50
I don't have that book, can't help you there. If its only about the sin(1/x) near 0, then you can reduce the total domain, skip the plot for a larger domain around 0, e.g. use {-1.6}{-0.1}, stretch the image with xunit=3... – Christoph Mar 28 '14 at 9:57
@user48793: on which page is it printed? – Herbert Mar 28 '14 at 11:07

It is impossible to draw these curves because they oscillate infinitely to zero (in fact, they are the typical examples of continuous and differentiable functions that you can not draw). The best we can get is a graph in a range that does not contain zero.

Spivak's pictures show very good the behavior of the functions, but they are not accurate graphs. In addition, it is complicated to represent all these functions in the same picture, because these curves require different scales.

Moreover, significant points are not rational multiples of π, but its reciprocals, such as 1/π (because the sine function has period 2π, functions (x^n)\sin (1/x) make waves in intervals [1/(nπ),1/((n+2)π)]).

This is my solution (new version), using my package xpicture. We will draw our functions in intervals of type [1/(nπ),1/((n+1)π)].

In addition, we changed the aspect ratio between the axes, because the height of waves goes to zero very quickly.

\documentclass{standalone}
\usepackage{xpicture,ifthen}

\begin{document}

\COMPOSITIONfunction{\SINfunction}{\RECIPROCALfunction}{\F} % F(x)=sin(1/x)
\PRODUCTfunction{\IDENTITYfunction}{\F}{\G}                 % G(x)=x sin(1/x)
\PRODUCTfunction{\IDENTITYfunction}{\G}{\H}                 % H(x)=x^2sin(1/x)

% Command \grafic plots the three functions for x in [#1,#2]
\newcommand{\grafic}[2]{%
\pictcolor{blue}
\ifthenelse{\lengthtest{#1 pt > 0.064 pt}}{% the xpicture algorithm, applied to F(x)=sin x,
% fails for x<1/5\pi\approx 0.064
% because tangents are too vertical
\pictcolor{green}
\PlotFunction[12]\F{#1}{#2}
\PlotFunction[12]\F{-#2}{-#1}}{}
\pictcolor{blue}
\PlotFunction[12]\G{#1}{#2}
\PlotFunction[12]\G{-#2}{-#1}
\pictcolor{red}
\PlotFunction[12]\H{#1}{#2}
\PlotFunction[12]\H{-#2}{-#1}}

\setlength\unitlength{2cm}
\referencesystem(0,0)(5,0)(0,1)            % Change aspect ratio to 5:1

\fbox{\begin{Picture}(-1.1,-1.1)(1.1,1.1)
\cartesianaxes(-1,-1)(1,1)
\linethickness{1pt}
\pictcolor{cyan}
\PlotFunction{\IDENTITYfunction}{-1}{1}
\pictcolor{gray}
\PlotFunction{\SQUAREfunction}{-1}{1}
{\changereferencesystem(0,0)(1,0)(0,-1)    % This is a trick to draw -x and -x^2  without defining them.
\pictcolor{cyan}
\PlotFunction{\IDENTITYfunction}{-1}{1}
\pictcolor{gray}
\PlotFunction{\SQUAREfunction}{-1}{1}}
\newcounter{iteracio}
\setcounter{iteracio}{1}
\COPY1\maxim
\whiledo{\value{iteracio}<10}{%                % Loop to print functions between 1,1/\pi,1/2\pi,...
\MULTIPLY{\value{iteracio}}\numberPI\minim
\DIVIDE1\minim\minim
\grafic{\minim}{\maxim}
\COPY\minim\maxim
\stepcounter{iteracio}}
% Add tics in x-axis at 1/\pi, 2/\pi
\DIVIDE{1}{\numberPI}{\inversePI}
\DIVIDE{1}{\numberHALFPI}{\twoinversePI}
\printxticlabel{\inversePI}{1/\pi}
\printxticlabel{\twoinversePI}{2/\pi}
\end{Picture}}
\end{document}


-
Sorry if I got the maths wrong! I wasn't sure if '1/n\pi' meant \frac{1}{n}\pi or \frac{1}{n\pi} :-) – Joseph Wright Mar 29 '14 at 12:56
Thank you, Joseph. – Robert Fuster Mar 29 '14 at 13:02