problem1_potentialwell.m 2.9 KB

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