# Verlet Integration

Verlet Integration is essentially a solution to the kinematic equation for the motion of any object,

Where is the position, is the velocity, is the acceleration, is the often forgotten jerk term, and is time. This equation is a central equation to almost every Newtonian physics solver and brings up a class of algorithms known as *force integrators*. One of the first force integrators to work with is *Verlet Integration*.

So, let's say we want to solve for the next timestep in . To a close approximation (actually performing a Taylor Series Expansion about ), that might look like this:

This means that if we need to find the next , we need the current , , , etc. However, because few people calculate the jerk term, our error is typically . That said, we can calculate with less knowledge and higher accuracy if we play a trick! Let's say we want to calculate of the *previous* timestep. Again, to a close approximation, that might look like this:

Now, we have two equations to solve for two different timesteps in x, one of which we already have. If we add the two equations together and solve for , we find

So, this means, we can find our next simply by knowing our current , the before that, and the acceleration! No velocity necessary! In addition, this drops the error to , which is great! Here is what it looks like in code:

```
function verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
while (pos > 0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
end
return time
end
```

```
double verlet(double pos, double acc, double dt) {
double prev_pos = pos;
double time = 0;
while (pos > 0) {
time += dt;
double next_pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = pos;
pos = next_pos;
}
return time;
}
```

```
void verlet(double *time, double pos, double acc, double dt) {
double prev_pos, temp_pos;
prev_pos = pos;
*time = 0.0;
while (pos > 0) {
*time += dt;
temp_pos = pos;
pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
}
```

```
static void verlet(double pos, double acc, double dt){
// Note that we are using a temp variable for the previous position
double prev_pos, temp_pos, time;
prev_pos = pos;
time = 0;
while (pos > 0){
time += dt;
temp_pos = pos;
pos = pos*2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
System.out.println(time);
}
```

```
def verlet(pos, acc, dt):
prev_pos = pos
time = 0
while pos > 0:
time += dt
next_pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos, pos = pos, next_pos
```

Unfortunately, this has not yet been implemented in haskell, so here's Julia code:

```
function verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
while (pos > 0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
end
return time
end
```

Unfortunately, this has not yet been implemented in scratch, so here's Julia code:

```
function verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
while (pos > 0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
end
return time
end
```

Unfortunately, this has not yet been implemented in matlab, so here's Julia code:

```
function verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
while (pos > 0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
end
return time
end
```

Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia code:

```
function verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
while (pos > 0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
end
return time
end
```

```
function verlet(pos, acc, dt){
var prev_pos, temp_pos, time;
prev_pos = pos;
time = 0;
while (pos > 0){
time += dt;
temp_pos = pos;
pos = pos*2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
return time;
}
```

```
fn verlet(mut pos: f64, acc: f64, dt: f64) {
let mut prev_pos = pos;
let mut time = 0.0;
while pos > 0.0 {
time += dt;
let temp_pos = pos;
pos = pos * 2.0 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
println!("{}", time);
}
```

Now, obviously this poses a problem, what if we want to calculate a term that requires velocity, like the kinetic energy, ? In this case, we certainly cannot get rid of the velocity! Well, we can find the velocity to accuracy by using the Stormer-Verlet method, which is the same as before, but we calculate velocity like so

Note that the 2 in the denominator appears because we are going over 2 timesteps. It's essentially solving . In addition, we can calculate the velocity of the next timestep like so

However, the error for this is , which is quite poor, but get's the job done in a pinch. Here's what it looks like in code:

```
function stormer_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
# Because acceleration is constant, velocity is straightforward
vel += acc*dt
end
return time, vel
end
```

```
double stormer_verlet(double pos, double acc, double dt) {
double prev_pos = pos;
double time = 0;
double vel = 0;
while (pos > 0) {
time += dt;
double next_pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = pos;
pos = next_pos;
// The acceleration is constant, so the velocity is straightforward
vel += acc * dt;
}
return time;
}
```

