function J = Jacobian_4dof(q,L,warm)
%__________________________________________________________________________
% in_args: q, joint vector
%          L, arm's link lengths
%          warm, which arm: left +1 or right -1
% out_args: J, Jacobian matrix 3x4
%
% Decodes input arguments  
 lt = L(1); lt_l = L(2); lt_w = L(3); lt_h = L(4); 
 lr = L(5); l0 = L(6); 
 l1 = L(7); l1_1 = L(8); l1_2 = L(9); l2 = L(10);
 
 s1 = sin(q(1)); c1 = cos(q(1));
 s2 = sin(q(2)); c2 = cos(q(2));
 s3 = sin(q(3)); c3 = cos(q(3));
 s4 = sin(q(4)); c4 = cos(q(4));
% Decodes the desired arm
 if warm == +1, FLAG_sign =  1;     %Left arm
 else           FLAG_sign = -1; end % Right arm     
% Calculates the Jacobian matrix (3x4)
 J(1,1) = l2*s4*(c1*s2*s3-FLAG_sign*s1*c3)+FLAG_sign*l2*c1*c2*c4+...
                                           FLAG_sign*l1*c1*c2;
 J(1,2) = l2*s4*(s1*c2*s3)-FLAG_sign*l2*s1*s2*c4-FLAG_sign*l1*s1*s2;
 J(1,3) = l2*s4*(s1*s2*c3-FLAG_sign*c1*s3);
 J(1,4) = l2*c4*(s1*s2*s3+FLAG_sign*c1*c3)-FLAG_sign*l2*s1*c2*s4;
 
 J(2,1) = 0;
 J(2,2) = FLAG_sign*l2*s2*s3*s4+l2*c2*c4+l1*c2;
 J(2,3) =-FLAG_sign*l2*c2*c3*s4;
 J(2,4) =-FLAG_sign*l2*c2*s3*c4-l2*s2*s4;
 
 J(3,1) = l2*s4*(FLAG_sign*s1*s2*s3+c1*c3)+l2*s1*c2*c4+l1*s1*c2;
 J(3,2) =-FLAG_sign*l2*s4*(c1*c2*s3)+l2*c1*s2*c4+l1*c1*s2;
 J(3,3) =-FLAG_sign*l2*s4*(c1*s2*c3+FLAG_sign*s1*s3);
 J(3,4) =-FLAG_sign*l2*c4*(c1*s2*s3-FLAG_sign*s1*c3)+l2*c1*c2*s4;
%--------------------------------------------------------------------------
end