8
\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}

enter image description here

Question:

  1. How to rewrite \hihihaha by recursion? (It mean that I would to it is more shorter)
  2. Of course, how to write the sum of function inside LaTeX? (With any your suggestions) It mean that --with k=1, LaTeX print the content of \hihihaha{0}+\hihihaha{1},with k=2, LaTeX print the content of \hihihaha{0}+\hihihaha{1}+\hihihaha{2}, ... !
  • What do you mean by the sum of function? Of which function? – Bernard Oct 23 at 18:27
  • @Bernard Yes, J_0(x)=\sum_{k=0}^{\infty}\frac{(-1)^k \left(\frac{x}{2}\right)^{2k}}{k!\Gamma(k+1) :) – justonly Oct 23 at 18:29
  • 1
    I don't think pstricks can calculate sums of series. – Bernard Oct 23 at 18:32
  • @Bernard Oh, sorry, my idea is to print \stuff, example, with k=1, I have \hihihaha{0}+\hihihaha{1}, with k=2, I have \hihihaha{0}+\hihihaha{1}+\hihihaha{2},... . I would to LaTeX will print the content of series automatically ... – justonly Oct 23 at 18:37
  • 2
    The TikZ library math allows you to define recursions, see the example on p. 694 of pgfmanual v3.1.4. There are already a few examples on this site using this for plots. Of course, plain LaTeX does allow you to define things recursively, so there should be no problem. – Schrödinger's cat Oct 23 at 18:41
6

Here's one way of doing it - using nested \multidos:

enter image description here

\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}

The variable definitions include:

  • \iA (integer from 2,...,11): Number of elements in each function
  • \iL (integer from 3,...,21): x-max according to which to evaluate the function
  • \iC (integer from 1,...,10): Used to extract the line colours linecol\iC (could also just have used \iA and shifted the colours linecolX)
  • \iB (integer from 0,...,\iA): Internal sequence of terms expanded to match the specified function

The outer \multido initialized \x and \psplots the function \x. The inner \multido constructs \x by progressively adding a \iB-th term. Within each iteration of \multido, \x has a term added to it

  • Your answer is accepted. Very good for \multido. Thanks. – justonly Oct 24 at 8:08
  • Sorry but why does \def\x{0 }% have the white space? – justonly Oct 25 at 1:53
  • 1
    @justonly: You don't need it. I just wanted the compilation of \x to have spaces between the operators and operands. Before \psplot[..]{.}{.}{\x}, add \show\x to see what \x looks like (with and without the space). – Werner Oct 25 at 2:19
11

An alternative using Asymptote, with automatically calculated endpoints of the graphs :

//
// 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);

enter image description here

7

This is my comment from above. It shows that for large enough n the Bessel function gets well approximated. The sum gets accumulated in a loop, which also stores the intermediate sums in macros that can be plotted in another loop. (In principle one loop would be sufficient to do everything but the question is how to define the sum in LaTeX.) Luckily pstricks nowadays loads pgffor, so I can just loop over your colors without any additional efforts.

\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}

enter image description here

  • Yes, I know you will. :-) Thanks. – justonly Oct 24 at 0:24
3
\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}

enter image description here

  • Thanks for your PostScript code with new several things. :-) ... – justonly Oct 24 at 8:19
  • Off topic, assume that \psplot...{a0+a1*x+a2*x^2+...+an*x^n}, in PSTricks, the biggest value of n is ? – justonly Oct 24 at 11:30
  • Try it by yourself ... – user187802 Oct 24 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.