Parcourir la source

Merge homework with classwork

Pjotir il y a 6 jours
Parent
commit
4eb5734c3b
4 fichiers modifiés avec 103 ajouts et 182 suppressions
  1. 30 64
      Homework4/problem1_drivenpendulum.m
  2. 0 65
      Homework4/problem2_3body.m
  3. 0 53
      Homework4/threeBodyODE.m
  4. 73 0
      classwork/huygens.m

+ 30 - 64
Homework4/problem1_drivenpendulum.m

@@ -1,5 +1,3 @@
-% Enormous portions of this code were borrowed from code written while
-% following along in class.
 clc; clear all; close all;
 
 % Constants
@@ -10,87 +8,55 @@ c = 0.5;
 m = 1;
 initial_angle = deg2rad(70);
 initial_v = 0;
-initial_conditions = [initial_angle, initial_v];
+initial_conditions = [initial_angle; initial_v];
 t_span = [0 100];
 
-% Driving frequencies for each test:
-omega1 = 1;
-omega2 = 1.9;
-
-% Setting up my ODE vectors manually again
-ode_system1 = @(t, theta) [
-    theta(2);     % dtheta/dt
-    (F0*cos(omega1*t) - m*g*L*sin(theta(1)) - c*theta(2))/(m*L^2) % dtheta2/d2t
-    ];
-ode_system2 = @(t, theta) [
+% 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(omega2*t) - m*g*L*sin(theta(1)) - c*theta(2))/(m*L^2) % dtheta2/d2t
-    ];
-
-% Test 1, using omega1
-[t1, theta1] = ode45(ode_system1, t_span, initial_conditions);
-theta_sol1 = theta1(:, 1);
-dthetadt_sol1 = theta1(:, 2);
-f1 = figure;
-ax1 = axes(f1);
-plot(ax1, t1, theta_sol1);
-title(ax1, sprintf("Test 1, using initial angle %.0f deg and driving freq %.0f rad/s", initial_angle, omega1));
-xlabel(ax1, "Time (s)");
-ylabel(ax1, "Theta (rad)");
-
+    (F0*cos(omega*t) - m*g*L*sin(theta) - c*theta(2))/(m*L^2) % dtheta2/d2t
+];
 
-% Test 2, using omega2
-[t2, theta2] = ode45(ode_system2, t_span, initial_conditions);
-theta_sol2 = theta2(:, 1);
-dthetadt_sol2 = theta2(:, 2);
-f2 = figure;
-ax2 = axes(f2);
-plot(ax2, t2, theta_sol2);
-title(ax2, sprintf("Test 2, using initial angle %.0f deg and driving freq %.0f rad/s", initial_angle, omega2));
-xlabel(ax2, "Time (s)");
-ylabel(ax2, "Theta (rad)");
+[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
-f3 = figure;
-ax3 = axes(f3);
+figure;
 axis equal;
-xlim(ax3, [-L, L]);
-ylim(ax3, [-L, 0.5]);
-xlabel(ax3, 'x (m)');
-ylabel(ax3, 'y (m)');
+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);
 
-% We're using test 2 results for the animation
-% Gotta render one frame first to get handles for the plots
-x = x0 + L*sin(theta_sol2(1));
-y = y0 - L*cos(theta_sol2(1));
-hold on;
-plot_string = plot(ax3, [x0,x], [y0,y], 'k-', 'LineWidth', 2);
-plot_ball = plot(ax3, x, y, 'ro', 'MarkerSize', 20);
-
-for i = 1:length(t2)
-
-    % Update title
-    title(ax3, sprintf("Pendulum Animation (Initial Angle = %.0f deg)\nTheta = %f\nTime = %f",...
-        rad2deg(initial_angle),...
-        theta_sol2(i),...
-        t2(i)...
-        ), 'FontSize',12);
+for i = 1:length(t)
 
     % Update pendulum position
-    x = x0 + L*sin(theta_sol2(i));
-    y = y0 - L*cos(theta_sol2(i));
+    x = x0 + L*sin(theta_sol(i));
+    y = y0 - L*cos(theta_sol(i));
 
     hold on;
 
-    set(plot_string, 'XData', [x0,x], 'YData', [y0,y]);
-    set(plot_ball, 'XData', x, 'YData', y);
+    % string
+    plot([x0,x], [y0,y], 'k-', 'LineWidth', 2)
+    % ball
+    plot(x, y, 'ro', 'MarkerSize', 20);
 
 
     drawnow;
-    pause(0.05);
+    pause(0.1);
+    hold off;
+    cla;
 end
+

+ 0 - 65
Homework4/problem2_3body.m

