0% found this document useful (0 votes)
3 views

ThreeBodyProblemCode

The document outlines a MATLAB function that simulates the gravitational interactions between three masses using the ODE45 solver. It defines the positions and velocities of the masses, calculates gravitational forces, and updates their accelerations accordingly. The results are visualized through various plots showing the motion of the masses over time.

Uploaded by

stephanie.schrad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ThreeBodyProblemCode

The document outlines a MATLAB function that simulates the gravitational interactions between three masses using the ODE45 solver. It defines the positions and velocities of the masses, calculates gravitational forces, and updates their accelerations accordingly. The results are visualized through various plots showing the motion of the masses over time.

Uploaded by

stephanie.schrad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1

2
3
4
5 clear all;
6 %% definethe function that returns the slopes
7 function [dxdt] = Gravity2BODE45(t,x_in,m1,m2,m3)
8 %% redefine the input array for convienience
9 x1 = x_in(1); %element 1 in is the x1-position
10 vx1 = x_in(2); %element 2 in is the x1-vel
11 y1 = x_in(3); %element 3 in is the y1-position
12 vy1 = x_in(4); %element 4 in is the y1-vel
13 x2 = x_in(5); %element 5 in is the x2-position
14 vx2 = x_in(6); %element 6 in is the x2-vel
15 y2 = x_in(7); %element 7 in is the y2-position
16 vy2 = x_in(8); %element 8 in is the y2-vel
17 x3 = x_in(9); %element 9 in is the x3-position
18 vx3 = x_in(10); %element 10 in is the x3-vel
19 y3 = x_in(11); %element 11 in is the y3-position
20 vy3 = x_in(12); %element 12 in is the y3-vel
21
22 %%
23 %% now do the physics
24 G=1.1904*10^-8;%6.6743*10^(-11);
25 r1 = sqrt((x1-x2).^2+(y1-y2).^2); %distance between m1 and m2
26 r2 = sqrt((x2-x3).^2+(y2-y3).^2); %distance between m2 and m3
27 r3 = sqrt((x3-x1).^2+(y3-y1).^2); %distance between m3 and m1
28 Fg1 = -G*m1*m2./r1./r1; %gravity force between m1 and m2
29 Fg2 = -G*m2*m3./r2./r2; %gravity force between m2 and m3
30 Fg3 = -G*m3*m1./r3./r3; %gravity force between m3 and m1
31 ax1 = ((Fg1.*(x1-x2)./r1/m1)+(Fg3.*(x1-x3)./r3/m1)); %calculate the cc
32 ay1 = ((Fg1.*(y1-y2)./r1/m1)+(Fg3.*(y1-y3)./r3/m1)); %calculate the acc
33 ax2 = ((Fg1.*(x2-x1)./r1/m2)+(Fg2.*(x2-x3)./r2/m2)); %calculate the cc
34 ay2 = ((Fg1.*(y2-y1)./r1/m2)+(Fg2.*(y2-y3)./r2/m2)); %calculate the acc
35 ax3 = ((Fg3.*(x3-x1)./r3/m3)+(Fg2.*(x3-x2)./r2/m3)); %calculate the cc
36 ay3 = ((Fg3.*(y3-y1)./r3/m3)+(Fg2.*(y3-y2)./r2/m3)); %calculate the acc
37
38 %%
39 %% define the output array
40 dxdt = zeros(1,12);
41 dxdt(1) = vx1; %element 1 out is dx1/dt = vel.
42 dxdt(2) = ax1; %element 2 out is dvx1/dt = acc.
43 dxdt(3) = vy1; %element 3 out is dy1/dt = vel.
44 dxdt(4) = ay1; %element 4 out is dvy1/dt = acc.
45 dxdt(5) = vx2; %element 5 out is dx2/dt = vel.
46 dxdt(6) = ax2; %element 6 out is dvx2/dt = acc.
47 dxdt(7) = vy2; %element 7 out is dy2/dt = vel.
48 dxdt(8) = ay2; %element 8 out is dvy2/dt = acc.
49 dxdt(9) = vx3; %element 5 out is dx3/dt = vel.
50 dxdt(10) = ax3; %element 6 out is dvx3/dt = acc.
51 dxdt(11) = vy3; %element 7 out is dy3/dt = vel.
52 dxdt(12) = ay3; %element 8 out is dvy3/dt = acc.
53 end
54 %%
55 %% set initial parameters
56 m1 = 1;%1.989*10^30; %mass [kg]
57 m2 = 3*10^-6;%1.989*10^30; %mass [kg]
58 m3 = 3.69*10^-8;%1.989*10^30; %mass [kg]
59 %%
60 %% the ODE45 gunction can only take an input function with
61 %% two varriables: t (time)
62 %% x (an array of all the things that will change)
63 %% so we plug in all the other constants and parameters here
64 %% e.g. g(x,y) = f(x,y,z) where z = #
65 %% so, redefine the function with all the extra parameters in place
66
67 FG2B = @(t,x_in)Gravity2BODE45(t,x_in,m1,m2,m3);
68 x10 = 0; %initial positions
69 y10 = 0;
70 vx10 = 0; %initial velocities
71 vy10 = 0;
72
73
74
75
76
77
78
79
80
81
82
83 x20 = 1;
84 y20 = 0.00257;
85 vx20 = 0; %initial velocities
86 vy20 = -0.00003;
87
88 x30 = 1.00257; %initial positions
89 y30 = 0;
90 vx30 = 0; %initial velocities
91 vy30 = -0.000041;
92
93 InitCon = [x10,vx10,y10,vy10,x20,vx20,y20,vy20,x30,y30,vx30,vy30]; %all init con
94 t0 = 0; %timing
95 tf = 200000;%60*60*24*365*4;
96 Nt = 1000;
97 times = linspace(t0,tf,Nt);
98 %times = [t0,tf];
99 %%
100 %% call the ode45 --- this is doing all the physics
101 opts = odeset('RelTol',1e-5,'AbsTol',1e-5);
102 %% other options that can be set: 'InitialStep',1e-8,'MaxStep',1e10
103 [time,x_out] = ode45(FG2B,times,InitCon,opts);
104
105 %%
106 %% relabel output for easy understanding
107 x1 = x_out(:,1);
108 vx1 = x_out(:,2);
109 y1 = x_out(:,3);
110 vy1 = x_out(:,4);
111 x2 = x_out(:,5);
112 vx2 = x_out(:,6);
113 y2 = x_out(:,7);
114 vy2 = x_out(:,8);
115 x3 = x_out(:,9);
116 vx3 = x_out(:,10);
117 y3 = x_out(:,11);
118 vy3 = x_out(:,12);
119 %%
120 subplot(2,2,1)
121 xcm = (x1*m1+x2*m2+x3*m3)/(m1+m2+m3);
122 ycm = (y1*m1+y2*m2+y3*m3)/(m1+m2+m3);
123 plot(x1,y1,'-o',x2,y2,'-o',x3,y3,'-o',xcm,ycm,'*')
124 %%
125 subplot(2,2,2)
126 plot(time,x1,time,y1,...
127 time,x2,time,y2,...
128 time,x3,time,y3,...
129 time,xcm,'*',time,ycm,'*')
130 %%
131 subplot(2,2,3)
132 r1 = sqrt((x1-x2).^2+(y1-y2).^2);
133 r2 = sqrt((x2-x3).^2+(y2-y3).^2);
134 r3 = sqrt((x3-x1).^2+(y3-y1).^2);
135 dx1 = (x1-x2);
136 dx2 = (x2-x3);
137 dx3 = (x3-x1);
138 dy1 = (y1-y2);
139 dy2 = (y2-y3);
140 dy3 = (y3-y1);
141 plot(dx1,dy1,'.',dx2,dy2,'.',dx3,dy3,'.')
142 %%
143 subplot(2,2,4)
144 Nplot=max(size(time));
145 for i=1:Nplot
146 plot(x1(1:i),y1(1:i),'-o',x2(1:i),y2(1:i),'-o',x3(1:i),y3(1:i),'-o',...
147 xcm(1:i),ycm(1:i),'*')
148 drawnow
149 end

You might also like