ode45 code for 2 body problem
Show older comments
Hi,
I wrote a code to simulate the orbits of Sirus A & B.
As you can see, I get a Spiral and not an elipse.
What is the probelm?
Thank you
Main_TB()
function Main_TB
global m1 m2
m1=3.978e30; %sirius A mass in kg
m2=2.025e30; %sirius B mass in kg
% about one year in seconds
tspan = [0, 1.578e8];
% initial_condtions = [X1, Y1, U1, V1, X2, Y2, U2, V2]
X1 = -9.991e10;
Y1 = 0;
U1 = 0;
V1 = 11627.08;
X2 = 1.9629e11;
Y2 = 0;
U2 = 0;
V2 = -26231.69;
initial_conditions = [-9.991e10, 0, 0, 11627.08, 1.9629e11, 0, 0, -26231.69];
opt = odeset('AbsTol', 1e-8, 'RelTol', 1e-8);
[t,z] = ode45('TB',tspan, initial_conditions);
plot(z(:,1),z(:,3),"-");
hold on;
plot(z(:,5),z(:,7),"-");
xlabel("X position (m)");
ylabel("Y Position (m)");
title ("Sirius A and B Orbits");
axis equal; %makes sure circular elliptical orbits aren't distored
end
function dz = TB(t,z)
global m1 m2
dz = zeros(8,1);
% Z(1)=X1
% Z(2)=U1 ( dx1/dt)
% Z(3)=Y1
% Z(4)=Y1 (dy1/dt)
% Z(5)=X2
% Z(6)=U2 (dx2/dt)
% Z(7)=Y2
% Z(8)= V2 (dy2/dt)
G=6.67e-11;
R12=sqrt((z(1) - z(5)).^2+(z(3) - z(7)).^2);
dz(1) = z(2);
dz(2) = (G*m2)*(z(5) - z(1))./R12.^3;
dz(3) = z(4);
dz(4) = (G*m2)*(z(7) - z(3))./R12.^3;
dz(5) = z(6);
dz(6) = (G*m1)*(z(1) - z(5))./R12.^3;
dz(7) = z(8);
dz(8) = (G*m1)*(z(3) - z(7))./R12.^3;
end
1 Comment
Torsten
on 17 Feb 2025
This is not a solution, but you forgot to include "opt" in the call to "ode45".
Accepted Answer
More Answers (1)
Walter Roberson
on 17 Feb 2025
0 votes
Use axis equal
Your x axis range is different from your y axis range, so you are seeing a distorted view.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


