Răsfoiți Sursa

Finish homework

I'm sure there's still improvements to be made, but this is probably the
last time I'm touching this.
Pjotir 4 săptămâni în urmă
părinte
comite
80855ef13f
1 a modificat fișierele cu 73 adăugiri și 8 ștergeri
  1. 73 8
      Homework3/problem1_potentialwell.m

+ 73 - 8
Homework3/problem1_potentialwell.m

@@ -1,3 +1,11 @@
+% The author(s) acknowledges the use of Google's Gemini in the
+% preparation or completion of this assignment. Google's Gemini was used
+% in the following way(s): To learn how to use several functions that were useful
+% in constructing this solution, including spdiags (for simplifying the
+% representation of a million element matrix) and trapz (for taking the numeric
+% integral of the wavefunctions in order to normalize them).
+
+
 clear all; clc; close all;
 
 % Janky constants
@@ -6,13 +14,70 @@ m = 1;
 v1 = 30;
 v2 = 40;
 L = 1;
-N = 10;
-x_min = -5; x_max = 5; x = linspace(x_min, x_max, N);
+N = 1000;
+% The well is L wide, so this 2L wide domain will show a little bit of the
+% decay into the barriers
+x = linspace(-L, L, N);
 dx = x(2) - x(1);
 
-diagonal_k = 2*ones(N, 1)
-off_diagonal_k = -1*ones(N-1, 1)
-laplacian = diag(diagonal_k) + diag(off_diagonal_k, 1) + diag(off_diagonal_k, -1)
-laplacian = laplacian / dx^2
-kinetic = -(hbar^2 / (2*m)) * laplacian
-potential = spdiags(v1, 0, N, N)
+% Potential energy function V(x)
+V = zeros(N, 1);
+V(x<-0.5*L) = v1;
+V(x>=-0.5*L & x<=0.5*L) = 0;
+V(x>0.5*L) = v2;
+
+% Much of this is borrowed from classwork.
+diagonal_k = -2*ones(N, 1);
+off_diagonal_k = 1*ones(N-1, 1);
+
+% Tri-diag kinetic matrix
+% I learned about sparse diagonals, and am using them here to make the
+% computation a little easier. I'll convert it back out later using full().
+T = spdiags([[off_diagonal_k; 0], diagonal_k, [0; off_diagonal_k]], [-1 0 1], N, N);
+T = -(hbar^2 / (2*m*dx^2)) * T;
+
+% Total hamiltonian
+H = T + spdiags(V, 0, N, N);
+
+% Solving the eigensystem
+% Just for fun, I'm changing all this code to show the first n wavefunctions,
+% just so I can see what happens at the higher energy states.
+n = 3;
+[psi, E_diag] = eig(full(H));
+E = diag(E_diag);
+[E_sorted, idx] = sort(E, 'ascend');
+psi_sorted = psi(:, idx);
+E_firstn = E_sorted(1:n);
+psi_firstn = psi_sorted(:, 1:n);
+
+% Normalizing the wavefunctions. We know the integral of the probability
+% distribution (aka the square of the wavefunction) needs to equal one, so we
+% can just sort of enforce that.
+% After writing this, I looked back to see how we did this in class. I had
+% completely forgotten that we basically just took the sum of dx wide rectangles
+% under the curve. That's probably a simpler way to reach practically the same
+% result, though it's technically less correct.
+for k = 1:n
+    norm_factor = sqrt(trapz(x, abs(psi_firstn(:, k)).^2));
+
+    % Actually you know what, let's test that. I'll determine just how different
+    % the normalization factor is when you use the discrete method.
+    norm_factor_discrete = sqrt(sum((psi_firstn(:,k).^2))*dx);
+    difference = abs(norm_factor-norm_factor_discrete)
+    % Miniscule, turns out. On the order of -10 to -12. I guess it doesn't really matter.
+
+    psi_firstn(:, k) = psi_firstn(:, k) / norm_factor;
+end
+
+figure; hold on; grid on;
+plot(x, V, 'k--');
+xlabel('x'); ylabel('psi(x)');
+scale = 3; % scaling factor for wavefunction amplitude
+for k = 1:n
+    % I had thought I should be adding the scaled wavefunction here, but the
+    % resulting curves appeared upside-down, so I guess we're subtracting.
+    y_shifted = E_firstn(k) - scale*psi_firstn(:, k);
+    plot(x, y_shifted);
+    yline(E_firstn(k), ':');
+end
+