![如何在 LaTeX 里面写出函数的和?](https://linux22.com/image/403572/%E5%A6%82%E4%BD%95%E5%9C%A8%20LaTeX%20%E9%87%8C%E9%9D%A2%E5%86%99%E5%87%BA%E5%87%BD%E6%95%B0%E7%9A%84%E5%92%8C%EF%BC%9F.png)
\documentclass[border=10pt,pstricks]{standalone}
\usepackage{pst-func,amsmath,amssymb}
\usepackage{xfp}
\begin{document}
\psset{xunit=1,yunit=2,linewidth=1pt}
\begin{pspicture}[plotpoints=500](-.5,-1.5)(12,2)
%
\def\numerator#1{\fpeval{(-1)^(#1)/(fact(#1)*fact(#1))}}
\def\function#1{(x/2)^(2*#1)}
\def\hihihaha#1{\numerator#1*\function#1}
%%
\psclip{\psframe[fillstyle=solid,fillcolor=white,linestyle=none](-.5,-1)(11,1.2)}
\psset{algebraic}
\psplot[linecolor=blue!50!orange]{0}{3}{\hihihaha{0}+\hihihaha{1}} % =2
\psplot[linecolor=red!50!orange]{0}{5}{\hihihaha{0}+\hihihaha{1}+\hihihaha{2}} % =4
\psplot[linecolor=blue!50!red]{0}{6}{\hihihaha{0}+\hihihaha{1}+\hihihaha{2}+\hihihaha{3}} %=6
\psplot[linecolor=blue!50!yellow]{0}{8}{\hihihaha{0}+\hihihaha{1}+\hihihaha{2}+\hihihaha{3}+\hihihaha{4}} %=8
\psplot[linecolor=pink!50!orange]{0}{10}{\hihihaha{0}+\hihihaha{1}+\hihihaha{2}+\hihihaha{3}+\hihihaha{4}+\hihihaha{5}} %=10
\psplot[linecolor=pink!50!cyan]{0}{12}{\hihihaha{0}+\hihihaha{1}+\hihihaha{2}+\hihihaha{3}+\hihihaha{4}+\hihihaha{5}+\hihihaha{6}} %=12
\psplot[linecolor=green!50!orange]{0}{14}{\hihihaha{0}+\hihihaha{1}+\hihihaha{2}+\hihihaha{3}+\hihihaha{4}+\hihihaha{5}+\hihihaha{6}+\hihihaha{7}} %=14
\psplot[linecolor=pink!50!red]{0}{16}{\hihihaha{0}+\hihihaha{1}+\hihihaha{2}+\hihihaha{3}+\hihihaha{4}+\hihihaha{5}+\hihihaha{6}+\hihihaha{7}+\hihihaha{8}} %=16
\psplot[linecolor=blue!50!green]{0}{18}{\hihihaha{0}+\hihihaha{1}+\hihihaha{2}+\hihihaha{3}+\hihihaha{4}+\hihihaha{5}+\hihihaha{6}+\hihihaha{7}+\hihihaha{8}+\hihihaha{9}} %=18
\psplot[linecolor=cyan!50]{0}{18}{\hihihaha{0}+\hihihaha{1}+\hihihaha{2}+\hihihaha{3}+\hihihaha{4}+\hihihaha{5}+\hihihaha{6}+\hihihaha{7}+\hihihaha{8}+\hihihaha{9}+\fpeval{1/(fact(10)*fact(10))}*x^20/\fpeval{2^20}} %=20
\endpsclip
\psBessel{0}{0}{10}%
\psaxes[showorigin=false,arrowinset=.2,arrowsize=.2,%
xsubticks=2,xsubticksize=1,ticksize=0 7pt,Dx=2]{->}(0,0)(-.5,-1.5)(11,2)
\rput(6,.75){$\displaystyle
J_0(x)=\sum_{k=0}^{\infty}\frac{(-1)^k \left(\frac{x}{2}\right)^{2k}}{k!\Gamma(k+1)}
$}%
\uput[-90](3,-1){$n=2$}
\uput[-90](4.2,-1){$n=6$}
\uput[-90](6,-1){$n=10$}
\uput[-90](7.7,-1){$n=14$}
\uput[-90](9.2,-1){$n=18$}
\uput[-90](10,-.2){$J_0(x)$}
%%%
\uput[90](4,1.2){$n=4$}
\uput[90](5.3,1.2){$n=8$}
\uput[90](6.8,1.2){$n=12$}
\uput[90](8.4,1.2){$n=16$}
\uput[90](10,1.2){$n=20$}
\end{pspicture}
\end{document}
问题:
- 如何
\hihihaha
通过递归重写?(这意味着我希望它更短) - 当然,如何
sum
在 LaTeX 中编写函数?(有任何建议)它的意思是 --with k=1, LaTeX 打印the content of \hihihaha{0}+\hihihaha{1}
,with k=2, LaTeX 打印the content of \hihihaha{0}+\hihihaha{1}+\hihihaha{2}
,... !
答案1
这里有一种方法可以做到——使用嵌套的\multido
:
\documentclass[border=10pt,pstricks]{standalone}
\usepackage{pst-func,xfp}
\begin{document}
\psset{xunit=1,yunit=2,linewidth=1pt}
\begin{pspicture}[plotpoints=500](-.5,-1.5)(12,2)
\psclip{\psframe[fillstyle=solid,fillcolor=white,linestyle=none](-.5,-1)(11,1.2)}
\psset{algebraic}
\colorlet{linecol1}{blue!50!orange}
\colorlet{linecol2}{red!50!orange}
\colorlet{linecol3}{blue!50!red}
\colorlet{linecol4}{blue!50!yellow}
\colorlet{linecol5}{pink!50!orange}
\colorlet{linecol6}{pink!50!cyan}
\colorlet{linecol7}{green!50!orange}
\colorlet{linecol8}{pink!50!red}
\colorlet{linecol9}{blue!50!green}
\colorlet{linecol10}{cyan!50}
\multido{\iA=2+1,\iL=3+2,\iC=1+1}{10}{%
\def\x{0 }%
\multido{\iB=0+1}{\iA}{%
\xdef\x{\x + \fpeval{(-1)^(\iB) / (fact(\iB) * fact(\iB))} * x^\inteval{2 * \iB} / \fpeval{2^(2 * \iB)}}
}%
\psplot[linecolor=linecol\iC]{0}{\iL}{\x}
}
\endpsclip
\psBessel{0}{0}{10}%
\psaxes[showorigin=false,arrowinset=.2,arrowsize=.2,%
xsubticks=2,xsubticksize=1,ticksize=0 7pt,Dx=2]{->}(0,0)(-.5,-1.5)(11,2)
\rput(6,.75){$\displaystyle
J_0(x) = \sum_{k = 0}^{\infty}\frac{(-1)^k \left( \frac{x}{2} \right)^{2 k}}{k! \Gamma(k + 1)}
$}%
\uput[-90](3,-1){$n = 2$}
\uput[-90](4.2,-1){$n = 6$}
\uput[-90](6,-1){$n = 10$}
\uput[-90](7.7,-1){$n = 14$}
\uput[-90](9.2,-1){$n = 18$}
\uput[-90](10,-.2){$J_0(x)$}
%%%
\uput[90](4,1.2){$n = 4$}
\uput[90](5.3,1.2){$n = 8$}
\uput[90](6.8,1.2){$n = 12$}
\uput[90](8.4,1.2){$n = 16$}
\uput[90](10,1.2){$n = 20$}
\end{pspicture}
\end{document}
变量定义包括:
\iA
(i
整数 2,...,11):每个函数中的元素数量\iL
(i
整数 3,...,21):x-max,据此评估函数\iC
(i
从 1 到 10 的整数):用于提取线条颜色linecol\iC
(也可以只使用\iA
并改变颜色linecolX
)\iB
(i
从 0 开始的整数,...,\iA
):扩展的内部术语序列以匹配指定的函数
外层\multido
初始化\x
并\psplot
为函数\x
。内层通过逐步添加第 项\multido
来构造。在 的每次迭代中,都会添加一项\x
\iB
\multido
\x
答案2
另一种方法是使用Asymptote
,自动计算图表的端点:
//
// sumfunc.asy
//
settings.tex="pdflatex";
import graph; import math; import palette;
size(14cm,6cm,IgnoreAspect);
import fontsize;defaultpen(fontsize(8pt));
texpreamble("\usepackage{lmodern}");
real xmin=0,xmax=10.2, ymin=-1,ymax=1;
real dxmin=0, dxmax=0.22, dymin=dxmax, dymax=dxmax;
xaxis(xmin-dxmin,xmax+dxmax,RightTicks(Step=1,step=0.5,OmitTick(0)),above=true);
yaxis(ymin-dymin,ymax+dymax,LeftTicks (Step=1,step=0.5),above=true);
pair f(real x){return (x,Jn(0,x));}
pair fn(real x, real n){
real a=1, s=1;
for(int k=1;k<=n;++k){
a*=-(x/2)^2/k^2; s+=a;
}
return (x,s);
}
typedef pair pairFreal(real);
pairFreal Fn(int n){return new pair(real x){return fn(x,n);};}
real[] yClip={-1.1,1.1};
guide[] gfn;
for(int i=0;i<10;++i){
guide g=graph(Fn(i+1),xmin,xmax);
g=firstcut(g,(xmin, yClip[i%2])--(xmax, yClip[i%2])).before;
gfn.push(g);
}
pen[] fnPen=Gradient(gfn.length+1,lightblue,darkblue);
guide gf=graph(f,xmin,xmax);
draw(gf,fnPen[fnPen.length-1]+1.2bp);
label("$J_0(x)$",relpoint(gf,1),plain.S);
for(int i=gfn.length-1;i>=0;--i){
draw(gfn[i],fnPen[i]+0.7bp);
label("$n=$"+string((i+1)*2),relpoint(gfn[i],1),(0,-(-1)^i));
}
label("$J_0(x)=\displaystyle\sum_{k=0}^{\infty}"+
"\frac{(-x^2)^k}{4^k(k!)^2}$",(5,0.6),UnFill);
答案3
这是我上面的评论。它表明,对于足够大的值,n
贝塞尔函数可以得到很好的近似。总和在循环中累积,该循环还将中间总和存储在可以在另一个循环中绘制的宏中。(原则上,一个循环足以完成所有事情,但问题是如何在 LaTeX 中定义总和。)幸运的是,pstricks 现在加载了pgffor
,所以我可以循环遍历你的颜色而无需任何额外的努力。
\documentclass[border=10pt,pstricks]{standalone}
\usepackage{pst-func,amsmath,amssymb}
\usepackage{xfp}
\begin{document}
\psset{xunit=1,yunit=2,linewidth=1pt}
\begin{pspicture}[plotpoints=500](-.5,-1.5)(13,2)
%
\def\numerator#1{\fpeval{(-1)^(#1)/(fact(#1)*fact(#1))}}
\def\function#1{(x/2)^(2*#1)}
\def\hihihaha#1{\numerator#1*\function#1}
\foreach \X in {0,...,20}
{\ifnum\X=0
\xdef\mysum{\expandafter\hihihaha{\X}}%
\else
\ifodd\X
\xdef\mysum{\mysum\expandafter\hihihaha{\X}}%
\else
\xdef\mysum{\mysum+\expandafter\hihihaha{\X}}%
\fi
\expandafter\xdef\csname mysum\romannumeral\X\endcsname{\mysum}
\fi}
\typeout{\mysumi}
%%
\psclip{\psframe[fillstyle=solid,fillcolor=white,linestyle=none](-.5,-1)(11,1.2)}
\psset{algebraic}
\foreach \Color [count=\X]in {blue!50!orange, %=2
red!50!orange, %=4
blue!50!red,%=6
blue!50!yellow,%=8
pink!50!orange,%=10
pink!50!cyan,%=12
green!50!orange,%=14
pink!50!red,%=16
blue!50!green,%=18
cyan!50}%=20
{\expandafter\psplot[linecolor=\Color]{0}{18}{\csname mysum\romannumeral\X\endcsname}}
\psplot[linecolor=red]{0}{18}{\mysum} %=20
\endpsclip
\psBessel{0}{0}{10}%
\psaxes[showorigin=false,arrowinset=.2,arrowsize=.2,%
xsubticks=2,xsubticksize=1,ticksize=0 7pt,Dx=2]{->}(0,0)(-.5,-1.5)(11,2)
\rput(6,.75){$\displaystyle
J_0(x)=\sum_{k=0}^{\infty}\frac{(-1)^k \left(\frac{x}{2}\right)^{2k}}{k!\Gamma(k+1)}
$}%
\uput[-90](3,-1){$n=2$}
\uput[-90](4.2,-1){$n=6$}
\uput[-90](6,-1){$n=10$}
\uput[-90](7.7,-1){$n=14$}
\uput[-90](9.2,-1){$n=18$}
\uput[0](10.8,-0.2){$n=40$}
\uput[-90](10,-.2){$J_0(x)$}
%%%
\uput[90](4,1.2){$n=4$}
\uput[90](5.3,1.2){$n=8$}
\uput[90](6.8,1.2){$n=12$}
\uput[90](8.4,1.2){$n=16$}
\uput[90](10,1.2){$n=20$}
\end{pspicture}
\end{document}
答案4
\documentclass[border=10pt,pstricks]{standalone}
\usepackage{pst-func,pst-math,amsmath,xfp}
\makeatletter
\def\myBessel{\@ifnextchar[{\myBessel@i}{\myBessel@i[]}}
\def\myBessel@i[#1]#2{{%%% #2 = kmax
\pst@killglue
\psset{plotpoints=500,#1}%
\psplot{0}{\inteval{#2+2}}{% #2=kmax
/Sum 1 def
1 1 #2 { /k exch def
-1 k exp x 2 div k dup add exp mul % -1^k (x/2)^(2k)
k 1 add GAMMA k tx@AddMathFunc begin ! end mul % denominator
Div
Sum add /Sum exch def
} for
Sum
}%
}\ignorespaces}
\makeatother
\begin{document}
\psset{yunit=2}
\begin{pspicture}[plotpoints=500](-.5,-1.5)(12,2)
\psclip{\psframe[linestyle=none](-.5,-1)(11,1.2)}
\multido{\iA=1+1,\iB=10+10}{10}{\myBessel[linewidth=1.5pt,linecolor=red!\iB]{\iA}}
\psBessel{0}{0}{10}%
\endpsclip
\psaxes[showorigin=false,arrowinset=.2,arrowsize=.2,%
xsubticks=2,xsubticksize=1,ticksize=0 7pt,Dx=2]{->}(0,0)(-.5,-1.5)(11,2)
\rput(6,.75){$\displaystyle
J_0(x)=\sum_{k=0}^{\infty}\frac{(-1)^k \left(\frac{x}{2}\right)^{2k}}{k!\Gamma(k+1)}$}%
\uput[-90](3,-1){$n=2$} \uput[-90](4.2,-1){$n=6$}
\uput[-90](6,-1){$n=10$} \uput[-90](7.7,-1){$n=14$}
\uput[-90](9.2,-1){$n=18$}\uput[-90](10,-.2){$J_0(x)$}
%%%
\uput[90](4,1.2){$n=4$} \uput[90](5.3,1.2){$n=8$}
\uput[90](6.8,1.2){$n=12$}\uput[90](8.4,1.2){$n=16$}
\uput[90](10,1.2){$n=20$}
\end{pspicture}
\end{document}