%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%b
% Problem Set 1
% 1.3 Solutions
% V. Anant
% Mildly modified and more fully anotated by W. Kaminsky 9/19/2006
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% preamble
clear;
close all;
format compact

% x row vector
dx = 0.02;
x = [-20:dx:20]; % x measured in nanometers.
% L row vector
L = [0.5 1 2]; % L measured in nanometers.

%  Calculate Psi_L
%  first make a mesh (see 'help meshgrid')
[xmesh, Lmesh] = meshgrid(x,L);
% then calculate psi
psi = (pi.*Lmesh.^2).^(-1/4).*exp(-xmesh.^2./(2*Lmesh.^2));
% this is the magnitude squared of psi
psi_sq = conj(psi).*psi;

% now plot magnitude squared
figure(1);
plot(x, psi_sq)
axis([-6 6 0 max(max(psi_sq))])
title('Probability Density of Gaussian Wavefunctions');
xlabel('x (nm)');
ylabel('|\Psi|^2  (nm^{-1})');
legend('L = 0.5nm', 'L = 1nm', 'L = 2nm');

%%%%%%%%%%%
% part (c)

% Normalization Calculation
psi_norm = sum(psi_sq,2)*dx

% Check: Sum of each row of psi_sq should be 1.
% Syntax Note:  As the sum is over the rows, use a 2 in the argument
% of the sum function

%%%%%%%%%%%
% part (d)

% Mean Value Calculation:
psi_mean = sum(conj(psi).*xmesh.*psi.*dx,2)   % measured in nanometers

%  Check: All the Gaussians have mean 0. 
%  Thus, psi_mean (see below) should be extremely close to 0 
%  (The discrepancy will be primarily due to the discretization.)


% Variance Calculation
psi_meansq = sum(conj(psi).*xmesh.^2.*psi.*dx,2) % measured in nanometrers

%  Check:  The Gaussians squared are of the form e^{-x^2 / L^2}
%  Thus, the variances are \sigma^2 = L^2 / 2, and so psi_meansq (see
%  below) should take values extremely close to 0.125 nm^2 for L = 0.5 nm,
%  0.5 nm^2 for L = 1.0 nm, and 2.0 nm^2 for L = 2.0 nm.


%  Uncertainty (Standard Deviation) Calculation
delta_x = sqrt(psi_meansq - psi_mean.^2)
%
% As the psi_mean should be [0 nm 0 nm 0 nm] and psi_meansq should be 
% [0.125 nm^2, 0.5 nm^2, 2.0 nm^2] for L = [0.5 nm 1.0 nm 2.0 nm]
% respectively, delta_x should simply be the square root of psi_meansq and
% thus [1/sqrt(8) nm  1/sqrt(2) nm  sqrt(2) nm] 
% (to 4 decimal places [0.3536 nm, 0.7071 nm, 1.4142 nm])
