Kaynağa Gözat

Add more nbody classwork

Pjotir 3 hafta önce
ebeveyn
işleme
a808a53ff0

+ 48 - 0
classwork/nbodyproblem/sunearthmoonsystem.m

@@ -0,0 +1,48 @@
+addpath("functions");
+clear all; clc; close all;
+
+G = 1; m1 = 1000; m2 = 50; m3 = 1; R = 10; r = 1;
+initial_conditions = [
+    0, 0, ...                   % x1, y1
+    R, 0, ...                   % x2, y2
+    R+r, 0, ...                 % x3, y3
+    0, 0, ...                   % vx1, vy1
+    0, sqrt((G*m1)/R), ...      % vx2, vy2
+    0, sqrt((G*m1)/R)+sqrt((G*m2)/r) ... % vx3, vy3
+];
+
+t_span = [0 100];
+f = @(t, Y) threeBodyODE(t, Y, G, m1, m2, m3);
+options = odeset('RelTol', 1e-6, 'AbsTol', 1e-6);
+[t, Y] = ode45(f, t_span, initial_conditions, options);
+
+x1_abs = Y(:, 1); x1 = x1_abs-x1_abs;
+y1_abs = Y(:, 2); y1 = y1_abs-y1_abs;
+x2_abs = Y(:, 3); x2 = x2_abs-x1_abs;
+y2_abs = Y(:, 4); y2 = y2_abs-y1_abs;
+x3_abs = Y(:, 5); x3 = x3_abs-x1_abs;
+y3_abs = Y(:, 6); y3 = y3_abs-y1_abs;
+
+figure;
+xlim([min([x1;x2;x3]), max([x1;x2;x3])]);
+ylim([min([y1;y2;y3]), max([y1;y2;y3])]);
+step = 10;
+
+for i = 1:step:length(t)
+    hold on;
+
+    plot(x1(1:i), y1(1:i), "Color", "y");
+    plot(x2(1:i), y2(1:i), "Color", "b");
+    plot(x3(1:i), y3(1:i), "Color", "g");
+
+    plot(x1(i), y1(i), "ro", "MarkerFaceColor", "y", "MarkerSize", 15);
+    plot(x2(i), y2(i), "bo", "MarkerFaceColor", "b", "MarkerSize", 15);
+    plot(x3(i), y3(i), "go", "MarkerFaceColor", "g", "MarkerSize", 15);
+    drawnow;
+    pause(0.02);
+    hold off;
+
+    if i <= length(t) - step
+        cla;
+    end;
+end

+ 48 - 0
classwork/nbodyproblem/sunsaturnjupitersystem.m

@@ -0,0 +1,48 @@
+addpath("functions");
+clear all; clc; close all;
+
+G = 6.674e-11; m1 = 1.989e30; m2 = 1.898e27*10; m3 = 5.683e26;
+initial_conditions = [
+    0, 0, ...                   % x1, y1
+    7.78e11, 0, ...             % x2, y2
+    1.43e12, 0, ...             % x3, y3
+    0, 0, ...                   % vx1, vy1
+    0, sqrt((G*m1)/7.78e11), ...% vx2, vy2
+    0, sqrt((G*m1)/1.43e12) ... % vx3, vy3
+];
+
+t_span = [0 365*24*3600*100];
+f = @(t, Y) threeBodyODE(t, Y, G, m1, m2, m3);
+options = odeset('RelTol', 1e-6, 'AbsTol', 1e-6);
+[t, Y] = ode45(f, t_span, initial_conditions, options);
+
+x1 = Y(:, 1);
+y1 = Y(:, 2);
+x2 = Y(:, 3);
+y2 = Y(:, 4);
+x3 = Y(:, 5);
+y3 = Y(:, 6);
+
+figure;
+xlim([min([x1;x2;x3]), max([x1;x2;x3])]);
+ylim([min([y1;y2;y3]), max([y1;y2;y3])]);
+step = 10;
+
+for i = 1:step:length(t)
+    hold on;
+
+    plot(x1(1:i), y1(1:i), "Color", "y");
+    plot(x2(1:i), y2(1:i), "Color", "b");
+    plot(x3(1:i), y3(1:i), "Color", "g");
+
+    plot(x1(i), y1(i), "ro", "MarkerFaceColor", "y", "MarkerSize", 15);
+    plot(x2(i), y2(i), "bo", "MarkerFaceColor", "b", "MarkerSize", 15);
+    plot(x3(i), y3(i), "go", "MarkerFaceColor", "g", "MarkerSize", 15);
+    drawnow;
+    pause(0.02);
+    hold off;
+
+    if i <= length(t) - step
+        cla;
+    end;
+end