function f = rotatingStringForces(r,t,R,w,d0,k,m)
% forces on a string of particles connected by springs 
% and pulled by a rotating arm
% Input:
%   r(3,n) : particle positions (m)
%   t      : time (s)
%   R      : pulling arm length (m)
%   w      : pulling arm angular velocity (rad/s)
%   do     : unstretched spring length (m)
%   k      : spring constant (N/m)
%   m      : particles mass (kg)
% Output:
%   f(3,n) : force on each particle (N)
% Jose M. Soler, Feb.2018

% Set parameters
g = 9.8;    % gravitational acceleration (m/s^2)

% Find spring forces
n = size(r,2);                         % number of particles
r(:,n+1) = [R*cos(w*t);R*sin(w*t);0];  % arm tip position
ri = r(:,2:n+1)-r(:,1:n);              % vector between particles
di = sqrt(sum(ri.^2));                 % distance between particles
di = repmat(di,3,1);                   % extend it to the size of ri
ri = ri./di;                           % normal vector between particles
f = k*(di-d0).*ri;                     % forces from upper spring
f(:,2:n) = f(:,2:n)-k*(di(:,1:n-1)-d0).*ri(:,1:n-1); % from lower spring

% Add gravitational force
f(3,:) = f(3,:)-m*g;

end

