Alger logo
𝔸𝕝𝕘𝕖𝕣
About

Verlet

"""
Verlet integration is an integration method used to integrate newtons - law of motion. It is frequently used to find trajectories in molecular dynamics simulation.
The function takes `four` inputs viz,
- `f` : the differential equation
- `x0` : the initial condition. This is a Vector with the first element as initial value for position (x_0) and the second initial condition for velocity (v_0) 
- `tspan`: is the time span for integration. It is a tuple (initial time, final time)
This functionr returns a tuple (x,t):
- `x` is the solution
- `t` is the array containing the time points
Reference:
- https://www.algorithm-archive.org/contents/verlet_integration/verlet_integration.html
Contributed by: [Ved Mahajan](https://github.com/Ved-Mahajan)
"""
function verlet_integration(f, x0, tspan, Δt = 1.0e-3)
    s, e = tspan
    N = Int(floor((e - s) / Δt))
    x = Vector{Float64}(undef, N)
    t = collect(s:Δt:(e-Δt))
    x[1] = x0[1]
    x[2] = x0[1] + x0[2] * Δt + 0.5 * f(x0[1]) * (Δt)^2

    for i in 2:(N-1)
        x[i+1] = 2 * x[i] - x[i-1] + f(x[i]) * (Δt)^2
    end
    return x, t
end
Algerlogo

© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.