% E = particlebox(N)
% Compute the first four energy levels for the "particle in a box"
% model using a finite-difference discretization for d^2/dx^2
% with N interior mesh points.

function E = particlebox(N)

% Points go 0 to N+1; 0 and N+1 satisfy BCs
h = 1/(N+1);
T = -diag(ones(N-1,1),-1) + 2*eye(N) - diag(ones(N-1,1),1);

% Estimate eigenvalues and eigenvectors
[V,D] = eig(T/h^2);
E = diag(D);

% Compute the first four energy levels
E = E(1:4);