# Euler Method for Orbits

1. Jun 15, 2015

### Timothy S

Hello everyone,

I am curious as to if it is possible to use the Euler Method to solve the gravity differential equations? Would the approximations quickly diverge to inaccurate solutions, or would it stay relatively reliable?

Thanks

2. Jun 15, 2015

### HallsofIvy

Staff Emeritus
I presume you mean the basic "Euler method" for solving differential equations. (Given a differential equation dx/dt= f(x, t) with initial value $x(t_0)= x_0$, calculate $f(t_0,x_0)$ and construct the line through $(t_0, x_0)$ with slope $f(t_0,x_0)$. Follow that line for some short distance, $\Delta t$ to find $x_1= x_0+ f(t_0, x_0)\Delta t$ and $t_1= t_0+ \Delta t$.)

Such an approximation method can be made as precise as you want by taking very short $\Delta t$ but generally, no, the step required to make orbit calcultations would have to be so small as to make the method unreasonable, even on a computer. I would recommend a "Runge-Kutta" method of order 3 or higher.

3. Jun 15, 2015

### MrAnchovy

https://en.wikipedia.org/wiki/N-body_problem

This document explains how NASA's JPL does it using a variable-step, variable-order multistep method: Euler's method is the most simple method of this class being a single-step method of order one and is only useful for demonstrating how quickly errors propagate! An alternative class of methods is the Runge-Kutta methods: the fourth order method referred to as "RK4" is available in a number of libraries for C, Java etc.

4. Jun 15, 2015

### Timothy S

Thank you, good point about the error propagation.

5. Jun 15, 2015

### Staff: Mentor

Hmm. Maybe the info in this thread can help me figure out why my orbital simulation I created Saturday likes to make the Earth spiral into the Sun and then go flying off into interstellar space.

6. Jun 15, 2015

### Timothy S

Your simulation is probably not using a high enough order Runge - Kutta method, and is giving you increasingly inaccurate results as time goes on.

7. Jun 15, 2015

### Staff: Mentor

My simulation is using a couple of laws from my physics book I just learned, lol. I can't even tell you what method it is!

8. Jun 15, 2015

### Timothy S

can you show what equations you are using?

9. Jun 15, 2015

### Staff: Mentor

I'm using the basic equations for motion along with newton's 2nd law and law of gravitation:

X = Xo +Vt+1/2At2
V = Vo + At
F=MA
F = GM1M2/r2

I break up the equations into X and Y components with some trigonometry and then calculate each one using a time step. Then I add the resulting values to their respective variables and repeat. I didn't look up anything on this prior to creating the simulation. I just wanted to see if I could do it using what I've learned in my physics class.

10. Jun 15, 2015

### Timothy S

Oh I see. You cannot do that with orbits as gravity is a differential equation and it is impossible to solve the orbital trajectory explicitly as a function of time.

You must first write the equation of motion of gravity in polar coordinates like so, \ddot{r}-r \dot{ \theta }^{2}=-\frac{GM}{ r^{2} }.
Because theta-dot however complicates things, we replace it with, \dot{ \theta } = \frac{h}{ r^{2} }. h is angular momentum. After this, to find r as a function of time, use a numerical method such as Runge-Kutta to find the trajectory as a function of time.

BTW, Im a high school sophomore too, good luck.

11. Jun 15, 2015

### Staff: Mentor

Gah! I've been outdone by a kid!
I'm 31 and in a basic college physics class, lol.

12. Jun 15, 2015

### Staff: Mentor

Doesn't look like the forum liked your math.

13. Jun 16, 2015

### MrAnchovy

The OP's method is perfectly valid, and all you have done is replaced a numerical solution in cartesian coordinates with a numerical solution in polar coordinates.

However your assumption that angular momentum of a body is constant is only valid in general for a two body problem, where an analytical solution is available negating the need for numerical integration.

14. Jun 16, 2015

### Staff: Mentor

That was the OP.

15. Jun 16, 2015

### MrAnchovy

Oh yes, Drakkith, your method is perfectly valid!

16. Jun 16, 2015

### Staff: Mentor

Dang! I thought I had a solution to why the Earth spiraled into the Sun in my simulation, but I guess not.

17. Jun 16, 2015

### tony873004

You can compare Euler vs. RK4 here. http://orbitsimulator.com/BA/compare.html . It actually uses Euler-Cromer, the difference being that velocities are updated before positions are updated. It's javascript, so you can view the code and look at functions RK4() and function Euler(). Euler's method is only a few lines long. Runge-Kutta 4 is slower, but you can take larger time steps which makes up for the difference. But Euler's method still works pretty well.
That's what I did about 12 years after graduating high school. I remembered my physics, but that capital letter G in all the gravitation formulas still confused me. I decided to give it a go anyway. My units were pixels per iteration. I was so happy when I saw a trajectory get bent by a mass. I spent all night zeroing in on the perfect value for a circular orbit. Months later, I wanted to use meters / second instead of pixels per iteration. So I introduced a "fudge factor" in my equations which made "Earth" take 365.24 days to go around "Sun". Later, I learned that what I came up with was Euler-Crommer method, and my fudge factor was G.

18. Jun 16, 2015

### MrAnchovy

One problem can be too SMALL a time step: you need δt to be large enough that δx/x >> ∈ where ∈ measures the precision of the arithmetical engine (or rather that |δx|/|x| >> ∈ where δx and x are vectors).

19. Jun 16, 2015

### Staff: Mentor

I used meters and seconds in all of the math and just divided the distances by about 2 million or something when I updated their position on screen.

20. Jun 16, 2015

### Staff: Mentor

I'm not familiar with any of that notation. For my timestep I used 12 hours. Since my program runs 60 timesteps per second max, making it much smaller would make the orbit take forever. However, I think I can get around this 60 fps limit by using a 'repeat X number of times' in my program, so it will run each calculation 60X times. Haven't tried it yet though.