```
void stormer_verlet(double *time, double *vel,
double pos, double acc, double dt) {
double prev_pos, temp_pos;
prev_pos = pos;
*vel = 0.0;
*time = 0.0;
while (pos > 0) {
*time += dt;
temp_pos = pos;
pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
*vel += acc * dt;
}
}
```

```
static void stormer_verlet(double pos, double acc, double dt){
// Note that we are using a temp variable for the previous position
double prev_pos, temp_pos, time, vel;
prev_pos = pos;
vel = 0;
time = 0;
while (pos > 0){
time += dt;
temp_pos = pos;
pos = pos*2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
// The acceleration is constant, so the velocity is straightforward
vel += acc*dt;
}
System.out.println(time);
}
```

```
def stormer_verlet(pos, acc, dt):
prev_pos = pos
time = 0
vel = 0
while pos > 0:
time += dt
next_pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos, pos = pos, next_pos
vel += acc * dt
```

Unfortunately, this has not yet been implemented in scratch, so here's Julia code:

```
function stormer_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
# Because acceleration is constant, velocity is straightforward
vel += acc*dt
end
return time, vel
end
```

Unfortunately, this has not yet been implemented in scratch, so here's Julia code:

```
function stormer_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
# Because acceleration is constant, velocity is straightforward
vel += acc*dt
end
return time, vel
end
```

Unfortunately, this has not yet been implemented in matlab, so here's Julia code:

```
function stormer_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
# Because acceleration is constant, velocity is straightforward
vel += acc*dt
end
return time, vel
end
```

Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia code:

```
function stormer_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
# Because acceleration is constant, velocity is straightforward
vel += acc*dt
end
return time, vel
end
```

```
function stormer_verlet(pos, acc, dt){
var prev_pos, temp_pos, time, vel;
prev_pos = pos;
vel = 0;
time = 0;
while (pos > 0){
time += dt;
temp_pos = pos;
pos = pos*2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
vel += acc*dt;
}
return time;
}
```

```
fn stormer_verlet(mut pos: f64, acc: f64, dt: f64) {
let mut prev_pos = pos;
let mut time = 0.0;
while pos > 0.0 {
time += dt;
let temp_pos = pos;
pos = pos * 2.0 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
println!("{}", time);
}
```

Now, let's say we actually need the velocity to calculate out next timestep. Well, in this case, we simply cannot use the above approximation and instead need to use the *Velocity Verlet* algorithm.

# Velocity Verlet

In some ways, this algorithm is even simpler than above. We can calculate everything like so

Which is literally the kinematic equation above, solving for , , and every timestep. You can also split up the equations like so

Here is the velocity Verlet method in code:

```
function velocity_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
end
return time, vel
end
```

```
double velocity_verlet(double pos, double acc, double dt) {
double time = 0;
double vel = 0;
while (pos > 0) {
time += dt;
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
}
return time;
}
```

```
void velocity_verlet(double *time, double *vel,
double pos, double acc, double dt) {
*vel = 0.0;
*time = 0.0;
while (pos > 0) {
*time += dt;
pos += (*vel) * dt + 0.5 * acc * dt * dt;
*vel += acc * dt;
}
}
```

```
static void velocity_verlet(double pos, double acc, double dt){
// Note that we are using a temp variable for the previous position
double time, vel;
vel = 0;
time = 0;
while (pos > 0){
time += dt;
pos += vel*dt + 0.5*acc * dt * dt;
vel += acc*dt;
}
System.out.println(time);
}
```

```
return time, vel
def velocity_verlet(pos, acc, dt):
time = 0
vel = 0
while pos > 0:
time += dt
pos += vel * dt + 0.5 * acc * dt * dt
vel += acc * dt
```

Unfortunately, this has not yet been implemented in haskell, so here's Julia code:

```
function velocity_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
end
return time, vel
end
```

Unfortunately, this has not yet been implemented in scratch, so here's Julia code:

```
function velocity_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
end
return time, vel
end
```

Unfortunately, this has not yet been implemented in matlab, so here's Julia code:

```
function velocity_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
end
return time, vel
end
```

Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia code:

