| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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)")
|