function r = equilibrium_3_masses(m,k,p)

% Solution to problem 1 of control 3, Computacion I. 
% Equilibrium points of 3 hanging masses connected by springs
% and under the gravitational force as shown in figure 1
% Input:
%   m(3)   : particle masses (kg)
%   k(5,5) : spring constants (N/m)
%   p(2)   : (x4, x5) coordinates (m)
% Output:
%   r(2,3) : equilibrium points of each mass (m)
% J.M.Soler, Apr.2017

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

% Find the equilibrium mass possitions by solving the 6 equations
%   sum_j(kij*(xj-xi)) = 0,    i=1:3, j=1:5
%   sum_j(kij*(yj-yi)) - mi*g = 0
% for the 3 unknowns xi, then for yi. This is
%   -(k12+k13+k14)*x1 + k12*x2 + k13*x3 = -k14*x4
%    k12*x1 - (k12+k23+k25)*x2 + k23*x3 = -k25*x5
%    k13*x1 + k23*x2 - (k13+k23)*x3     = 0
%   -(k12+k13+k14)*y1 + k12*y2 + k13*y3 = -k14*y4 + m1*g
%    k12*y1 - (k12+k23+k25)*y2 + k23*y3 = -k25*x5 + m2*g
%    k13*y1 + k23*y2 - (k13+k23)*y3     = m3*g
% We write these as A*r=b for eqs. 1-3 and 4-6 separately
x4=p(1); x5=p(2); y4=0; y5=0;
A = [ -(k(1,2)+k(1,3)+k(1,4)), k(1,2), k(1,3)
      k(1,2), -(k(1,2)+k(2,3)+k(2,5)), k(2,3)
      k(1,3), k(2,3), -(k(1,3)+k(2,3)) ];      % this is common for 1-3 and 4-6
b = [ -k(1,4)*x4, -k(1,4)*y4+m(1)*g
      -k(2,5)*x5, -k(2,5)*y5+m(2)*g
               0,            m(3)*g ];  % first column for 1-3, second for 4-6
r = A\b;                                % solve system of eqs. for both columns
r = r';                                 % rearrange as 2x3 matrix

end