```
function velocity_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
end
return time, vel
end
```

```
function velocity_verlet(pos, acc, dt){
var time, vel;
vel = 0;
time = 0;
while (pos > 0){
time += dt;
pos += vel*dt + 0.5*acc * dt * dt;
vel += acc*dt;
}
return time;
}
```

```
fn velocity_verlet(mut pos: f64, acc: f64, dt: f64) {
let mut time = 0.0;
let mut vel = 0.0;
while pos > 0.0 {
time += dt;
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
}
println!("{}", time);
}
```

Even though this method is more used than the simple Verlet method mentioned above, it unfortunately has an error term of , which is two orders of magnitude worse. That said, if you want to have a simulation with many objects that depend on one another --- like a gravity simulation --- the Velocity Verlet algorithm is a handy choice; however, you may have to play further tricks to allow everything to scale appropriately. These types of simulations are sometimes called *n-body* simulations and one such trick is the Barnes-Hut algorithm, which cuts the complexity of n-body simulations from to

## Example Code

Both of these methods work simply by iterating timestep-by-timestep and can be written straightforwardly in any language. For reference, here are snippets of code that use both the classic and velocity Verlet methods to find the time it takes for a ball to hit the ground after being dropped from a given height.

```
function verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
while (pos > 0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
end
return time
end
function stormer_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
temp_pos = pos
pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos = temp_pos
# Because acceleration is constant, velocity is straightforward
vel += acc*dt
end
return time, vel
end
function velocity_verlet(pos::Float64, acc::Float64, dt::Float64)
prev_pos = pos
time = 0.0
vel = 0.0
while (pos > 0.0)
time += dt
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
end
return time, vel
end
function main()
time = verlet(5.0, -10.0, 0.01);
println("Time for Verlet integration is: $(time)\n")
time, vel = stormer_verlet(5.0, -10.0, 0.01);
println("Time for Stormer Verlet integration is: $(time)")
println("Velocity for Stormer Verlet integration is: $(vel)\n")
time, vel = velocity_verlet(5.0, -10.0, 0.01);
println("Time for velocity Verlet integration is: $(time)")
println("Velocity for velocity Verlet integration is: $(vel)\n")
end
main()
```

```
#include <iostream>
// Simple function for velocity-verlet
double verlet(double pos, double acc, double dt) {
double prev_pos = pos;
double time = 0;
while (pos > 0) {
time += dt;
double next_pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = pos;
pos = next_pos;
}
return time;
}
// Simple function for stormer-verlet
double stormer_verlet(double pos, double acc, double dt) {
double prev_pos = pos;
double time = 0;
double vel = 0;
while (pos > 0) {
time += dt;
double next_pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = pos;
pos = next_pos;
// The acceleration is constant, so the velocity is straightforward
vel += acc * dt;
}
return time;
}
double velocity_verlet(double pos, double acc, double dt) {
double time = 0;
double vel = 0;
while (pos > 0) {
time += dt;
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
}
return time;
}
int main() {
// Note that depending on the simulation, you might want to have the verlet
// loop outside.
// For example, if your acceleration chages as a function of time, you might
// need to also change the acceleration to be read into each of these
// functions
std::cout << verlet(5.0, -10, 0.01) << std::endl;
std::cout << stormer_verlet(5.0, -10, 0.01) << std::endl;
std::cout << velocity_verlet(5.0, -10, 0.01) << std::endl;
}
```

