-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticle_in_1D.m
74 lines (65 loc) · 1.99 KB
/
Particle_in_1D.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Quantum particle in a box (1-dimension)
%
% This program solves the particle in a box problem on a symmetric
% potential of length L using both the analytic solution and the eigenmode
% solution (e.g. using a matrix of finite differences for the Hamiltonian
% operator H in the equation: H*psi(x) = E*psi(x))
%
% Made by: Oscar A. Nieves
% Made in: 2019
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; clc; close all;
% Inputs (Setting Planck's constant to 1)
hbar = 1;
m = 1;
L = 4;
x = -L/2:0.01:L/2;
dx = x(2)-x(1);
N = length(x);
% Derivative matrix Dxx
main_diag = diag(-2*ones(N,1),0);
upper_diag = diag(ones(N-1,1),1);
lower_diag = diag(ones(N-1,1),-1);
Dxx = 1/dx^2 * (lower_diag + main_diag + upper_diag);
% Linear operator on the left (matrix A)
A = -hbar^2/2/m * Dxx;
% Eigenvalues and eigenvectors of A
[A_eigenvectors, A_eigenvalues] = eig(A);
% Extract energies and solutions psi(x)
sols = 3;
for n = 1:sols
Energies(n) = A_eigenvalues(n,n);
psi(:,n) = A_eigenvectors(:,n);
end
% Normalize eigenfunctions
for n = 1:3
psi_sq = psi(:,n).*conj(psi(:,n));
normalization = dx/2 * sum( psi_sq(1:end-1) + psi_sq(2:end) );
psi(:,n) = psi(:,n)/sqrt(normalization);
end
% Plot first 3 solutions (energy levels)
figure(1);
set(gcf,'color','w');
subplot(121);
plot(x, psi(:,1:sols), 'LineWidth',3);
xlabel('x');
ylabel('\psi(x)');
legend('\psi_1(x)','\psi_2(x)','\psi_3(x)'); legend boxoff;
set(gca,'FontSize',20);
subplot(122);
plot(x, psi_analytic(x,1),x, psi_analytic(x,2),x, psi_analytic(x,3), 'LineWidth',3);
xlabel('x');
ylabel('\psi(x)');
legend('\psi_1(x)','\psi_2(x)','\psi_3(x)'); legend boxoff;
set(gca,'FontSize',20);
% Analytic solution
function psi_out = psi_analytic(xv,nv)
L = 4;
if rem(nv,2) == 0
psi_out = sqrt(2/L)*sin(nv*pi.*xv/L);
else
psi_out = sqrt(2/L)*cos(nv*pi.*xv/L);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%