% dforceRTR.m
% forces RTR dyad: Ai->R  Aj->T Aj->R
% slider (j) with CM at Aj=Cj
function out=dforceRTR...
(rAi_,rAj_,rCi_,Fi_,Mi_,Fj_,Mj_)
% rAi_ is the position vector of Ai
% rAj_ is the position vector of Aj=Cj
% rCi_ is the position vector of Ci
% Fi_ is the total force on (i) at Ci including
%     the inertia force at Ci and 
%     the gravitational force at Ci
% Mi_ is the total moment on (i) including
%     the moment of inertia on (i)
% Fj_ is the total force on (j) at Aj=Cj including
%     the inertia force at Cj and 
%     the gravitational force at Cj
% Mj_ is the total moment on (j) including
%     the moment of inertia on (j)

syms FAix FAiy FAjx FAjy 
% FAix is the reaction force on (i) at Ai on x-axis
% FAiy is the reaction force on (i) at Ai on y-axis
FAi_=[FAix, FAiy, 0]; % unknown joint force  at Ai
% FAjx is the reaction force on (j) at Aj on x-axis
% FAjy is the reaction force on (j) at Aj on y-axis
FAj_=[FAjx, FAjy, 0]; % unknown joint force  at Aj

% sum forces on (i) and (j)
% FAi_ + Fi_ + FAj_ + Fj_ = 0_
eqF_ = FAi_+Fi_+FAj_+Fj_;
eqFx = eqF_(1); % (1)
eqFy = eqF_(2); % (2)


rAiAj_ = rAj_ - rAi_; % vector AiAj_ 
% sum of forces of (j) projection onto rAiAj_
% (FAj_+Fj_+ Fij).AiAj_ = 0 and FAij.AiAj_ = 0
% Fij reaction force of (i) on (j) at Aj
% Fij perpendicular on AiAj_ 
eqFi = (FAj_+Fj_)*rAiAj_.'; % (3)

rAiCi_ = rCi_ - rAi_; % vector AiCi_ 
% sum of moments of (i) and (j) about Ai
% AiCi_ x Fi_ + AiAj_ x (Fj_+FAj_) + Mi_ + Mj_
eqMij_ = cross(rAiCi_,Fi_)+cross(rAiAj_,Fj_+FAj_)+Mi_+Mj_;
eqMijz = eqMij_(3); % (4)

% Eqs(1)(2)(3)(4) =>
sol=solve(eqFx, eqFy, eqFi, eqMijz);
FAixs=eval(sol.FAix);
FAiys=eval(sol.FAiy);
FAjxs=eval(sol.FAjx);
FAjys=eval(sol.FAjy);

FAjs_=[FAjxs, FAjys, 0];

% sum of forces of (j)
% FAj_ + Fj_ + Fij_ = 0_ ; 
% Fij_ = -Fji_ reaction force of (i) on (j) at Aj
Fij_ = - Fj_ - FAjs_;
Fijx = Fij_(1);
Fijy = Fij_(2);

% sum of moments of (j) about Aj
% Mij_ + Mj_= 0_; =>
% Mij reaction moment of (i) on (j) 
Mijz = - Mj_(3);
 
out = [FAixs,FAiys,FAjxs,FAjys,Fijx,Fijy,Mijz];
end