# How do i measure error?

1. Sep 13, 2014

### DivergentSpectrum

sorry for spamming the boards with all these questions lol- i promise this is my last post regarding n body problems. how do i measure error? i cant really find much on this subject. i want to use adaptive stepsize in my n body simulator im guessing for the n-body problem i base my error on energy and angular momentum:
h(n+1)
∫work dt=KEn+1- KEn
h*n
h(n+1)
∫torque*dt=AMn+1-AMn+1
h*n
of course the integrals would be evaluated using simpsons rule, which is equivalent to RK4 in order.
this gives me 4 equations (actually 4*p where p is the number of particles).
now the way i see it i can do this 2 ways:

the work divided by kinetic energy gained should equal 1, and if the fraction isnt close enough to one i repeat the step.

or work minus kinetic energy should equal zero, so i could find the difference and if it isnt close enough to 0 i repeat the step.

i think both of them have their advantages and drawbacks, for example if i went the subtraction route, then if the difference is really small it doesnt really matter if im dealing with big numbers.
on the other hand, if i use division i might end up with divide by zero problems.

2. Sep 13, 2014

### HallsofIvy

Staff Emeritus
How you measure "error" will depend a lot on what error you are measuring! You mention "Simpson's rule" for integration and Runge-Kutta fourth order for differential equations. Both of those have well defined error rules:
http://en.wikipedia.org/wiki/Simpson's_rule- [Broken] The error in approximating an integral by Simpson's rule is
$$\frac{1}{90}\left(\frac{b- a}{2}\right)^5|f(\xi)|^4$$
Where $\xi$ is a number between a and b. Since we don't know exactly where [itex]\xi]/itex] is, we can say that the error is less than or equal to
$$\frac{1}{90}\left(\frac{b- a}{2}\right)^5M^4$$
where "M" is an upper bound on |f(x)| between a and b.

For the Runge-Kutta fourth order method: Well, that's much more complicated so I will just give the reference:
"Runge-Kutt methods with minimum error bounds" by Anthony Ralston
www.ams.org/.../1962-16-080/S0025-5718-1962-0150954-0/S0025-5718-1962-0150954-0.pdf [Broken]

Last edited by a moderator: May 6, 2017
3. Sep 14, 2014

### DivergentSpectrum

well, here im measuring error in velocity and position using the principles of conservation.

i found runge kutta fehlberg. does anybody have any advice on this?
the optimal step size is given by

(tol*h/(2*|rk4-rk5|))1/4*h
where tol is tolerance rk4 is the answer derived by the 4th order method and rk5 is the answer derived by the 5th order method and h is step size
but im measuring both velocity and position, and an error in velocity probably doesnt necessarily correspond to the same error in position.
do i just multiply the optimal stepsize by h?

4. Sep 15, 2014

### DivergentSpectrum

i implemented rkf45, but its horribly innefficient. the reason being i have only figured out how to implement variable time step for the entire system. if i could use different size time steps for each particle it would be much better, but i think im just going to revert back to old fashioned rk4 for now