0

I am trying to plot $\sin(\pi x)$ and $\frac{2\pi}{\sin(\pi x)}$ in the range (1/2,3/2).

Here is the code I am using.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis} [axis lines=center]
    \addplot [domain=1/2:3/2, smooth, thick] {(sin(deg(pi*x)) };
  \end{axis}
\end{tikzpicture}

\begin{tikzpicture} \begin{axis} [axis lines=center] \addplot [domain=1/2:3/2, smooth, thick] {(2pi)/(sin(deg(pix)) }; \end{axis} \end{tikzpicture} \end{document}

I get the right plot for the first one but wrong one for the second one.

What am I doing wrong?

Are there better ways of doing this?

Addendum:

I tried Taylor expanding the function around $x=1$ and tried plotting. Again, it does not give me the right plot whereas plotting in Mathematica, this function also gives me the right plot.

\begin{tikzpicture}
  \begin{axis} [axis lines=center]
    \addplot [domain=1/2:3/2, smooth, thick] {3.28987-3.28987*x+2/(x-1) };
  \end{axis}
\end{tikzpicture}
  • The other possibility is to split your plots around the asymptote to get domain=1/2:.9 and domain=1.1:3/2. – Teepeemm Mar 29 '23 at 01:57

2 Answers2

0

I think there are 2 mistakes: your domain and your Taylor expansion. I had trouble following your explanation for the third graph when you talked about Mathematica plotting it correctly but not giving us a picture of Mathematica's correct plot. The problem I noticed is that you said the domain is 1/2 to 3/2 but this is not correct: 1 is not part of the domain since sin(pi)=0 . The solution is to break the plot into 2 pieces to avoid 1:

\documentclass[11pt]{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis} [axis lines=center]
    \addplot [domain=1/2:.99, smooth, thick] {(2*pi)/(sin(deg(pi*x)) };
    \addplot [domain=1.01:3/2, smooth, thick] {(2*pi)/(sin(deg(pi*x)) };
  \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
  \begin{axis} [axis lines=center]
    \addplot [domain=1/2:.99,, smooth, thick] {3.28987-3.28987*x+2/(x-1) };
    \addplot [domain=1.01:3/2, smooth, thick] {3.28987-3.28987*x+2/(x-1) };
  \end{axis}
\end{tikzpicture}
\end{document}

The result is shown below. enter image description here

Both plots don't agree now but they look quite similar. So I went to a Sage Cell Server and had it calculate a Taylor polynomial. The screenshot was:

enter image description here

Changing the plot for the Taylor polynomial and plotting it gives: enter image description here

Since 1/sin(x)=csc(x) and the derivative of csc(x)=-csc(x)cot(x), I suspect there is a sign problem in your Taylor expansion: you have +2/(x-1) and Sage gives -2/(x-1).

DJP
  • 12,451
0

Edit:
You should be aware that `pgfplots is not dedicated math programs as are Mathematica, Matlab and others, but is powerful tool for drawing of diagrams with (La)Tex.

To downvoter: I don't see any rational explanation what is wrong with my answer, for doing this. What I missed in it? Any explanation from anyone is welcome!

For the first two diagrams in your question try the following:

\documentclass[margin=3mm]{standalone}
 \usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document} \pgfplotsset{ % common axis settings trig format=rad, % <--- added

axis lines=center,
enlarge x limits = {0.1, upper},% &lt;--- added
enlarge y limits = 0.1,         % &lt;--- added
    xtick={4*pi/6,5*pi/6,...,9*pi/6},         % &lt;--- added
    xticklabels={$4\pi/6$,$5\pi/6$,$\pi$, 
                 $7\pi/6$,$8\pi/6$,$4\pi/3$}, % &lt;--- added
%    xticklabels={120,150,180,210,240},
    xticklabel style={minimum height=3.3ex, 
                      font=\footnotesize},    % &lt;--- added
samples = 200,                  %   &lt;------ increased number of sample
no marks,
restrict y to domain=-100:100,  %   &lt;------
every axis plot post/.append style={very thick},  % &lt;--- added
        }

\begin{tikzpicture}[baseline] % both pictures are aligned to baseline \begin{axis} \addplot +[domain=pi/2:3*pi/2] { sin(x) }; \end{axis} \end{tikzpicture}

\begin{tikzpicture}[baseline] \begin{axis}[ extra x ticks={pi}, extra tick style={grid=major, densely dashed, xticklabel={\empty}}, ]
\addplot +[domain=pi/2:3*pi/2] { 2*pi/sin(x) }; \end{axis} \end{tikzpicture} \end{document}

Main changes in the above code in comparison to code of your MWE are:

  • Removed smooth option.
  • Increased is number of samples. Ba this is increased accuracy of function calculations.
  • To the code for the second diagram is added restrict y to domain=-100:100 for restricting calculation of function until its values are smaller than set y limits (-100,+100). By this its calculation closer to x = 1 is skipped.
  • Since calculation of functions consider argument in radians, xtick labels are in radians too, however you can changes them to degrees (as do code commented code line below xtickclabels = ...) just uncomment code line xticklabels={120,150,180,210,240}.

enter image description here

For diagram added in addendum in your question is not entirely clear, what is problem with it and what you expect that it should present. It is actually shifted and inverted second diagram. Anyway, added to proposed MWE in form:

\begin{tikzpicture}[baseline]
  \begin{axis}[
        extra x ticks={1}, % for marking function pol
        %extra x ticks label={1}, 
        extra tick style={grid=major, densely dashed},
              ]
    \addplot +[domain=1/2:3/2] {3.28987-3.28987*x+2/(x-1) };
  \end{axis}
\end{tikzpicture}

gives the following result:

enter image description here

I this what you after?

Zarko
  • 296,517