@@ -1,65 +0,0 @@
-clear all; close all; clc;
-
-% Constants
-G = 6.674e-11; % N m^2 kg^-2
-M_sun = 1.989e30; % kg
-M_earth = 5.972e24; % kg
-M_mars = 6.39e23; % kg
-r_earth_sun = 1.496e11; % m
-r_mars_sun = 2.279e11; % m
-year = 365.25 * 24 * 60 * 60; % s
-
-% Task 1, calculate orbital velocities
-v_earth = sqrt((G*M_sun)/r_earth_sun);
-v_mars = sqrt((G*M_sun)/r_mars_sun);
-
-% Task 2, set up initial conditions
-initial_conditions = [0 0 r_earth_sun 0 r_mars_sun 0 0 0 0 v_earth 0 0.85*v_mars];
-
-% Task 3, solve the position over time from gravitational force
-% We're doing this twice with beta=2 and beta=2.01
-t_span = [0 10*year];
-force1 = @(t, Y) threeBodyODE(t, Y, G, M_sun, M_earth, M_mars, 2);
-force2 = @(t, Y) threeBodyODE(t, Y, G, M_sun, M_earth, M_mars, 2.01);
-options = odeset('RelTol', 1e-6, 'AbsTol', 1e-6);
-[t1, Y1] = ode45(force1, t_span, initial_conditions, options);
-[t2, Y2] = ode45(force2, t_span, initial_conditions, options);
-
-x1 = Y1(:, 1);
-y1 = Y1(:, 2);
-x2 = Y1(:, 3);
-y2 = Y1(:, 4);
-x3 = Y1(:, 5);
-y3 = Y1(:, 6);
-
-figure;
-hold on;
-xlim([min([x1;x2;x3]), max([x1;x2;x3])]);
-ylim([min([y1;y2;y3]), max([y1;y2;y3])]);
-plot(x1, y1, 'yo', 'MarkerFaceColor', 'y', 'MarkerSize', 25);
-plot(x2, y2, 'g-');
-plot(x3, y3, 'r-');
-legend('Sun', 'Earth Path', 'Mars Path');
-xlabel('x (m)');
-ylabel('y (m)');
-title("Sun, Earth, and Mars path over 10 years (beta = 2)")
-
-x1 = Y2(:, 1);
-y1 = Y2(:, 2);
-x2 = Y2(:, 3);
-y2 = Y2(:, 4);
-x3 = Y2(:, 5);
-y3 = Y2(:, 6);
-
-figure;
-hold on;
-xlim([min([x1;x2;x3]), max([x1;x2;x3])]);
-ylim([min([y1;y2;y3]), max([y1;y2;y3])]);
-plot(x1, y1, 'yo', 'MarkerFaceColor', 'y', 'MarkerSize', 25);
-plot(x2, y2, 'g-');
-plot(x3, y3, 'r-');
-legend('Sun', 'Earth Path', 'Mars Path');
-legend('Sun', 'Earth Path', 'Mars Path');
-xlabel('x (m)');
-ylabel('y (m)');
-title("Sun, Earth, and Mars path over 10 years (beta = 2.01)")

+ 0 - 53
Homework4/threeBodyODE.m

@@ -1,53 +0,0 @@
-function dYdt = threeBodyODE(t, Y, G, m1, m2, m3, beta, alpha)
-    if nargin < 7
-        beta = 2;
-    end
-    if nargin < 8
-        alpha = 0;
-    end
-
-    x1 = Y(1);
-    y1 = Y(2);
-    x2 = Y(3);
-    y2 = Y(4);
-    x3 = Y(5);
-    y3 = Y(6);
-    vx1 = Y(7);
-    vy1 = Y(8);
-    vx2 = Y(9);
-    vy2 = Y(10);
-    vx3 = Y(11);
-    vy3 = Y(12);
-
-    dx12 = x2 - x1; dy12 = y2 - y1;
-    dx13 = x3 - x1; dy13 = y3 - y1;
-    dx23 = x3 - x2; dy23 = y3 - y2;
-
-    r12 = sqrt(dx12^2 + dy12^2);
-    r13 = sqrt(dx13^2 + dy13^2);
-    r23 = sqrt(dx23^2 + dy23^2);
-
-    theta12 = atan2(dy12, dx12);
-    theta13 = atan2(dy13, dx13);
-    theta23 = atan2(dy23, dx23);
-
-    F_mag12 = (G*m1*m2/r12^beta)*(1 + alpha/r12^2);
-    F_mag13 = (G*m1*m3/r13^beta)*(1 + alpha/r13^2);
-    F_mag23 = (G*m2*m3/r23^beta)*(1 + alpha/r23^2);
-
-    F_x12 = F_mag12*cos(theta12);
-    F_y12 = F_mag12*sin(theta12);
-    F_x13 = F_mag13*cos(theta13);
-    F_y13 = F_mag13*sin(theta13);
-    F_x23 = F_mag23*cos(theta23);
-    F_y23 = F_mag23*sin(theta23);
-
-    ax1 = (F_x12+F_x13)/m1;
-    ay1 = (F_y12+F_y13)/m1;
-    ax2 = (F_x23-F_x12)/m2;
-    ay2 = (F_y23-F_y12)/m2;
-    ax3 = (-F_x13-F_x23)/m3;
-    ay3 = (-F_y13-F_y23)/m3;
-
-    dYdt = [vx1; vy1; vx2; vy2; vx3; vy3; ax1; ay1; ax2; ay2; ax3; ay3];
-end

+ 73 - 0
classwork/huygens.m

@@ -0,0 +1,73 @@
+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;
+
+sources_num = 100
+source_position_x = floor(linspace(1, Nx, sources_num));
+source_position_y = ones(1, sources_num) * floor(Ny/2);
+source_phase = zeros(1, sources_num);
+% Destructive interference
+% source_phase(1:2:end) = pi;
+source_phase = linspace(0, 20*pi, sources_num);
+
+%initialize fields
+u = zeros(Nx, Ny);
+u_prev = zeros(Nx, Ny);
+
+%plot initial
+figure;
+hold on;
+colorbar;
+zlim([-1.5, 1.5]);
+clim([-1.5, 1.5]);
+view(3);
+hsurf = surf(X, Y, u, 'EdgeColor', 'none');
+figure;
+hold on;
+grid off;
+axis off;
+xlim([1, Nx]);
+ylim([1, Ny]);
+hplot = imagesc(u);
+
+for n = 1:Nt
+
+    for i = 1:length(source_position_x)
+        source = sin(omega * n * dt+source_phase(i));
+        u(source_position_y(i), source_position_x(i)) = source;
+    end
+
+    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);
+
+    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);
+        set(hplot, 'CData', u);
+        drawnow;
+    endif
+
+    pause(0.005);
+end