function sin_expand_hermite(nmax,xmax) % Code is run 3 times with % Run-#1 nmax=10, xmax=9.5 % Run-#2 nmax=20, xmax=17 % Run-#3 nmax=40, xmax=32 % Changes by hand are required between runs to plot the 3 graphs - Fig. 4.4 k=1;s=1; degtorad=180/pi; x=[0:.1:xmax]; nx=length(x); % Hermite polynomials hp=[]; p1=ones(1,nx); %H_0 hp=[hp p1']; p2=2*s*x; %H_2 hp=[hp p2']; for n=1:2*nmax+1 p3=2*s*x.*p2-2*n*p1; %H_(n+1)=2xH_(n)-2H_(n-1) hp=[hp p3']; %H_n are the cols of hp matrix p1=p2;p2=p3; end % The columns of the matrix hp are the Hermite polynomials on the grid x rtpi=sqrt(pi); % The coefficients in the expansion of sin(x) in H_n(x) for np1=1:nmax n=np1-1; m=2*n+1; %odd m; 1,3,5..... c(m)=(-1)^n*(k/s)^m*exp(-(k/s)^2/4)/(2^m*factorial(m)); %coefficients end ss=zeros(1,nx)'; % Sum the series for np1=1:nmax n=np1-1;m=2*n+1; vec=hp(:,(m+1)); ss=ss+c(m)*vec; end % Plotting results - 1st run use 3,1,1; 2nd run 3,1,2 and then 3,1,3: subplot(3,1,3) plot(x(1:5:nx-5),ss(1:5:nx-5),'--ok','linewidth',1.5,... 'markersize',7,'markerfacecolor','k') hold on % To format the graphs % 1st run, xm=10, dx=1 % 2nd run, xm=18, dx=2 % 3rd run, xm=35, dx=5 xm=35; dx=5; x=[0:.1:xm] plot(x,sin(k*x),'-k','linewidth',1.5) set(gca,'FontSize',20) set(gca,'Ytick',[-2:1:1],'linewidth',1.6) set(gca,'Xtick',[0:dx:xm],'linewidth',1.6) axis([0 xm -2 1]) xlabel('$x$','Interpreter','LaTex','FontSize',20) ylabel('$\sin(x)$','Interpreter','LaTex','FontSize',20)