Kaynağa Gözat

Finish Homework 4

Pjotir 1 hafta önce
ebeveyn
işleme
2665ef602d

+ 64 - 30
Homework4/problem1_drivenpendulum.m

@@ -1,3 +1,5 @@
+% Enormous portions of this code were borrowed from code written while
+% following along in class.
 clc; clear all; close all;
 
 % Constants
@@ -8,55 +10,87 @@ 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];
 
-% Setting up my ODE vector manually again
-% driving frequency for this test:
-omega = 1;
-ode_system = @(t, theta) [
-    theta(1);     % theta/t
+% 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) [
     theta(2);     % dtheta/dt
-    (F0*cos(omega*t) - m*g*L*sin(theta) - c*theta(2))/(m*L^2) % dtheta2/d2t
-];
+    (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)");
+
 
-[t, theta] = ode45(ode_system, t_span, initial_conditions);
-theta_sol = theta(:, 1);
-dthetadt_sol = theta(:, 2);
+% 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)");
 
-figure;
-plot(t, theta_sol);
 
 %-------------------------------------------------
 % Pendulum's base position
 x0 = 0; y0 = 0;
 
 % Animation loop
-figure;
+f3 = figure;
+ax3 = axes(f3);
 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);
+xlim(ax3, [-L, L]);
+ylim(ax3, [-L, 0.5]);
+xlabel(ax3, 'x (m)');
+ylabel(ax3, 'y (m)');
 
-for i = 1:length(t)
+% 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);
 
     % Update pendulum position
-    x = x0 + L*sin(theta_sol(i));
-    y = y0 - L*cos(theta_sol(i));
+    x = x0 + L*sin(theta_sol2(i));
+    y = y0 - L*cos(theta_sol2(i));
 
     hold on;
 
-    % string
-    plot([x0,x], [y0,y], 'k-', 'LineWidth', 2)
-    % ball
-    plot(x, y, 'ro', 'MarkerSize', 20);
+    set(plot_string, 'XData', [x0,x], 'YData', [y0,y]);
+    set(plot_ball, 'XData', x, 'YData', y);
 
 
     drawnow;
-    pause(0.1);
-    hold off;
-    cla;
+    pause(0.05);
 end
-

+ 65 - 0
Homework4/problem2_3body.m

@@ -0,0 +1,65 @@
+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)")

+ 53 - 0
Homework4/threeBodyODE.m

@@ -0,0 +1,53 @@
+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