% This program sums the terms in the linear combination of eigenstates % to produce a wave function that is constant for 0 < x < 0.5 and zero % between 0.5 and 1.0 . % It is describe in the notes of the last infinite square well lecture. nterms = 100; % nterms is the number of terms kept. % Convergence should be attained as nterms becomes large. n = 1:nterms; c = (1 - cos(pi*0.5*n))*2./(pi*n); % coefficients computer in notes. x = 0:0.001:1; psi = 0*x; for counter = 1:nterms, psi = psi + c(counter)*sqrt(2)*sin(pi*counter*x); end exactpsi = sqrt(2)*(x < 0.5); plot(x,abs(psi).^2,x,abs(exactpsi).^2) xlabel('x') ylabel('|psi|^2')