You are on page 1of 4

%% Finite Well

Finite_depth=5; % Depth of the finite well


Systemsize=20; % Size of our system
L=Systemsize*.8; % Size of the well

% Defining the x vector and the step dx


leftend=-Systemsize/2;
rightend=Systemsize/2;
N=1e3;
x=linspace(leftend,rightend,N);
dx=x(2)-x(1);

% Defining the 2nd derivative matrix


d2=(diag(-2.*ones(size(x)))+diag(ones(1,length(x)-1),1)+diag(ones(1,length(x)-1),-1))./dx^2;

% Defining the potential as a function of x


V=@(x) Finite_depth.*(1-rectangularPulse(-L/2,L/2,x));

% Turning the potential from a vector function of x into a diagonal matrix


Potential=V(x).*eye(length(x));

% Defining the Hamiltonian matrix


H=-d2+Potential;

% Diagonalizing it to get psi (eigenfunctions) and E (energies)


[psi,E]=eig(H);

% Plotting the well


figure(1); plot(x,V(x),'linewidth',1.5); ylim([-0.5 5.5]); grid on; xlabel('x'); ylabel('V');
title('Finite Potential Well');

% Plotting the eigenfunctions


figure(2); plot(x,psi(:,1),x,psi(:,2)+0.1,x,psi(:,3)+0.2,x,psi(:,4)+0.3,x,...
psi(:,5)+0.4,'linewidth',1.5); hold all; grid on; xlabel('x'); ylabel('\psi(x)');
title('First 5 Eigenfunctions'); ylim([-0.1 0.5]);

% Adding the well bounds


plot([-8 -8],[-1 1],'color','k','linewidth',2.5); plot([8 8],[-1 1],'color','k','linewidth',2.5);

%% Well with a Bump

Infinite_depth=1e3; % Depth of the infinite well


Bump_height=0.5; % Height of the bump
Systemsize=20; % Size of our system
L=Systemsize*.8; % Size of the well
a=Systemsize*.3; % Width of the bump

% Defining the x vector and the step dx


leftend=-Systemsize/2;
rightend=Systemsize/2;
N=1e3;
x=linspace(leftend,rightend,N);
dx=x(2)-x(1);

% Defining the 2nd derivative matrix


d2=(diag(-2.*ones(size(x)))+diag(ones(1,length(x)-1),1)+diag(ones(1,length(x)-1),-1))./dx^2;

% Defining the potential as a function of x


V=@(x) Infinite_depth.*(1-rectangularPulse(-L/2,L/2,x))+...
Bump_height.*rectangularPulse(-a/2,a/2,x); % bump in the middle

% Turning the potential from a vector function of x into a diagonal matrix


Potential=V(x).*eye(length(x));

% Defining the Hamiltonian matrix


H=-d2+Potential;

% Diagonalizing it to get psi (eigenfunctions) and E (energies)


[psi,E]=eig(H);

% Turning the Diagonal matrix E into a vector of energies


E=diag(E);
% Definig a vector of the level numbers
n=1:length(x);

% Plotting the well


figure(1); plot(x,V(x),'linewidth',1.5); xlim([-10 10]); ylim([-0.5 5.5]); grid on; ...
xlabel('x'); ylabel('V'); title('Infinite Well with a Bump');

% Plotting the eigenfunctions


figure(2); plot(x,psi(:,1),x,psi(:,2)+0.2,x,psi(:,3)+0.4,x,psi(:,4)+0.6,x,...
psi(:,5)+0.8,'linewidth',1.5); hold all; grid on; xlabel('x'); ylabel('\psi(x)');
title('First 5 Eigenfunctions'); ylim([0 0.9]);
plot([-8 -8],[-1 1],'color','k','linewidth',2.5); plot([8 8],[-1 1],'color','k','linewidth',2.5);

% Plotting the energies vs. n. In our units, an infinite well has


% En=pi^2*n^2/L^2
figure(3); plot(n,E,'o','linewidth',1); hold all; plot(n,pi^2.*n.^2/L^2,'linewidth',2); ...
xlim([0 110]); ylim([0 400]); grid on; xlabel('n');
legend('E_n','\pi^2n^2/L^2','location','northwest');

% Defining the initial state


psi_init=1/sqrt(2).*(psi(:,1)+psi(:,2));

% Defining the propagation matrix as a function of time


% Note that here, I used the fact that Delta t is proportional to hbar to
% get rid of hbar in the exponent
propagation=@(t) expm(-1i*H*t);
Delta_t=pi/(E(2)-E(1));

t_steps=21;

% Creating a vector of times to plot


ts=linspace(0,2*Delta_t,t_steps);

% Allocating memory for the propagated wavefunctions


propagated_psi=nan(size(psi,1),t_steps);

figure;
for j=1:t_steps

subplot(3,7,j);

% Propagating psi by operating on it with the propagation matrix


propagated_psi(:,j)=propagation(ts(j))*psi_init;

% Plotting
plot(x,abs(propagated_psi(:,j)).^2,'linewidth',2); grid on; ylim([0 6e-3]);
title(['t = ' num2str(ts(j)/Delta_t) '\Delta t']);

