# Leapfrog Integrator's Only Half Right

This topic is 4173 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm using a leapfrog integrator in my physics engine, but the thing is though is that the only way it can have correct values that nearly match my results with all the other integrators that I've been experimenting with is by multiplying the positions and velocitys by 2. Here is an example code that I've used, only this is a for loop that displays 100 of the results:
    Dim t As Single, x As Single, v As Single, dt As Single, a As Single
Dim i As Long

t = 0
x = 0
v = 0
a = 9.8
dt = 1 / 100

List1.AddItem "t" & "    " & "x" & "    " & "v"

For i = 0 To 99

x = x + v * dt
v = v + a * dt
v = v + -0.5 * a * dt
t = t + dt

'incorrect values:
'List1.AddItem t & "    " & (x) & "    " & (v)

'correct values:
List1.AddItem t & "    " & (x * 2) & "    " & (v * 2)

Next i


Is the formula correct or did I do something wrong to get the results?

##### Share on other sites
Why do you substract half the acceleration times dt from the velocity?

##### Share on other sites
Cause if its positive, the values are totally wrong. I got the code from this anyways, and he used -0.5 as well:

http://einstein.drexel.edu/courses/Comp_Phys/Integrators/leapfrog/simple_leapfrog.c

But the thing is though is that he used X for his dxdv's and deriv's functions when it was suppose to be V. Noticed that right away after taking a look at his Forward Euler integrator and RK4. And on top of that, it was only half way there with the leap frog, so I had to multiply the results by 2.

##### Share on other sites
x = x + v * dt
v = v + a * dt
v = v + -0.5 * a * dt
t = t + dt
...
v = v + a * dt + -0.5 * a * dt
...
v = v + 0.5 * a * dt

dt = 1
a = 4

0 + 0
0 + 4
4 - 2
...
0 + 2
2 + 4
6 - 2

SGD integrator

v = v0 + a*T
d = d0 + v*T

0 + 4
0 + 4
...
4 + 4
8 + 4 // note d0 = 4

real solution

d = v0*T + a*T^2 /2
v = a*T

d = 8

It seems you have a problem.

BTW One of leaprfrogs algorithms isn't required to use velocity explicitly. If you need to get velocity from velocityless integrator, you must recalculate it yourself.