function ft = totalForce(x, v, rm, km, lm, mu)
% Total horizontal force on a mass due to 3 springs plus friction
% Input:
%   x         : mass position (m)
%   v         : mass velocity (m/s)
%   rm(2,3)   : spring fixed positions (m)
%   km(1,3)   : spring constants (N/m)
%   lm(1,3)   : spring equilibrium lengths (m)
%   mu        : friction coefficient
% Output:
%   ft        : total force on mass in x direction (N)
% J.M.Soler, May 2019

% Set internal parameter
vtol = 1e-12;   % velocity tolerance (m/s)

% Find elastic force
fel = Force_Potential_3_springs(x, rm, km, lm);

% Add friction
ff = mu*abs(fel(2));                % friction force (absolute value)
if abs(v)<vtol && ff>abs(fel(1))
  ft = 0;                           % friction compensates elastic force
else
  ft = fel(1)-ff*sign(v);
end

end
