double_slit.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. clear all; close all; clc;
  2. c = 2.998e8; % m/s
  3. Lx = 6e-6; % m (6 um)
  4. Ly = 5e-6;
  5. Nx = 360;
  6. Ny = 360;
  7. x = linspace(0, Lx, Nx);
  8. y = linspace(-Ly/2, Ly/2, Ny);
  9. dx = x(2) - x(1);
  10. dy = y(2) - y(1);
  11. dt = 0.5*dx/c;
  12. [X, Y] = meshgrid(x, y);
  13. tmax = (Lx/c)*3;
  14. Nt = floor(tmax/dt);
  15. % light source
  16. lambda = 500e-9;
  17. k = 2*pi/lambda;
  18. omega = c*k;
  19. source_width = 1*Ly;
  20. source_mask = zeros(Ny, 1);
  21. source_index = find(abs(y)<=source_width/2);
  22. source_mask(source_index) = 1;
  23. % initialize fields
  24. u = zeros(Ny, Nx);
  25. u_prev = zeros(Nx, Ny);
  26. % setting up slit
  27. barrier_index_x = floor(Nx/3);
  28. slit_width = lambda;
  29. % double slit
  30. slit_seperation = 2*lambda;
  31. barrier_index_top = find(abs(y-slit_seperation/2) > slit_width/2);
  32. barrier_index_bottom = find(abs(y+slit_seperation/2) > slit_width/2);
  33. barrier_index_y = intersect(barrier_index_top, barrier_index_bottom);
  34. % Accumulation vector
  35. I = zeros(Ny, 1);
  36. %plot initial
  37. fig1 = figure;
  38. hold on;
  39. shading interp;
  40. colorbar;
  41. zlim([-4, 4]);
  42. clim([-4, 4]);
  43. xlabel('x');
  44. ylabel('y');
  45. zlabel('z');
  46. view(37.5, 30);
  47. hsurf = surf(X, Y, u, 'EdgeColor', 'none');
  48. % Also the accumulation plot
  49. fig2 = figure;
  50. hold on;
  51. aplot = plot(y, I');
  52. xlim([-Lx/2 Lx/2]);
  53. ylim([0, 50]);
  54. grid on;
  55. % single slit plot
  56. % wallZ = 0;
  57. % plot3([x(barrier_index_x), x(barrier_index_x)], [y(1), y(floor(length(y)/2))-slit_width/2], [wallZ, wallZ], 'r-', 'LineWidth', 3);
  58. % plot3([x(barrier_index_x), x(barrier_index_x)], [y(floor(length(y)/2))+slit_width/2, y(end)], [wallZ, wallZ], 'r-', 'LineWidth', 3);
  59. for n = 1:Nt
  60. u_prev_prev = u_prev;
  61. u_prev = u;
  62. % 2D wave equation udpate
  63. u = 2*u_prev - u_prev_prev + 4*(c*dt)^2*del2(u_prev, dx, dy);
  64. % Apply boundary conditions
  65. u = absorbing_bc(u, u_prev, c, dt, dx, dy);
  66. % Soft source on left edge (prevents reflected waves from trapping/exploding)
  67. if n < Nt
  68. source = 0.5 * sin(omega * n * dt);
  69. u(:, 1) = u(:, 1) + source .* source_mask;
  70. end
  71. % Zeroing out waveform at barrier
  72. u(barrier_index_y, barrier_index_x) = 0;
  73. % Accumulate waveform at the edge
  74. I = I + u(:, end).^2;
  75. if mod(n, 2) == 0
  76. current_time_str = num2str(n*dt*1e15, '%.2f');
  77. title(['Time: ', current_time_str, ' fs']);
  78. set(hsurf, 'ZData', u, 'CData', u);
  79. set(aplot, 'ydata', I');
  80. % Scale fig2 y axis if intensity exceeds current limit
  81. if max(I) > ylim(gca)*[0;1]
  82. ylim(gca, [0, max(I)*1.2]);
  83. endif
  84. drawnow;
  85. endif
  86. end