%The function rotates the stress tensor from the principle stress coordinate system to the borehole coodinate system
%Inputs:
    %stress- the stress tensor in the far-field stress frame;
    %alphazs1 The rotation angle of the far-field stress frame around D-axis;
    %alphays The rotation angle of the far-field stress frame around y'-axis;
    %alphazs2 The rotation angle of the far-field stress frame around zm-axis;
    %traj = [boreholeI boreholeA]: A 1 X 2 vector describing inclination and azimuth of the borehole; 
    %All the angles are input referred to the NED frame.
%Outputs:
    %is- the stress tensor in the borehole coordinate frame.
function is=StressRotation3D(stress, alphazs1, alphays, alphazs2, traj) % input angles are all in radian
N1=StrRMatrixZ(-alphazs1);% Function StrRMatrixZ is the rotation of stress around Z-axis
N2=StrRMatrixY(-alphays);% Function StrRMatrixY is the rotation of stress around Y-axis
N3=StrRMatrixZ(-alphazs2);
N4=StrRMatrixZ(traj(2));
N5=StrRMatrixY(traj(1));
is=N5*N4*N1*N2*N3*stress*N3.'*N2.'*N1.'*N4.'*N5.';%Equation (9)
is(abs(is)<10^(-10))=0;