First of all, why do you need global variables? You are not calling anything anyway!
Learn to pass variables in as arguments to functions that will need them. Globals are a novice programming crutch that you never really need, but will cause you problems in the future, by making your code buggy and far more difficult to debug.
Next, you keep on talking about acceleration being a constant. Nothing could be further from the truth! While the gravitational constant is a constant, acceleration will vary with time! If the acceleration on your hypothetical earth really was a constant, then your planet would move off in a straight line, at faster and faster velocities, eventually exceeding the speed of light. I hope you are planning on adding relativistic motion to this simulation. Otherwise, when the earth exceeds the speed of light, things might get interesting.
Looking at your code, in fact, you set ax and ay outside of the loop, not changing them ever. So that is exactly what you are doing. Spaceship earth, reporting for duty! This planet is headed outside of the solar system, and soon, the galaxy.
The point is, in order for a planet to maintain an orbit, the accelerations in the x and y directions will indeed vary. In fact, the acceleration of your planet will always be pointed inwards, towards the sun. But the direction of that acceleration will vary, depending where the planet is in its orbit around the sun.
You can think of this in a polar coordinates form. So the acceleration is always radially inwards. This may be what you are thinking, that in polar coordinates, as long as the radius stays constant, then acceleration is indeed constant. But the direction of the radial vector is constantly changing as you move around a circle. The velocity will be orthogonal to the acceleration, if the planet is in orbit. So your planet stays constantly accelerating towards the sun, but stays in orbit. Yes, it seems vaguely paradoxical, but this is how orbital motion works.
So, yes, you need to make G a constant. They call it the gravitational constant for a good reason. But you cannot make ax and ay constant!
I'd also point out that the position of the sun is itself not constant. So a better model has the sun reacting to the forces on it, moving in tiny circles itself. Essentially, the sun and the earth move in concert, each acting on the forces they feel.
Finally, one thing you would realize is that over a long period of time, the predicted positions of those bodies would accumulate errors. You are using Euler's method to integrate what is essentially a system of differential equations. It would be a better choice to use a better method for this problem. So you might formulate the problem as a set of differential equations, then feed that to an ODE solver like ODE45.
I'm not saying ODE45 is what you truly need to use for your problem, but it would be a better way to solve the general problem. The good thing is then you allow the solver to do all the work. Good programming practice uses tools written by experts to solve your problems. Don't get the idea that you need to write these solvers yourself. In fact, too many people seem to think they do. They learned how to solve the problem that way, in some basic class on programming or mathematics, thus ODE solvers, optimization, whatever. Then when it is time for them to do some serious work, they think they need to code up Newton's method themselves. WRONG.
Best Answer