2 커밋 5cf3795e3f ... 2b66b50b74

작성자 SHA1 메시지 날짜
  Pjotir 2b66b50b74 Add more electric field classwork, including a single slit diffraction example 1 주 전
  Pjotir 538c8abd33 Start Homework4 1 주 전
4개의 변경된 파일과 161개의 추가작업 그리고 2개의 파일을 삭제
  1. 62 0
      Homework4/problem1_drivenpendulum.m
  2. 6 0
      classwork/absorbing_bc.m
  3. 9 2
      classwork/electric_field.m
  4. 84 0
      classwork/single_slit.m

+ 62 - 0
Homework4/problem1_drivenpendulum.m

@@ -0,0 +1,62 @@
+clc; clear all; close all;
+
+% Constants
+g = 9.81;
+L = 1;
+F0 = 10;
+c = 0.5;
+m = 1;
+initial_angle = deg2rad(70);
+initial_v = 0;
+initial_conditions = [initial_angle; initial_v];
+t_span = [0 100];
+
+% Setting up my ODE vector manually again
+% driving frequency for this test:
+omega = 1;
+ode_system = @(t, theta) [
+    theta(1);     % theta/t
+    theta(2);     % dtheta/dt
+    (F0*cos(omega*t) - m*g*L*sin(theta) - c*theta(2))/(m*L^2) % dtheta2/d2t
+];
+
+[t, theta] = ode45(ode_system, t_span, initial_conditions);
+theta_sol = theta(:, 1);
+dthetadt_sol = theta(:, 2);
+
+figure;
+plot(t, theta_sol);
+
+%-------------------------------------------------
+% Pendulum's base position
+x0 = 0; y0 = 0;
+
+% Animation loop
+figure;
+axis equal;
+xlim([-L, L]);  % Set x-axis limits
+ylim([-L, 0.5]);  % Set y-axis limits
+xlabel('x (m)');
+ylabel('y (m)');
+title(sprintf('Pendulum Animation (Angle = %.0f deg)', rad2deg(initial_angle)),'FontSize',14);
+
+for i = 1:length(t)
+
+    % Update pendulum position
+    x = x0 + L*sin(theta_sol(i));
+    y = y0 - L*cos(theta_sol(i));
+
+    hold on;
+
+    % string
+    plot([x0,x], [y0,y], 'k-', 'LineWidth', 2)
+    % ball
+    plot(x, y, 'ro', 'MarkerSize', 20);
+
+
+    drawnow;
+    pause(0.1);
+    hold off;
+    cla;
+end
+

+ 6 - 0
classwork/absorbing_bc.m

@@ -0,0 +1,6 @@
+function u_next = absorbing_bc(u_next, u, c, dt, dx, dy)
+    u_next(:, 1) = u(:, 2) + ((c*dt-dx)/(c*dt+dx))*(u_next(:, 2)-u(:,1));
+    u_next(:, end) = u(:, end-1) + ((c*dt-dx)/(c*dt+dx))*(u_next(:, end-1)-u(:,end));
+    u_next(1, :) = u(2, :) + ((c*dt-dy)/(c*dt+dy))*(u_next(2, :)-u(1,:));
+    u_next(end, :) = u(end-1, :) + ((c*dt-dy)/(c*dt+dy))*(u_next(end-1, :)-u(end,:));
+end

+ 9 - 2
classwork/electric_field.m

@@ -32,11 +32,17 @@ figure;
 hold on;
 hold on;
 colorbar;
 colorbar;
 zlim([-2, 2]);
 zlim([-2, 2]);
+clim([-2, 2]);
 view(3);
 view(3);
 hsurf = surf(X, Y, u, 'EdgeColor', 'none');
 hsurf = surf(X, Y, u, 'EdgeColor', 'none');
 
 
 for n = 1:Nt
 for n = 1:Nt
-    source = sin(omega*n*dt);
+    if n < Nt/4
+        source = sin(omega*n*dt);
+    else
+        source = 0;
+    end
+
     u(:,1) = source.*source_mask;
     u(:,1) = source.*source_mask;
 
 
     u_prev_prev = u_prev;
     u_prev_prev = u_prev;
@@ -44,7 +50,8 @@ for n = 1:Nt
 
 
     u = 2*u_prev - u_prev_prev + 4*(c*dt)^2*del2(u_prev, dx, dy);
     u = 2*u_prev - u_prev_prev + 4*(c*dt)^2*del2(u_prev, dx, dy);
 
 
-    u = Neumann(u);
+    % u = Neumann(u);
+    u = absorbing_bc(u, u_prev, c, dt, dx, dy);
 
 
     if mod(n, 2) == 0
     if mod(n, 2) == 0
         current_time_str = num2str(n*dt*10e15, '%.2f');
         current_time_str = num2str(n*dt*10e15, '%.2f');

+ 84 - 0
classwork/single_slit.m

@@ -0,0 +1,84 @@
+clear all; close all; clc;
+
+c = 2.998e8; % m/s
+Lx = 5e-6;   % m (5 um)
+Ly = 5e-6;
+Nx = Ny = 300;
+x = linspace(0, Lx, Nx);
+y = linspace(-Ly/2, Ly/2, Ny);
+dx = x(2) - x(1);
+dy = y(2) - y(1);
+dt = 0.5*dx/c;
+[X, Y] = meshgrid(x, y);
+tmax = (Lx/c)*2;
+Nt = floor(tmax/dt);
+
+% light source
+lambda = 500e-9;
+k = 2*pi/lambda;
+omega = c*k;
+
+source_width = 1*Ly;
+source_mask = zeros(Ny, 1);
+source_index = find(abs(y)<=source_width/2);
+source_mask(source_index) = 1;
+
+% initialize fields
+u = zeros(Nx, Ny);
+u_prev = zeros(Nx, Ny);
+
+% setting up slit
+barrier_index_x = floor(Nx/3);
+slit_width = lambda;
+
+% single slit
+barrier_index_y = find(abs(y)>slit_width/2);
+
+%plot initial
+figure;
+hold on;
+shading interp;
+colorbar;
+zlim([-2, 2]);
+clim([-2, 2]);
+xlabel('x');
+ylabel('y');
+zlabel('z');
+view(37.5, 30);
+hsurf = surf(X, Y, u, 'EdgeColor', 'none');
+
+% single slit plot
+wallZ = 0;
+plot3([x(barrier_index_x), x(barrier_index_x)], [y(1), y(floor(length(y)/2))-slit_width/2], [wallZ, wallZ], 'r-', 'LineWidth', 3);
+plot3([x(barrier_index_x), x(barrier_index_x)], [y(floor(length(y)/2))+slit_width/2, y(end)], [wallZ, wallZ], 'r-', 'LineWidth', 3);
+
+
+for n = 1:Nt
+    if n < Nt
+        source = sin(omega*n*dt);
+    else
+        source = 0;
+    end
+
+    u(:,1) = source.*source_mask;
+
+    u_prev_prev = u_prev;
+    u_prev = u;
+
+    u = 2*u_prev - u_prev_prev + 4*(c*dt)^2*del2(u_prev, dx, dy);
+
+    % u = Neumann(u);
+    u = absorbing_bc(u, u_prev, c, dt, dx, dy);
+
+    % Zeroing out waveform at barrier
+    u(barrier_index_y, barrier_index_x) = 0;
+
+    if mod(n, 2) == 0
+        current_time_str = num2str(n*dt*10e15, '%.2f');
+        title(['Time: ', current_time_str, ' fs']);
+        set(hsurf, 'ZData', u, 'CData', u);
+        drawnow;
+    endif
+
+    pause(0.005);
+end