-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravity.m
188 lines (160 loc) · 3.84 KB
/
gravity.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#clear memory
clear all;
pause on;
##----------Start Configuration----------##
### Define environment ###
# The gravitaional constant
global G = 4.5;
# define the initial parameters of the bodies
# here you can define as many bodies as you like in similar fasion
global bodies; # don't touch this line
bodies(1).m = 10; # mass
bodies(1).x = [0,43.761]; # position
bodies(1).v = [-14.359,0]; # velocity
bodies(2).m = 100; # mass
bodies(2).x = [0,38.761]; # position
bodies(2).v = [-4.409,0]; # velocity
bodies(3).m = 400; # mass
bodies(3).x = [0,-10.784]; # position
bodies(3).v = [1.461,0]; # velocity
### Simulation Parameters
# value of delta t
global delta_t = 0.001;
# simulation time
T = 10;
### plot parameters
# axis limits
global lim = 50;
# normal markersize (size of least massive body)
global marker_size = 15
#trace marker size (size of the path trace)
global trace_size = 3;
# frames per unit time
fp_unit = 10;
##----------End Configuration----------##
#### ----- Start Script ------#####
#dont work for single body systems
if(!isvector(bodies))
return;
endif
#the number of bodies
global n = size(bodies)(2);
# Set initial acceleration zero
for k = 1:size(bodies)(2);
bodies(k).a = [0,0];
endfor
# the least mass among all bodies
global least_mass = bodies(1).m;
# Compute least mass
for k = 1:size(bodies)(2);
if(least_mass > bodies(k).m)
least_mass = bodies(k).m;
endif
endfor
# compute relative radius
for k = 1:size(bodies)(2);
bodies(k).r_r = (bodies(k).m / least_mass)^(1/3);
endfor
#Compute the acceleration on each body
function get_acc()
global bodies;
global delta_t;
global n;
global G;
for k1 = 1:n;
bodies(k1).a = [0, 0];
for k2 = 1:n;
if (k2 != k1)
a = G * bodies(k2).m / norm(bodies(k2).x - bodies(k1).x)^3;
bodies(k1).a += a * (bodies(k2).x - bodies(k1).x);
endif
endfor
endfor
endfunction
#calculate the next position of the body in a small delta time
function update_bodies()
global bodies;
global delta_t;
global n;
for k = 1:n;
bodies(k).x += bodies(k).v * delta_t + bodies(k).a * delta_t^2 / 2;
bodies(k).v += bodies(k).a * delta_t;
endfor
endfunction
## function to plot the bodies
# plot_v1 clears the previous plot
function plot_bodies_v1()
global bodies;
global delta_t;
global least_mass;
global n;
global marker_size;
global lim;
n = size(bodies)(2);
cla;
hold "on";
for k = 1:n;
plot(bodies(k).x(1), bodies(k).x(2), ".k", "markersize", bodies(k).r_r * marker_size);
endfor
axis([-lim, lim, -lim, lim], "equal");
grid("on");
endfunction
# external variables for plot_v2
global trace_cnt = 0; # counter
global trace_lim = 10000; # max number of points to plot before reset
# plot_v2 keeps the previous plot
function plot_bodies_v2()
global bodies;
global delta_t;
global n;
global trace_size;
global lim;
global trace_cnt;
global trace_lim;
hold "on";
trace_cnt++;
if(trace_cnt >= trace_lim)
trace_cnt = 0;
hold "off";
endif
for k = 1:n;
plot(bodies(k).x(1), bodies(k).x(2), "*k", "markersize", trace_size);
endfor
axis([-lim, lim, -lim, lim], "equal");
grid("on");
endfunction
# Initial and final values of t
t1 = 0.0;
t2 = T;
# number of delta t between frames
tp = 1 / fp_unit / delta_t;
# counter
cnt = 0;
## Set up plot
clf;
subplot(1,2,1);
cla;
subplot(1,2,2);
cla;
###--- Start simulation --- ###
tstart2 = clock();
tstart = clock();
for t = t1:delta_t:t2;
if(!mod(cnt,tp))
subplot(1,2,1);
plot_bodies_v1();
title(sprintf("t = %0.2f",t));
subplot(1,2,2);
plot_bodies_v2();
title(sprintf("t = %0.2f",t));
exe_time = etime(clock(), tstart);
#printf("t = %f\n",exe_time);
pause(delta_t * tp - exe_time);
tstart = clock();
endif
cnt++;
get_acc();
update_bodies();
endfor
exe_time = etime(clock(), tstart2);
printf("t = %f\n",exe_time);