% Three tensions  -Andy Ruina March 8, 2021
% 3D.
% A mass m at A  is held by three strings/rods anchored at B, C and D.
% The strings/rods are 'two-force members' only having tension or
% compression.
% Gravity g acts and the tensions cause an acceleration a (a vector).
% The positions of A, B, C and D are given as are m,g and a.
% Find the tension in string AB.
%  
% Two methods are shown  1) Matrix solving; 2) dot and cross products

% GIVENS  (more or less random numbers used here
%          consistent units are assumed.)
m = 7; g = 10;  a = [ 1 2  3]';  mg = m*g*[0 0 -1]';  ma = m*a;
rA = [ 2 5 9 ]'; rB = [ 5 6 10]'; rC = [30 -10 50]';  rD = [0 0 100]';

rAB   = rB - rA;          rAC = rC - rA;          rAD = rD - rA;
lamAB = rAB/norm(rAB);  lamAC = rAC/norm(rAC);  lamAD = rAD/norm(rAD);

% Thus the problem is to solve this LMB equation for tension TAB

%  lamAB * TAB + lamAC*TAC + lamAD * TAD  + mg  = ma         (1)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% METHOD 1:  write linear equations in matrix form

lambdamatrix = [lamAB, lamAC, lamAD];

% Equation  (1) turns into:             lambdamatrix * T  = ma - mg (2)
% where T = [TAB  TAC  TAD]' is unknown.
% So, we solve (2) using backslash

T = lambdamatrix\(ma - mg);
TAB = T(1)    % Done, we have found TAB.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% METHOD 2: use a judicious dot product

% Find a vector perpendicular to  lamAC and lamAD

goodvec =  cross(rAC, rAD);

% Dot goodvec with both sides of (1).  The TAC and TAD terms drop out.
%  Solve for TAB

TAB = dot(goodvec, (ma - mg)) / dot(goodvec, lamAB)

disp('Note that the two answers agree.')


