%
%  Lecture 5  Particle in a box
%

home
clear

% 1-D particle in a box solutions
hbar = 1.05457e-34; % J s
m = 9.10938e-31; % kg, electron mass
L = 1e-9; % 1 nm

% solutions
E1 = ((pi*hbar/L)^2)/(2*m)/1.60218e-19; % eV
E = @(n) E1*n*n; % eV
pib=@(x,n) (2/L)^0.5*sin(n*pi*x/L);

x=[0:L/300:L]; % evenly spaced array of 100 numbers between 0 and L

% calculate wavefunction n=1
pib1=pib(x,1);
norm=max(pib1);

h=figure('color',[1 1 1]);
set(h,'WindowStyle','Docked');
%
% plot functions normalized to n=1 and offset by energies in eV
%
plot(x*1e9,E(1)+pib1/norm,[0 1],[E(1) E(1)],'k:');
text(0.2,E(1),'n=1')
hold all
for i=2:5
    plot(x*1e9,E(i)+pib(x,i)/norm,[0 1],[E(i) E(i)],'k:');
    text(0.2,E(i),int2str(i))
end
ylabel('Energy (eV)');xlabel('Distance (nm)');
%legend('n=1','n=2','n=3','n=4','n=5');
title('Particle in a box solutions for an electron in a 1 nm box');

% superposition of 2 functions
h=figure('color',[1 1 1]);
set(h,'WindowStyle','Docked');

pibsum=pib(x,1)+pib(x,2);
plot(x*1e9,pibsum,[0 1],[0 0],'k:')
title('Particle in a box \psi_1+\psi_2')

% Superposition of cosine waves
h=figure('color',[1 1 1]);
set(h,'WindowStyle','Docked');

pibsum=0
N=5
for i=1:2:N
    pibsum=pibsum+pib(x,i)*(-1)^((i-1)/2);
end
pibsum=pibsum/max(pibsum);
plot(x*1e9,pibsum)
hold all

pibsum=0
N=10
for i=1:2:N
    pibsum=pibsum+pib(x,i)*(-1)^((i-1)/2);
end
pibsum=pibsum/max(pibsum);
plot(x*1e9,pibsum)

pibsum=0
N=20
for i=1:2:N
    pibsum=pibsum+i*pib(x,i)*(-1)^((i-1)/2);
end
pibsum=pibsum/max(pibsum);
plot(x*1e9,pibsum)

pibsum=0
N=100
for i=1:2:N
    pibsum=pibsum+i*pib(x,i)*(-1)^((i-1)/2);
end
pibsum=pibsum/max(pibsum);
plot(x*1e9,pibsum)
legend('N=5','N=10','N=20','N=100')
xlabel('Distance (nm)'); ylabel('\Sigma\psi_i')
title('Superpositions of particle in a box wavefunctions')

% two-dimensional PIB solutions
pib2D=@(x,y,n1,n2) (2/L)*sin(n1*pi*x/L).*sin(n2*pi*y/L);

[x,y]=meshgrid(0:L/100:L);
pib2D11=pib2D(x,y,1,1);
pib2D11=pib2D11/max(max(pib2D11));

h=figure('color',[1 1 1]);
set(h,'WindowStyle','Docked');
meshc(x/L,y/L,pib2D11)
title('Two-dimensional particle in a box, n_x = n_y = 1')

pib2D22=pib2D(x,y,2,2);
pib2D22=pib2D22/max(max(pib2D22));

h=figure('color',[1 1 1]);
set(h,'WindowStyle','Docked');
meshc(x/L,y/L,pib2D22)
title('Two-dimensional particle in a box, n_x = n_y = 2')

pib2D13=pib2D(x,y,3,1);
pib2D13=pib2D13/max(max(pib2D13));

h=figure('color',[1 1 1]);
set(h,'WindowStyle','Docked');
meshc(x/L,y/L,-pib2D13)
title('Two-dimensional particle in a box, n_x = 3, n_y =1')
pibsum =
     0
N =
     5
pibsum =
     0
N =
    10
pibsum =
     0
N =
    20
pibsum =
     0
N =
   100