Planetary orbit (two-body) Open ↗
Code
% A planet around a star at the origin, with GM = 1
f = @(tt, u) [u(3); u(4); -u(1)/norm(u(1:2))^3; -u(2)/norm(u(1:2))^3];
[tt, u] = ode45(f, [0 12], [1; 0; 0; 1.25]);
plot(u(:, 1), u(:, 2), 'LineWidth', 1.8), hold on
scatter(0, 0, 160, [0.95 0.65 0.1], 'filled')
scatter(u(1, 1), u(1, 2), 40, 'k', 'filled')
hold off
axis equal
title('Kepler orbit from ode45')
subtitle(sprintf('energy drift %.1e', abs(0.5*sum(u(end, 3:4).^2) - 1/norm(u(end, 1:2)) - (0.5*1.25^2 - 1))))