我试图绘制函数y=atan(c^2/(x*sqrt(x^2+c^2/2))
,但将结果与 geogebra 进行比较,我认为 pgfplots 有问题或我的代码有错误
\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 图
pgfplots 图
你能帮我吗
答案1
这个答案确实包含了马克斯·斯尼普来“修正”情节。但你还有两个问题。
- 情节的正反两部分之间的连接线和
- 由于样本数量的原因,该图看上去很“丑陋”。
点 1 的解决方案如下敲击并且可以通过说明较高的数字samples
(200 或以上)来解决第 2 点,也许可以与 结合使用smooth
。
但在这里我决定使用非线性间距方法并利用函数的对称性将函数绘制成两部分,这也避免了上述两个问题。
有关此解决方案如何工作的详细信息,请查看代码中的注释。
% 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}
答案2
添加序言后,GeoGebra 输出或多或少被重现。
编辑:这现在或多或少是一个 percusse-Marijn 的答案,如果两者中的任何一个增加了解决方案,我很乐意撤回我的答案。谢谢!
\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}
答案3
Max Snippe 回答了我的问题,pgfplots 不是弧度,我应该补充一下
\addplot [red,domain=-15:15,samples=41]{atan(\constante^2/(x*sqrt(\constante^2/2+x^2))/180*pi)};
来修复这个问题。