#Trying to simulate 2 Body problem. Using Runge-Kutta.

14 messages · Page 1 of 1 (latest)

split wyvern
#

Hello! I am a bored maths student. I have been scratching my head at this for hours now.

How in the hell do i stop this guy from pinging off out of space?

My main loop is simple i can send more on each function if needed.
57 SDL_RenderClear(renderer); 58 59 rungeKutta(&sattelite, &sun, dt); 60 // eulerMethod(&sattelite, &sun, dt); 61 drawCircle(renderer, &sun, 40, 249, 215, 28); 62 drawCircle(renderer, &sattelite, 20, 50, 205, 50); 63 drawVelocity(renderer, &sattelite, dt); 64 drawAcceleration(renderer, &sattelite, dt); 65 66 67 68 printf("--------------------\n"); 69 SDL_RenderPresent(renderer); 70 SDL_Delay(16); 71 }

flint plazaBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

craggy osprey
#

Try giving the satellite a sufficiently large initial velocity. As for why your satellite zooms off the screen, it’s because your acceleration goes to infinity at the center of the sun (since a=-GM/r^2, and r goes to 0).

split wyvern
#

I was wondering if there was a way to parameterise the space. So I can say 1 pixel is equal to x metres

craggy osprey
# split wyvern I have a check which if that happens my acceleration gets set to 0

Yeah, the problem is that the distance becomes zero, not the acceleration (the latter is infinite). As for the second part, the best way is probably to store everything in meters internally and then to convert from meters to pixels every time you draw it with a function that transforms positions in meters to screen positions.

split wyvern
#

Ah i found a potential issue to it all.

#

@craggy ospreyDo you know how todo a runge kutta estimation?

craggy osprey
#

What do you mean?

split wyvern
#

i used the runge kutta method

craggy osprey
#

Yeah, what about it?

split wyvern
craggy osprey
#

Not quite. A better approach would probably be to convert your second-order ODE to a system of first-order ODEs and to solve that with RK4. Also, you don’t need to store acceleration as part of your state. And the steps after the first try to use the slope from the previous step to get a better estimate of the slope at the midpoint or, for the fourth step, at the end of the interval, and all this uses a function that gives the derivative of your state given certain parameters.