```
#include <stdio.h>
void verlet(double *time, double pos, double acc, double dt) {
double prev_pos, temp_pos;
prev_pos = pos;
*time = 0.0;
while (pos > 0) {
*time += dt;
temp_pos = pos;
pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
}
void stormer_verlet(double *time, double *vel,
double pos, double acc, double dt) {
double prev_pos, temp_pos;
prev_pos = pos;
*vel = 0.0;
*time = 0.0;
while (pos > 0) {
*time += dt;
temp_pos = pos;
pos = pos * 2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
*vel += acc * dt;
}
}
void velocity_verlet(double *time, double *vel,
double pos, double acc, double dt) {
*vel = 0.0;
*time = 0.0;
while (pos > 0) {
*time += dt;
pos += (*vel) * dt + 0.5 * acc * dt * dt;
*vel += acc * dt;
}
}
int main() {
double time, vel;
verlet(&time, 5.0, -10, 0.01);
printf("Time for Verlet integration is: %lf\n",
time);
stormer_verlet(&time, &vel, 5.0, -10, 0.01);
printf("Time and velocity for Stormer Verlet integration is: %lf, %lf\n",
time, vel);
velocity_verlet(&time, &vel, 5.0, -10, 0.01);
printf("Time and velocity for velocity Verlet integration is: %lf, %lf\n",
time, vel);
return 0;
}
```

```
// Submitted by lolatomroflsinnlos
static void verlet(double pos, double acc, double dt){
// Note that we are using a temp variable for the previous position
double prev_pos, temp_pos, time;
prev_pos = pos;
time = 0;
while (pos > 0){
time += dt;
temp_pos = pos;
pos = pos*2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
System.out.println(time);
}
// Simple function for stormer-verlet
static void stormer_verlet(double pos, double acc, double dt){
// Note that we are using a temp variable for the previous position
double prev_pos, temp_pos, time, vel;
prev_pos = pos;
vel = 0;
time = 0;
while (pos > 0){
time += dt;
temp_pos = pos;
pos = pos*2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
// The acceleration is constant, so the velocity is straightforward
vel += acc*dt;
}
System.out.println(time);
}
// Simple function for velocity-verlet
static void velocity_verlet(double pos, double acc, double dt){
// Note that we are using a temp variable for the previous position
double time, vel;
vel = 0;
time = 0;
while (pos > 0){
time += dt;
pos += vel*dt + 0.5*acc * dt * dt;
vel += acc*dt;
}
System.out.println(time);
}
public static void main(String[] args) {
verlet(5.0, -10, 0.01);
stormer_verlet(5.0, -10, 0.01);
velocity_verlet(5.0, -10, 0.01);
}
```

```
def verlet(pos, acc, dt):
prev_pos = pos
time = 0
while pos > 0:
time += dt
next_pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos, pos = pos, next_pos
return time
def stormer_verlet(pos, acc, dt):
prev_pos = pos
time = 0
vel = 0
while pos > 0:
time += dt
next_pos = pos * 2 - prev_pos + acc * dt * dt
prev_pos, pos = pos, next_pos
vel += acc * dt
return time, vel
def velocity_verlet(pos, acc, dt):
time = 0
vel = 0
while pos > 0:
time += dt
pos += vel * dt + 0.5 * acc * dt * dt
vel += acc * dt
return time, vel
def main():
time = verlet(5, -10, 0.01)
print("Verlet")
print("Time: {:.10f}".format(time))
print()
time, vel = stormer_verlet(5, -10, 0.01)
print("Stormer-Verlet")
print("Time: {:.10f}".format(time))
print("Velocity: {:.10f}".format(vel))
print()
time, vel = velocity_verlet(5, -10, 0.01)
print("Velocity Verlet")
print("Time: {:.10f}".format(time))
print("Velocity: {:.10f}".format(vel))
print()
if __name__ == '__main__':
main()
```

```
-- submitted by Jie
type Position = [Double]
type Speed = [Double]
type Time = Double
type Particle = (Position, Speed, Acceleration, Time)
type Acceleration = [Double]
verletStep :: (Particle -> Acceleration)
-> Time
-> Particle
-> Particle
-> Particle
verletStep acc dt (xOld, _, aOld, _) (x, v, a, t) = (x', v', a', t+dt)
where
x' = zipWith3 (\xOld x a -> 2*x - xOld + a*dt^2 ) xOld x a
v' = zipWith3 (\v a aOld -> v + 0.5*(aOld + a)*dt) v a aOld
a' = acc (x', v', [], t+dt)
trajectory :: (Particle -> Acceleration)
-> Time
-> Particle
-> [Particle]
trajectory acc dt [email protected](x, v, a, t0) = t
where
t = p0 : p1 : zipWith (verletStep acc dt) t (tail t)
p1 = (x', v', acc (x', v', [], t0+dt), t0+dt)
x' = zipWith3 (\x v a -> x + v*dt + 0.5*a*dt^2 ) x v a
v' = zipWith (\v a -> v + a*dt) v a
freeFall :: Particle
freeFall = last $ takeWhile (\([x],_,_,_) -> x > 0) $ trajectory acc dt p0
where
p0 = ([5], [0], [-10], 0)
dt = 0.001
acc _ = [-10]
```