end

% Calculating expectation values


t_steps2=251;

% Creating a vector of times to plot


ts2=linspace(0,10*Delta_t,t_steps2);

% Allocating memory for the propagated wavefunctions


propagated_psi=nan(size(psi,1),t_steps2);

% There are two equivalent ways to calculate the expectation values.


% Allocating memory:
mean_x=nan(1,t_steps2);
mean_x2=mean_x;
mean_xsq=mean_x;
mean_xsq2=mean_x;
mean_H=mean_x;
sigma=mean(x);

% For the 2nd method: define the X and X^2 operator matrix:
Xmat=x.*eye(length(x));
Xsqmat=x.^2.*eye(length(x));

for j=1:t_steps2
% Propagating psi by operating on it with the propagation matrix
propagated_psi(:,j)=propagation(ts2(j))*psi_init;

mean_x(j)=trapz(x.*abs(propagated_psi(:,j))'.^2); % calculating using an integral


mean_x2(j)=propagated_psi(:,j)'*Xmat*propagated_psi(:,j); % calculating by just <psi|X|psi>

mean_xsq(j)=trapz(x.^2.*abs(propagated_psi(:,j))'.^2);
mean_xsq2(j)=propagated_psi(:,j)'*Xsqmat*propagated_psi(:,j);

mean_H(j)=propagated_psi(:,j)'*H*propagated_psi(:,j); % Calculating by <psi|H|psi>

sigma(j)=sqrt(mean_xsq(j)-mean_x(j)^2);

end

figure; plot(ts2/Delta_t,mean_x,'linewidth',1.5);
xlabel('t/\Delta t'); ylabel('<x>'); grid on;
figure; plot(ts2/Delta_t,mean_xsq,'linewidth',1.5);
xlabel('t/\Delta t'); ylabel('<x^2>'); grid on; ylim([0 25]);
figure; plot(ts2/Delta_t,sigma,'linewidth',1.5);
xlabel('t/\Delta t'); ylabel('\sigma_X'); grid on;
figure; plot(ts2/Delta_t,mean_H,'linewidth',1.5);
xlabel('t/\Delta t'); ylabel('<H>'); grid on; ylim([0 0.3]);

%% Bump with Increasing Height

Infinite_depth=1e3; % Depth of the infinite well


Bump_height=[0.001 0.01 0.03 0.05 0.1]*Infinite_depth; % Height of the bump
Systemsize=20; % Size of our system
L=Systemsize*.8; % Size of the well
a=Systemsize*.3; % Width of the bump

% Defining the x vector and the step dx


leftend=-Systemsize/2;
rightend=Systemsize/2;
N=1e3;
x=linspace(leftend,rightend,N);
dx=x(2)-x(1);

% Defining the 2nd derivative matrix


d2=(diag(-2.*ones(size(x)))+diag(ones(1,length(x)-1),1)+diag(ones(1,length(x)-1),-1))./dx^2;

for i=1:length(Bump_height)

% Defining the potential as a function of x


V=@(x) Infinite_depth.*(1-rectangularPulse(-L/2,L/2,x))+...
Bump_height(i).*rectangularPulse(-a/2,a/2,x); % bump in the middle

% Turning the potential from a vector function of x into a diagonal matrix


Potential=V(x).*eye(length(x));

% Defining the Hamiltonian matrix


H=-d2+Potential;

% Diagonalizing it to get psi (eigenfunctions) and E (energies)


[psi,E]=eig(H);

% Turning the Diagonal matrix E into a vector of energies


E=diag(E);

% Definig a vector of the level numbers


n=1:length(x);

% Plotting the well


figure(31); set(gcf,'position',[100 50 1000 700]);
subplot(3,2,i); plot(x,V(x),'linewidth',1.5); xlim([-10 10]); %ylim([-0.5 5.5]); ...
grid on; xlabel('x'); ylabel('V');
title(['Infinite Well with a Bump of ' num2str(Bump_height(i)/Infinite_depth) '\cdotU']);

% Plotting the eigenfunctions


figure(32); set(gcf,'position',[100 50 1000 700]);
subplot(3,2,i); plot(x,psi(:,1),x,psi(:,2)+0.2,x,psi(:,3)+0.4,x,psi(:,4)+0.6,x,...
psi(:,5)+0.8,'linewidth',1.5); hold all; grid on; xlabel('x'); ylabel('\psi(x)');
title(['First 5 Eigenfunctions for V = ' num2str(Bump_height(i)/Infinite_depth) '\cdotU']);
ylim([-0.1 0.9]);
plot([-8 -8],[-1 1],'color','k','linewidth',2.5); plot([8 8],[-1 1],'color','k','linewidth',2.5);
figure(33); set(gcf,'position',[100 50 1000 700]);
subplot(3,2,i); plot(n(1:50),E(1:50),'o'); xlabel('n'); ylabel('E_n');
title(['Energies for V = ' num2str(Bump_height(i)/Infinite_depth) '\cdotU']);
grid on;

end

You might also like