1

I'm trying to graph \sin\frac{\pi}{x} in for my notes and pgf plots is giving me some issues. I've tried looking around but I haven't found a post that quite gave me what I need. This is what I have

\documentclass[12pt]{article}
\usepackage{fullpage}
\usepackage{amssymb}
\usepackage{verbatim,amsfonts, colonequals, amsthm, amsmath, mathrsfs, amscd, pgfplots,multicol,geometry}
\usepackage{amsmath,gensymb,tikz}

\pgfplotsset{my style/.append style={axis x line=middle, axis y line=
middle, xlabel={$x$}, ylabel={$y$}, axis equal }, trig format plots=rad}

\pgfplotsset{Axis Style/.style={
width=.5\textwidth, %height=5cm,
axis x line=center,
axis y line=middle,
samples=100,
ymin=-2.5, ymax=2.5,
xmin=-7.0, xmax=7.0,
domain=-2*pi:2*pi,
axis line style=<->,
}}

\geometry{textwidth=6.3in,textheight=9in}

\parindent=0in
\pagestyle{empty}

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=0.8]
\begin{axis}[my style, ymax=1, ymin=-1]
\addplot[domain=0:2*pi] {sin(x)};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

I would like to have x-ticks at pi/2, pi, 3pi/2, and 2pi. Also, I would like my ymin=-1 and ymax=1, but it doesn't seem to be recognizing that. Finally, whenever I try to graph sin(pi/x), I get compiling errors. Can someone point me in the right direction in fixing this?

5
  • 1
    The problem with sin(pi/x) is that your domain contains 0. Start with 0.1 e.g., it will work.
    – bmv
    Jan 19, 2018 at 18:16
  • 1
    What a simple fix! Any ideas about my axis markers and why the graph isn't setting the ymin and ymax as I have it in the code?
    – JohnC
    Jan 19, 2018 at 18:41
  • 1
    For your axis markers see tex.stackexchange.com/questions/23461/…
    – bmv
    Jan 19, 2018 at 20:07
  • @JohnC, did my answer answer you question or do you need further assistance? In the first case, please consider upvoting it (by clicking on the arrows next to the score) and/or marking it as the accepted answer (by clicking on the checkmark ✓). In the later case, please let us know what you are missing. Jan 21, 2018 at 20:54
  • I upvoted it but never accepted it. Thanks!
    – JohnC
    Jan 24, 2018 at 3:52

1 Answer 1

3

So you want to do something like the following? Please have a look at the comments in the code for details.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        my style/.append style={
            axis x line=middle,
            axis y line=middle,
            xlabel={$x$},
            ylabel={$y$},
            % changed from `axis equal' so that your given y limits are applied
            axis equal image,
            % -----------------------------------------------------------------
            % added stuff
            % -----------------------------------------------------------------
            % so the `ylabel' isn't colliding the graph
            ylabel style={
                anchor=south,
            },
            % so the line around x=2 isn't clipped near y=1
            clip=false,
            % so the axis is over the plot
            axis on top,
            % to avoid overshooting because of the step function near x=0
            line join=bevel,
            % to smooth the function(s)
            smooth,
            % -----------------------------------------------------------------
        },
        trig format plots=rad,
    }

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        my style,
        xmin=0,
        ymax=1,
        ymin=-1,
    ]
        % split the function in three domains so it looks continuous without
        % having to use "useless" point for bigger x values
        \addplot [
            red,
            domain=0.01:0.1,
            samples=501,
        ] {sin(pi/x)};
        \addplot [
            red,
            domain=0.1:1,
            samples=501,
        ] {sin(pi/x)};
        \addplot [
            red,
            domain=1:2*pi,
            samples=51,
            smooth,
        ] {sin(pi/x)};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

You must log in to answer this question.

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