function [Fel,Ep] = Force_Potential_3_springs(x, rm, km, lm)
% Elastic force on a mass due to 3 springs, and system potential energy
% Input:
%   x(1,np)   : mass 'x' coordinate (m);  'np' number of positions
%   rm(2,3)   : spring fixed positions (m)
%   km(1,3)   : spring constants (N/m)
%   lm(1,3)   : spring equilibrium lengths (m)
% Output:
%   Fel(2,np) : elastic force on mass (N)
%   Ep(1,np)  : System potential energy (J)
% J.M.Soler, May 2019

np = numel(x);          % number of points
nm = numel(km);         % number of springs
Fel = zeros(2,np);
Ep = zeros(1,np);
for ip = 1:np                 % loop on mass positions
  for im = 1:nm               % loop on springs
    rr = [x(ip);0]-rm(:,im);  % spring vector
    Fel(:,ip) = Fel(:,ip)-km(im)*(1-lm(im)/norm(rr))*rr;
    Ep(ip) = Ep(ip)+0.5*km(im)*(norm(rr)-lm(im))^2;
  end
end

end
