function [ M, F1, F2 ] = Shaft_Moment( X, Load, R1, R2 )
%
%  This routine computes the moments on a shaft given point loads at
%  specific locations along the shaft.
%
%  Input
%
%    X =   The location along the shaft where the moment is computed
%    Load  an array of the locations and amounts for the loads.
%          [Distance1, Load1; Distance2, Load2; Distance3, Load3; ...]
%    R1    location of the first reaction along the shaft (support bearing)
%    R2    location of the second reaction along the shaft (support bearing)
%
%  Output
%
%    M    The moment
%    F1   The force at reaction 1
%    F2   The force at reaction 2

%  First we need to compute the reaction forces at the bearings.  We will
%  sum the moments around R1 to compute F2, the reaction force at bearing
%  2.

nLoads = size(Load);        %get the number of loads  
moment = 0;

%  Sum the moments at R1 to compute the force at reaction 2

for i = 1: nLoads(1);
      moment = moment + Load(i,2) * (Load(i,1) - R1);
end

F2 = -moment / (R2 - R1);

%  Now sum the forces to compute the reaction force at reaction 1

F1 = -F2;

for i = 1: nLoads(1);
    F1 = F1 - Load(i, 2);
end

%  Now we compute the moment at X

M = 0;
for i = 1: nLoads(1);
    if (X > Load(i,1))
        M = M + Load(i,2) * (X - Load(i,1));
    end
end

%  Add the moments caused by the reactions

if (X > R1)
    M = M + F1 * (X - R1);
end

if (X > R2)
    M = M + F2 * (X - R2);
end

end

