Simulating gravity, forces seem to be miscalculated (need help)
Show older comments
I am trying to simulate how 5-15 different bodies affect eachother in terms of gravity.
This is my first time coding, so needless to say I need some help.
The problem is that the forces acting on the bodies seem to be miscalculated or at least are acting in the wrong direction, for instance all forces are positive in the x-axis. Some of the bodies seem to "run away" when another gets close.
Here is my code so far (sorry if it is a mess).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%GRAVITATION %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
clc;
n=randi([5,15]); %n is the amount of bodies
XYM=rand(3,n)*(1000-10); %A matrix with x- and y-coordinates and mass of each body. (Each column is a
body)
t=0;
v0X=zeros(1,n);
v0Y=zeros(1,n);
G=6.673e-11;
while n>1
sz=XYM(3,:);
c=gradient(XYM(3,:));
scatter(XYM(1,:),XYM(2,:),sz,c,'filled')
whitebg('k')
pause(.02)
%Distances are calculated, also in x and y
m=n;
dist=zeros(m,n);
for i1=1:m
for j1=1:n
dist(j1, i1) = sqrt((XYM(1,i1) - XYM(1, j1)) ^ 2 + ...
(XYM(2,i1) - XYM(2,j1)) ^ 2);
end
end
distX=zeros(m,n);
for i2=1:m
for j2=1:n
distX(j2, i2) = XYM(1, i2) - XYM(1,j2);
end
end
distY=zeros(m,n);
for i3=1:m
for j3=1:n
distY(j3, i3) = XYM(2, i3) - XYM(2,j3);
end
end
%The forces are calculated and summed
F=zeros(m,n);
for i4=1:m
for j4=1:n
F(j4,i4)=(G*XYM(3,i4)*XYM(3,j4))/(dist(i4,j4).^2);
end
end
F(~isfinite(F))=0;
k=distY./distX;
k(isnan(k))=0;
angle=atan(k);
FX=F.*cos(angle);
FY=F.*sin(angle);
FX=sum(FX);
FY=sum(FY);
%Force (FX,FY) -> acceleration (aX,aY) -> velocity (v0X,v0Y) -> distance
travelled (sX,sY)
aX=FX./XYM(3,:);
aY=FY./XYM(3,:);
v0X=v0X+aX*t
v0Y=v0Y+aY*t;
sX=v0X*t+(aX.*t^2)/2;
sY=v0Y*t+(aY.*t^2)/2;
XYM(1,:)=XYM(1,:)+sX;
XYM(2,:)=XYM(2,:)+sY;
%Bodies put together when less or equal to 10 units away from eachother
d=dist<=10;
d=d-eye(m,n);
c=sum(sum(d));
if c>0
[r,c]=find(d);
a=r(1,1);
b=r(2,1);
v0X(:,a)=(XYM(3,a).*v0X(:,a)+XYM(3,b).*v0X(:,b))/(XYM(3,a)+XYM(3,b));
v0Y(:,a)=(XYM(3,a).*v0Y(:,a)+XYM(3,b).*v0Y(:,b))/(XYM(3,a)+XYM(3,b));
v0X(:,b)=[];
v0Y(:,b)=[];
XYM(3,a)=XYM(3,a)+XYM(3,b);
XYM(:,b)=[];
n=n-1;
m=n;
end
t=t+60;
end
What are the flaws of the code, if any? Am I even on the right track?
3 Comments
Guillaume
on 19 Oct 2018
I also don't really have time to go through the code and try to understand it (it needs a lot more comments, each variable should say what its purpose is). You can certainly simplify your code and probably get rid of all the loops. For example, to calculate dist you can use pdist2 or simply:
%no need to preallocate dist, just calculate it all in one go with:
dist = hypot(XYM(1, :) - XYM(1, :)', XYM(2, :) - XYM(2, :)');
Viktor Bodin
on 19 Oct 2018
Viktor Bodin
on 19 Oct 2018
Edited: Viktor Bodin
on 19 Oct 2018
Answers (1)
James Tursa
on 19 Oct 2018
I don't have the time to step through your code, but I don't see where you set the sign of F as an attracting force. Maybe all you need to do is flip the sign of F just before you start calculating the accelerations:
F = -F;
1 Comment
Viktor Bodin
on 19 Oct 2018
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!