4

Im trying to plot the function y=atan(c^2/(x*sqrt(x^2+c^2/2)), but comparing the result with geogebra i think pgfplots has a issue or my code has an error

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
\begin{axis}[
  x=0.3cm,y=1.0cm,
  axis lines=middle,
  ymajorgrids=true,
  xmajorgrids=true,
  xmin=-15,
  xmax=15,
  ymin=-2.5,
  ymax=2.5,
  %xtick={-16.0,-15.0,...,16.0},
  %ytick={-8.0,-7.0,...,8.0},
]
%\clip(-16.03,-8.94) rectangle (16.03,8.94);
\draw[line width=4.pt] (-15.43,7.94) -- (-11.43,7.94);
\addplot [red,domain=-15:15,samples=41,]  {atan(\constante^2/(x*sqrt(\constante^2/2+x^2)))};
\end{axis}
\end{tikzpicture}

geogebra plot

enter image description here

pgfplots plot

enter image description here

Can you help me? please

gernot
  • 49,614

3 Answers3

8

This answer does incorporate the comment given by Max Snippe to "correct" the plot. But then you have two remaining problems.

  1. The connecting lines between the negative and positive part of the plot and
  2. the otherwise "ugly" looking plot because of the number of samples.

A solution for point 1 is given by percusse and point 2 could be fixed by stating a high number of samples (200 or above), maybe in combination with smooth.

But here I decided to use a non-linear spacing approach and utilizing the symmetry of the function to plot the function in two parts which also avoids the above two mentioned problems.

For details on how this solution works, please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[
    /pgf/declare function={
        c = 1;
        f(\x) = atan(c^2/(\x*sqrt(c^2/2 + \x^2)))/180*pi;
        % state lower and upper boundaries
        lb = 0.001;
        ub = 15;
        % -----------------------------------------------------------------
        %%% nonlinear spacing: <https://tex.stackexchange.com/a/373820/95441>
        % "non-linearity factor"
        a = 0.5;
        % function to use for the nonlinear spacing
        Y(\x) = exp(a*\x);
        % rescale to former limits
        X(\x) = (Y(\x) - Y(lb))/(Y(ub) - Y(lb)) * (ub - lb) + lb;
        % -----------------------------------------------------------------
    },
]
    \begin{axis}[
        x=0.3cm,
        y=1.0cm,
        axis lines=middle,
        ymajorgrids=true,
        xmajorgrids=true,
        % use limits already stated above
        xmin=-ub,
        xmax=ub,
        ymin=-2.5,
        ymax=2.5,
        % the default number of samples is sufficient
        samples=25,
        % make the plot smooth
        smooth,
%        no markers,     % <-- uncomment this line to not show markers
    ]
        %\clip(-16.03,-8.94) rectangle (16.03,8.94);
        % (not sure what is this good for ...)
        \draw [line width=4.pt] (-15.43,7.94) -- (-11.43,7.94);

        % draw the positive part of the function
        % and "forget" it to not  increase the `cycle list index'
        \addplot+ [domain=lb:ub,forget plot]
            ({X(x)}, {f(X(x))});
        % draw the negative part of the function
        % utilizing the point symmetry of the function
        \addplot+ [domain=lb:ub]
            ({-X(x)}, {-f(X(x))});
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
  • 1
    Perfect (+1) ! My initial comment was wrong, not the argument is in degrees, but what atan() returns is in degrees of course. Another way of fixing this is setting trig format plots=rad, which might be more intuitive to use as a global solution. – Max Jul 29 '18 at 20:11
  • 2
    This is of course also possible, i.e. instead of adding /180*pi to the function one could add trig format=rad or trig format plots=rad to the axis options. – Stefan Pinnow Jul 29 '18 at 20:18
3

After adding the preamble, the GeoGebra output is more or less reproduced.

EDIT: This is now more or less a percusse-Marijn answer, if any of the two adds a solution I will be happy to retract mine. Thanks!

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{arrows}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
\begin{axis}[
x=0.3cm,y=1.0cm,
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=-15,
xmax=15,
ymin=-2.5,
ymax=2.5,
 x filter/.expression={
        abs(x)<0.3 ? nan : x
},
restrict y to domain=-2.5:2.5,
]
\draw[line width=4.pt] (-15.43,7.94) -- (-11.43,7.94);
\pgfmathsetmacro{\constante}{1}
\addplot [red,domain=-15:15,samples=41]
{atan(\constante^2/(x*sqrt(\constante^2/2+x^2)))};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    The problem seems to be the vertical line in the middle of the plot, I think the OP would like to have two disconnected lines. – Marijn Jul 29 '18 at 18:48
  • 3
    restrict y to domain=-3:3 should do it – percusse Jul 29 '18 at 18:52
  • I think it is about the form of the graph. You can see in the tikz plot that the graph seems to diverge for x->0, whereas it takes the values+1.5 and -1.5 in the Geogebra plot for x->0. – Tom Jul 29 '18 at 18:55
0

Max Snippe responds my question, pgfplots is not in radians, I should add

\addplot [red,domain=-15:15,samples=41]{atan(\constante^2/(x*sqrt(\constante^2/2+x^2))/180*pi)};

to fix the problem.

  • I don't see how this code reproduces the form of the Geogebra plot. The Geogebra plot has (correctly) y(0) = 1.5, whereas the plot of your answer certainly does not have y(0) = 1.5. Can you explain this? – Tom Jul 29 '18 at 19:25