Below is the complete MATLAB code to solve the two-body problem using Newton's law of gravitation and display an animation of the orbit. This script assigns the position vectors of the two masses to the variables x1, y1, x2, and y2 over one full orbital period. Make sure to run this script in MATLAB Online or the desktop version to view the animation.
% Two-Body Orbit Simulation and Animation
% Using Newton's Universal Law of Gravitation
% Parameters
e = 0.7; % Eccentricity
m1 = 1; % Mass of first body
m2 = 4; % Mass of second body
G = 1; % Gravitational constant (normalized)
M = m1 + m2; % Total mass
% Orbital period based on Kepler's third law adjusted for eccentricity
T = 2 * pi / (1 - e)^(1.5);
tspan = linspace(0, T, 1000); % Time span for the simulation
options = odeset('RelTol', 1.e-6); % ODE solver options
%% Initial Conditions
% Semi-major axis (normalized)
a = 1;
% Initial separation at periastron
r0 = a * (1 - e);
% Initial positions (center of mass at origin)
x1_0 = -m2 / M * r0;
y1_0 = 0;
x2_0 = m1 / M * r0;
y2_0 = 0;
% Relative velocity from vis-viva equation
v_rel = sqrt(G * M * (2 / r0 - 1 / a));
% Initial velocities to ensure center of mass is stationary
vx1_0 = 0;
vy1_0 = -m2 / M * v_rel;
vx2_0 = 0;
vy2_0 = m1 / M * v_rel;
% State vector: [x1, y1, vx1, vy1, x2, y2, vx2, vy2]
state0 = [x1_0, y1_0, vx1_0, vy1_0, x2_0, y2_0, vx2_0, vy2_0];
%% Solving the Differential Equations
% Use ode45 to solve the system of ODEs
[t, sol] = ode45(@two_body, tspan, state0, options);
% Extract positions from the solution
x1 = sol(:, 1);
y1 = sol(:, 2);
x2 = sol(:, 5);
y2 = sol(:, 6);
%% Animation (Uncomment to Run on MATLAB Online or Desktop)
% Uncomment the following block to view the animation.
%{
% Parameters for visual representation
k = 0.1;
R1 = k * (m1)^(1/3);
R2 = k * (m2)^(1/3); % Radius of masses for visualization
theta = linspace(0, 2*pi, 100);
% Setup the figure
figure;
axis equal;
hold on;
set(gcf, 'color', 'w');
axis off;
xlim([-2, 5]);
ylim([-2.5, 2.5]);
% Initial positions of the masses
planet = fill(R1 * cos(theta) + x1(1), R1 * sin(theta) + y1(1), 'b');
sun = fill(R2 * cos(theta) + x2(1), R2 * sin(theta) + y2(1), 'r');
pause(1);
% Number of periods to animate
nperiods = 5;
% Animation loop
for j = 1:nperiods
for i = 1:length(t)
% Update positions of the masses
planet.XData = R1 * cos(theta) + x1(i);
planet.YData = R1 * sin(theta) + y1(i);
sun.XData = R2 * cos(theta) + x2(i);
sun.YData = R2 * sin(theta) + y2(i);
drawnow;
end
end
%}
%% Local Function for Differential Equations
function dstate = two_body(~, state)
% Unpack the state vector
x1 = state(1);
y1 = state(2);
vx1 = state(3);
vy1 = state(4);
x2 = state(5);
y2 = state(6);
vx2 = state(7);
vy2 = state(8);
% Compute the distance components
delta_x = x2 - x1;
delta_y = y2 - y1;
distance = sqrt(delta_x^2 + delta_y^2);
% Avoid division by zero
if distance == 0
error('Collision detected: distance between masses is zero.');
end
% Compute accelerations based on Newton's law of gravitation
a_x1 = G * m2 * delta_x / distance^3;
a_y1 = G * m2 * delta_y / distance^3;
a_x2 = -G * m1 * delta_x / distance^3;
a_y2 = -G * m1 * delta_y / distance^3;
% Pack the derivatives into a column vector
dstate = [vx1; vy1; a_x1; a_y1; vx2; vy2; a_x2; a_y2];
end
Explanation of the Code
-
Parameters and Initial Conditions:
- The script begins by defining the eccentricity
e, masses m1 and m2, and the gravitational constant G.
- The semi-major axis
a is set to 1 for normalization.
- Initial positions are calculated to place the center of mass at the origin.
- The initial velocities are determined using the vis-viva equation to ensure the desired eccentricity.
-
Solving the Differential Equations:
- The
ode45 solver is used to integrate the system of differential equations over the time span tspan.
- The positions of both masses over time are extracted from the solution.
-
Animation:
- The animation section is provided but commented out because MATLAB Grader does not support it.
- To view the animation, uncomment the block by removing
%{ and %}.
- The animation visually represents the two masses as circles moving according to their computed trajectories.
-
Local Function two_body:
- This function calculates the derivatives of the state vector.
- It computes the gravitational accelerations based on the current positions of the masses.
- An error is thrown if the masses collide (i.e., the distance between them becomes zero).
Running the Code
-
Without Animation:
- Simply run the script in MATLAB to perform the simulation and compute the trajectories.
- The variables
x1, y1, x2, and y2 will contain the positions of the two masses over time.
-
With Animation:
- To view the animation, uncomment the animation block in the script.
- Run the script in MATLAB Online or the desktop version to see the animated orbit of the two masses.
Notes
- Ensure that your MATLAB environment supports the execution of local functions within scripts.
- The gravitational constant
G is normalized to 1 for simplicity. You can adjust it if working with different units.
- The script assumes that the two masses do not collide. In a real-world scenario, additional handling may be required for close encounters.