sunearthmoonsystem.m 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. addpath("functions");
  2. clear all; clc; close all;
  3. G = 1; m1 = 1000; m2 = 50; m3 = 1; R = 10; r = 1;
  4. initial_conditions = [
  5. 0, 0, ... % x1, y1
  6. R, 0, ... % x2, y2
  7. R+r, 0, ... % x3, y3
  8. 0, 0, ... % vx1, vy1
  9. 0, sqrt((G*m1)/R), ... % vx2, vy2
  10. 0, sqrt((G*m1)/R)+sqrt((G*m2)/r) ... % vx3, vy3
  11. ];
  12. t_span = [0 100];
  13. f = @(t, Y) threeBodyODE(t, Y, G, m1, m2, m3);
  14. options = odeset('RelTol', 1e-6, 'AbsTol', 1e-6);
  15. [t, Y] = ode45(f, t_span, initial_conditions, options);
  16. x1_abs = Y(:, 1); x1 = x1_abs-x1_abs;
  17. y1_abs = Y(:, 2); y1 = y1_abs-y1_abs;
  18. x2_abs = Y(:, 3); x2 = x2_abs-x1_abs;
  19. y2_abs = Y(:, 4); y2 = y2_abs-y1_abs;
  20. x3_abs = Y(:, 5); x3 = x3_abs-x1_abs;
  21. y3_abs = Y(:, 6); y3 = y3_abs-y1_abs;
  22. figure;
  23. xlim([min([x1;x2;x3]), max([x1;x2;x3])]);
  24. ylim([min([y1;y2;y3]), max([y1;y2;y3])]);
  25. step = 10;
  26. for i = 1:step:length(t)
  27. hold on;
  28. plot(x1(1:i), y1(1:i), "Color", "y");
  29. plot(x2(1:i), y2(1:i), "Color", "b");
  30. plot(x3(1:i), y3(1:i), "Color", "g");
  31. plot(x1(i), y1(i), "ro", "MarkerFaceColor", "y", "MarkerSize", 15);
  32. plot(x2(i), y2(i), "bo", "MarkerFaceColor", "b", "MarkerSize", 15);
  33. plot(x3(i), y3(i), "go", "MarkerFaceColor", "g", "MarkerSize", 15);
  34. drawnow;
  35. pause(0.02);
  36. hold off;
  37. if i <= length(t) - step
  38. cla;
  39. end;
  40. end