function H = constructHamiltonian(x); % CONSTRUCTHAMILTONIAN function to prepare an (N-1)*(N-1) matrix % representing the sum of kinetic and potential energy operators. global hbar eV m; N = length(x) - 1; deltaX = (max(x) - min(x))/N; off = ones(N-2,1); % same as secDriv.m secDiff = -2*eye(N-1) + diag(off,1) + diag(off,-1); A = secDiff/(deltaX^2); KE = -hbar^2/(2*m*eV) * A; % build the KE matrix PE = zeros(N-1); % build the PE matrix for j=1:(N-1) PE(j,j) = potential(x(j+1)); end; H = KE + PE; return;