| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- 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
|