function x = verlet1( force, mass, x0, v0, dt, tmax )

% Solves Newton's equation, using Verlet's method, for one particle in 1D.
% J.M.Soler, Jan.2011
% 
% Input:
%   force : function to calculate the force as f=force(x), in N
%   mass  : particle mass in kg
%   x0    : intial position, in m
%   v0    : initial velocity in m/s
%   dt    : integration time interval in s
%   tmax  : final integration time in s
% Output:
%   x     : position at each time (0:dt:tmax), in m

% Set auxiliary variables and arrays
t = (0:dt:tmax);
nt = length(t);
x = zeros(1,nt);

% Integrate trajectory
a0 = force(x0) / mass;
x(1) = x0;
x(2) = x0 + v0*dt + 0.5*a0*dt^2;
for it = 2:nt-1
    a = force(x(it)) / mass;
    x(it+1) = 2*x(it) - x(it-1) + a*dt^2;
end

end % function verlet1
