% dforceRRT.m
% dynamic forces analysis
% RRT dyad: Ai->R  Aj->R Aj->T 
% slider (j) with CM at Aj=Cj
function out=dforceRRT...
(rAi_,rAs_,rAj_,rCi_,Fi_,Mi_,Fj_,Mj_)

% rAi_ is the position vector of Ai
% rAs_ is the position vector of As on the sliding direction
% 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)

rPCi_ = rCi_ - rAj_; % position vector PCi_ 
rPAi_ = rAi_ - rAj_; % position vector PAi_ 
rPAj_ = rAs_ - rAj_; % position vector PAj_ 

% FAj_ projection onto rPAj_ is zero: FAj_.PAj_ = 0 
eqFPAj = FAj_*rPAj_.'; % (3)

% sum of moments of (i) about P
% PCi_ x Fi_ + PAi_ x FAi_ + Mi_ = 0_
eqMi_ = cross(rPCi_,Fi_)+cross(rPAi_,FAi_)+Mi_;
eqMiz = eqMi_(3); % (4)

% Eqs.(1)-(4) =>
sol=solve(eqFx, eqFy, eqMiz, eqFPAj);
FAixs=eval(sol.FAix);
FAiys=eval(sol.FAiy);
FAjxs=eval(sol.FAjx);
FAjys=eval(sol.FAjy);

% sum of moments of (j) about Aj
% MAj_ + Mj_ = 0_
% MAj_ is the reaction moment on (j) of the sliding direction
MAjz = - Mj_(3);

% numerical value
FAjs_=[FAjxs, FAjys, 0];

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

out = [FAixs,FAiys,FAjxs,FAjys,Fijx,Fijy,MAjz];
end