Projections function
Created Matlab function utilised by the main Manifold Variance code (MatlabUCMVariance.txt) to calculate te projection of the deviation matrix within and perpendicular to the null space.


_________________________________________

function[TPTIME,TOTIME]= projections(inputfile1,inputfile2,inputfile3)

%This function given in input the file of the mean joint
%configuration,static parameters and the file of a selected trial calculate
%the Jacobian matrix, null space and deviation matrix from the mean joint
%configuration to finally output the projection of the deviation matrix
%into the null space of the jacobian (TPTIME)and the projection
%perpendicular to the null space (TOTIME)throughtout the stance phase of a
%gait cycle 


MEAN= csvread(inputfile1);
STA = csvread(inputfile2);
TRIAL=csvread(inputfile3);
%segment length in metres
LHM = STA(1,1)/1000;%sole of the foot
LAM = STA(1,2)/1000;%toe to ankle
LAK = STA(1,3)/1000;
LKH = STA(1,4)/1000;
LHCM = STA(1,5)/1000;
LHA = STA(1,8)/1000;%heel to ankle
Lalpha = STA(1,6); % Heel angle 
Lbeta = STA(1,7); %Toe angle
J1TIME=zeros(100,1);
J2TIME=zeros(100,1);
J3TIME=zeros(100,1);
J4TIME=zeros(100,1);
J5TIME=zeros(100,1);
J6TIME=zeros(100,1);
J7TIME=zeros(100,1);
J8TIME=zeros(100,1);
TPTIME=zeros(4,1,100);%Multidimensional array with projection of the deviation matrix into the null space for each instant of time of the stance phase
TOTIME=zeros(4,1,100);%Multidimensional array with projection of the deviation matrix perpendicular to the null space for each instant of time of the stance phase

for i=1:100
%angle at the mean joint configuration throughout the stance phase in
%RADIANS
tG = MEAN(i,1); % Ground angle
tA = -MEAN(i,2)*pi/180;% Ankle dorsi/plantarflexion
tK = MEAN(i,3)*pi/180; % Knee flexion/extension 
tH = MEAN(i,4)*pi/180; % Hip flexion/extension
%angle of a selected trial during the stance phase in
%RADIANS
tGT = TRIAL(i,8); % Ground angle
tAT = -TRIAL(i,1)*pi/180;% Ankle dorsi/plantarflexion
tKT = TRIAL(i,2)*pi/180; % Knee flexion/extension 
tHT = TRIAL(i,3)*pi/180; % Hip flexion/extension

%deviation matrix column vector 4x1
DM=[tGT-tG; tAT-tA; tKT-tK; tHT-tH];

if tG >= 0
   J1 = - LHCM*sin(tA+tG-tH+tK+1.5708)-LKH*sin(tA+tG+tK+1.5708)-LHM*sin(Lalpha+tG)-LAK*sin(tA+tG+1.5708);
   J5 = LHCM*cos(tA+tG-tH+tK+1.5708)+LKH*cos(tA+tG+tK+1.5708)+LHM*cos(Lalpha+tG)+LAK*cos(tA+tG+1.5708);
else
   J1 = LAM*sin(tG-Lbeta)-LKH*sin(tA+tG+tK+1.5708)-LAK*sin(tA+tG+1.5708)-LHCM*sin(tA+tG-tH+tK+1.5708);
   J5 = LHCM*cos(tA+tG-tH+tK+1.5708)+LKH*cos(tA+tG+tK+1.5708)+LAK*cos(tA+tG+1.5708)-LAM*cos(tG-Lbeta);
end

%row components of Jacobian Matrix of function x
J2 = -LHCM*sin(tA+tG-tH+tK+1.5708)-LKH*sin(tA+tG+tK+1.5708)-LAK*sin(tA+tG+1.5708);
J3 = -LHCM*sin(tA+tG-tH+tK+1.5708)-LKH*sin(tA+tG+tK+1.5708);
J4 = LHCM*sin(tA+tG-tH+tK+1.5708);
%row components of Jacobian Matrix of function y
J6 = LHCM*cos(tA+tG-tH+tK+1.5708)+LKH*cos(tA+tG+tK+1.5708)+LAK*cos(tA+tG+1.5708);
J7 = LHCM*cos(tA+tG-tH+tK+1.5708)+LKH*cos(tA+tG+tK+1.5708);
J8 = -LHCM*cos(tA+tG-tH+tK+1.5708);

JM= [J1 J2 J3 J4; J5 J6 J7 J8];%Jacobian matrix at the mean joint configuration

J1TIME(i,1)=J1;
J2TIME(i,1)=J2;
J3TIME(i,1)=J3;
J4TIME(i,1)=J4;
J5TIME(i,1)=J5;
J6TIME(i,1)=J6;
J7TIME(i,1) =J7;
J8TIME(i,1) =J8;

NJ= null (JM);%Null space of the Jacobian
%Calculation of the projection of the deviation matrix into the null space
%of J
for l=1:2
    for j=1:4
        P(l,j)= NJ(j,l)* DM(j,1);
    end 
    C(l,1)= sum(P(l,:));
end
for j=1:4
    thetaP1(j,1)= C(1,1)* NJ(j,1);
    thetaP2(j,1)= C(2,1)* NJ(j,2);
end
thetaP=thetaP1+thetaP2;%projection into the null space --> parallel component
thetaO=DM-thetaP;  %component perpendicular to the null space

for k=1:4
    TPTIME(k,1,i)=thetaP(k,1);
    TOTIME(k,1,i)=thetaO(k,1);
end


end



end