Register to reply 
Error in NBody simulation 
Share this thread: 
#1
Jan2912, 08:41 PM

P: 25

Hello all.
I'm currently writing a program in Matlab that works out the positions of N bodies under the influence of gravity. The code is setup such that it only requires a mass and an initial position and velocity for each body. Any number of bodies can be entered but for testing purposes I am just using two. The code uses either Euler or RK4 (fourthorder RungeKutta integration). The problem I am getting occurs with either type of integration. Firstly, here is the code:
The above image looks pretty good yeah? The Earth orbits the Sun in pretty much a circular orbit. Now, let's zoom in on the Sun. Remember that the Sun also orbits the centre of mass of the Sun/Earth system but it will remain very close to the centre of mass because the Sun/Earth mass ratio is so large. I don't know about you though, but this doesn't look right: I cannot explain this. Can someone please help? Just take the Euler integration...the code is so simple that I can't find the error. I have tried making the Sun body 1 and the Earth body 2, just in case I was referring to an index incorrectly or something but that did nothing. 


#2
Jan2912, 11:16 PM

PF Gold
P: 293

I have not look closely at your code, but given the motion of the sun, I would guess what it would be. You are probably starting the earth to the right of the sun causing the sun to be attracted slightly to the right. When the earth is on the other side of the sun, it may well stop the motion of the sun to the right but thats all it does. You need to start the animation with the proper initial motion of the sun.
Hope this helps... 


#3
Jan3012, 01:00 AM

P: 1,398

edguy99 is right, If you start the earth moving at speed v, and the sun motionless, there is an initial momentum of [itex] m_{earth} v [/itex], wich means you have a net drift of
the center of mass by [tex] v \frac {m_{earth} } {m_{earth} + m_{sun}} [/tex] The orbit of the earth will drift as well, but the yearly drift is only [tex] 2 \pi r \frac { m_{earth} } {m_{earth} + m_{sun}} [/tex] about (1/50000) AU The graph produced for the sun is a cycloid, a superposition of a rotation round the center of mass and a linear motion with (nearly) the same speed. You get the same graph if you graph the position of a point on the circumference of a rolling wheel. 


#4
Jan3012, 05:37 AM

P: 25

Error in NBody simulation
Thank you kindly for your response. After analysing it a bit more I see exactly what you are saying. There isn't an error in the code, aside from the initial conditions which shouldn't be exactly zero for the initial velocity of the Sun.
I have since tried some other examples too and have been able to replicate them (eg the figure 8 which is pretty cool). 


Register to reply 
Related Discussions  
Nbody problem simulation and relativity  Special & General Relativity  14  
Error on virtual body simulation about the min stepsize  Mechanical Engineering  0  
Nbody simulation  General Physics  8  
NBody Gravity Simulation  Introductory Physics Homework  1  
Nbody simulation  Astronomy & Astrophysics  2 