答案1
您还可以使用 LuaJITTeX(以及 LuaTeX ≥ 1.0.3)中的 FFI 直接访问 Bessel 函数libm
。性能和准确性简直令人惊叹!
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\directlua{
ffi=require("ffi")
ffi.cdef[[
double jn(int n, double x);
double yn(int n, double x);
]]
}
\pgfmathdeclarefunction{BesselJ}{2}{%
\edef\pgfmathresult{%
\directlua{tex.print(ffi.C.jn(\pgfmathfloatvalueof{#1},\pgfmathfloatvalueof{#2}))}%
}%
}
\pgfmathdeclarefunction{BesselY}{2}{%
\edef\pgfmathresult{%
\directlua{tex.print(ffi.C.yn(\pgfmathfloatvalueof{#1},\pgfmathfloatvalueof{#2}))}%
}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
cycle list={[samples of colormap={6 of viridis}]},
enlargelimits=false,
domain=0:20,
restrict y to domain=-2:1,
ymin=-1,
samples=100,
no markers]
\pgfplotsinvokeforeach{0,...,2}{
\addplot { BesselJ(#1,x) }; \addlegendentry{$J_{#1}(x)$};
}
\pgfplotsinvokeforeach{0,...,2}{
\addplot { BesselY(#1,x) }; \addlegendentry{$Y_{#1}(x)$};
}
\end{axis}
\end{tikzpicture}
\end{document}
答案2
如果您不局限于使用 TiKZ,可以通过包和宏pstricks
绘制所有阶的贝塞尔函数和一阶修正贝塞尔函数。请参阅 pst-func 文档的 §§4-5。pst-func
\psBessel
\psModBessel
答案3
使用xelatex
或运行latex->dvips->ps2pdf
:
\documentclass{scrbook}
\usepackage{pst-func}
\begin{document}
\psset{xunit=0.25,yunit=5}
\begin{pspicture}(-30,-.85)(30,1.25)
\rput(13,0.8){%
$\displaystyle J_n(x)=\frac{1}{\pi}\int_0^\pi\cos(x\sin t-nt)\mathrm{d}t$}
\psaxes[Dy=0.2,Dx=4]{->}(0,0)(-30,-.8)(30,1.2)
\psset{linewidth=1pt}
\psBessel[linecolor=red]{0}{-28}{28}%
\psBessel[linecolor=blue]{1}{-28}{28}%
\psBessel[linecolor=green]{2}{-28}{28}%
\psBessel[linecolor=magenta]{3}{-28}{28}%
\end{pspicture}
\end{document}
答案4
% !TEX program = LuaLaTeX
% !Mode:: "TeX:UTF-8"
\documentclass[12pt]{article}
\usepackage{luacode}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
%%---------------------------
\begin{luacode*}
local kner = function(n,x,t)
return math.cos(x*math.sin(t)-n*t)
end
local bessel = function(m,n,x)
local summ =0.0
local pi=3.1415
for k=1, m do
tk=k*pi/m
summ = summ+kner(n,x,tk)
end
return summ/m
end
thirddata = thirddata or {}
thirddata.bessel = function(m,n,x)
return tex.print(bessel(m,n,x))
end
\end{luacode*}
%%---------------------------
\begin{document}
\begin{figure}[pb]
\centering
\pgfplotsset{width=15cm, height=7cm}
\begin{tikzpicture}
[declare function={
bessel(\m,\n,\x) = \directlua{thirddata.bessel(\m,\n,\x)};
}]
\begin{axis}[
use fpu=false,
xmin=-30, xmax=30,
ymin=-1, ymax=1.2,
minor x tick num=4,
minor y tick num=4,
axis lines = middle,
]
\addplot[red,thick,domain=-28:28,samples=1000] (x,{bessel(200,0,x)});
\addlegendentry{ $ J_{0}(x)$}
\addplot[green,thick,domain=-28:28,samples=1000] (x,{bessel(200,1,x)});
\addlegendentry{ $ J_{1}(x)$}
\addplot[blue,thick,domain=-28:28,samples=1000] (x,{bessel(200,2,x)});
\addlegendentry{ $ J_{2}(x)$}
\addplot[yellow,thick,domain=-28:28,samples=1000] (x,{bessel(200,3,x)});
\addlegendentry{ $ J_{3}(x)$}
\end{axis}
\end{tikzpicture}
\caption{ $J_n(x)=\frac{1}{\pi}\int_0^\pi\cos(x\sin t-nt)\mathrm{d}t$ }
\end{figure}
\[ J_n(x)=\frac{1}{\pi}\int_0^\pi\cos(x\sin t-nt)\mathrm{d}t \]
We use the composite trapezoidal Rule to approximate this definite integral by a sum of finite terms
\[ J_n(x)=\frac{1}{\pi}\int_0^\pi\cos(x\sin t-nt)\mathrm{d}t \approx \frac{1}{m}\sum_{k=1}^{m} \cos(x \sin t_k-n t_k)\]
where $t_k=k \frac{\pi}{m}$
\end{document}