function hermite(npoly) hold off format long e % Define the grid on [-xmax,xmax] xmax=10; ngrid=501; dx=2*xmax/(ngrid-1); x=[-xmax:dx:xmax]; wtfcn=exp(-x.*x); srwtfcn=sqrt(wtfcn); length(x); % Calculate Hermite quadrature points xn=[1:1:npoly]; rtb=sqrt(xn/2); rtb(npoly)=[]; t=diag(rtb,-1)+diag(rtb,1); [f,lambda]=eig(t); pt=diag(lambda); % pt(i) are the quadrature points zero2=0*ones(1,ngrid); %To draw dashed line at y = 0 a=[]; gam=sqrt(pi/4); srgam=sqrt(gam); rtpi=sqrt(pi); h1=ones(1,ngrid); a=[a h1']; h2=2*x; a=[a h2']; % First two Hermite polynomials H_0 = 1 and H_1 = 2x are defined and % stored as the columns of the matrix a - for np1=2:npoly n=np1-1; %Recurrence relation h3=2*(x.*h2) - 2*n*h1; a=[a h3']; h1=h2; h2=h3; end npt=length(pt); zero=0*ones(1,npt); hermpolyn=exp(-x.*x/2)'.*a(:,npoly+1); % Normalize to maximum value on the interval cmax=max(hermpolyn); hermpolyn=hermpolyn/cmax; % Concatenate the two string variables to print the value of N on the graph strn1=num2str(npoly); strn2={'$n = $'}; strn=strcat(strn2,strn1); plot(x,hermpolyn,'-k','linewidth',1.6) hold on plot(pt,zero,'ok','markersize',8,'markerfacecolor','k') hold on set(gca,'FontSize',20) plot(x,zero2,'--k','linewidth',1.6) hold on text(-7,-1,0,strn,'Interpreter','latex','fontsize',20) axis([-xmax xmax -1.2,1]) set(gca,'Ytick',[-1.2:0.4:1],'linewidth',1.6) set(gca,'Xtick',[-xmax:5:xmax],'linewidth',1.6) xlabel('$x$','Interpreter','LaTex','FontSize',20) ylabel('$e^{-(x^2/2)}H_{n}(x)$','Interpreter','LaTex','FontSize',20) end