function legendre_with_pts(npoly) hold off format long e % Define the grid on [-xmax,xmax] xmax=1; ngrid=501; dx=2*xmax/(ngrid-1); x=[-1:dx:1]; % Calculate Legendre quadrature points xn=[0:1:npoly-1];xnsq=xn.*xn;b=xnsq./(4*xnsq-1); rtb=sqrt(b); rtb(1)=[]; 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 L1=ones(1,ngrid); L2=x; % First two Legendre polynomials are L_0 = 1 and L_1 = x for np1=2:npoly n=np1-1; %Recurrence relation L3=((2*n+1)*(x.*L2) - n*L1)/(n+1); L1=L2; L2=L3; end npt=length(pt); zero=0*ones(1,npt); Legpolyn=L3; % Normalize to maximum value on the interval cmax=max(Legpolyn); Legpolyn=Legpolyn/cmax; % Concatenate the two string variables to print the value of N on the graph strn1=num2str(npoly); strn2={'$\ell = $'}; strn=strcat(strn2,strn1); plot(x,Legpolyn,'-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(-.5,-.3,strn,'Interpreter','latex','fontsize',24) axis([-1 1 -.4,.4]) set(gca,'Ytick',[-.4:0.2:.4],'linewidth',1.6) set(gca,'Xtick',[-1:.5:1],'linewidth',1.6) xlabel('$x$','Interpreter','LaTex','FontSize',20) ylabel('$L_{\ell}(x)$','Interpreter','LaTex','FontSize',20) end