```
% Submitted by P. Mekhail
% Parameters to change
n = 400; % Number of steps
x0 = 5; % Ball starting height(in metres)
v0 = 0; % Ball starting velocity (+ive is up)
dt = 0.01; % Time step (in seconds)
eff = 0.4; % Ball efficency when bouncing
A = @(x) -10; % Acceleration as a function of position
bounce = 1; % Do you want the ball to bounce?
% Making position and time vectors
x = zeros(n,1);
t = 0:dt:n*dt-dt;
% Setting the initial conditions
x(1) = x0;
x(2) = x0 + v0*dt + 0.5*A(x0)*dt^2;
% Runnin Verlet Integration
for i = 2:n-1
xnew = 2*x(i)-x(i-1)+A(x(i))*dt^2;
if bounce
if xnew > 0
% If you haven't hit the ground keep going
x(i+1) = xnew;
else
% If you have calculated velocity and invert its sign
v = sqrt(eff)*(xnew-x(i-1))/(2*dt);
x(i+1) = x(i) - v*dt + 0.5*A(x(i))*dt^2;
end
else
x(i+1) = xnew;
end
end
plot(t,x)
title('Ball''s Trajectory')
xlabel('Time (s)'); ylabel('Height (m)');
```

Submitted by P. Mekhail

```
function verlet(pos, acc, dt){
var prev_pos, temp_pos, time;
prev_pos = pos;
time = 0;
while (pos > 0){
time += dt;
temp_pos = pos;
pos = pos*2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
return time;
}
function stormer_verlet(pos, acc, dt){
var prev_pos, temp_pos, time, vel;
prev_pos = pos;
vel = 0;
time = 0;
while (pos > 0){
time += dt;
temp_pos = pos;
pos = pos*2 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
vel += acc*dt;
}
return time;
}
function velocity_verlet(pos, acc, dt){
var time, vel;
vel = 0;
time = 0;
while (pos > 0){
time += dt;
pos += vel*dt + 0.5*acc * dt * dt;
vel += acc*dt;
}
return time;
}
console.log(verlet(5.0, -10, 0.01));
console.log(stormer_verlet(5.0, -10, 0.01));
console.log(velocity_verlet(5.0, -10, 0.01));
```

```
fn verlet(mut pos: f64, acc: f64, dt: f64) {
let mut prev_pos = pos;
let mut time = 0.0;
while pos > 0.0 {
time += dt;
let temp_pos = pos;
pos = pos * 2.0 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
println!("{}", time);
}
fn stormer_verlet(mut pos: f64, acc: f64, dt: f64) {
let mut prev_pos = pos;
let mut time = 0.0;
while pos > 0.0 {
time += dt;
let temp_pos = pos;
pos = pos * 2.0 - prev_pos + acc * dt * dt;
prev_pos = temp_pos;
}
println!("{}", time);
}
fn velocity_verlet(mut pos: f64, acc: f64, dt: f64) {
let mut time = 0.0;
let mut vel = 0.0;
while pos > 0.0 {
time += dt;
pos += vel * dt + 0.5 * acc * dt * dt;
vel += acc * dt;
}
println!("{}", time);
}
fn main() {
verlet(5.0, -10.0, 0.01);
stormer_verlet(5.0, -10.0, 0.01);
velocity_verlet(5.0, -10.0, 0.01);
}
```