syms l1 l2 l3 l4 l5 real
syms phi1 phi2 phi3 phi4 phi0 real
syms L0 real

% 添加假设条件，使表达式在实数域
assumeAlso(l1 > 0);
assumeAlso(l4 > 0);
assumeAlso(L0 > 0);

%雅可比矩阵
j11 = (l1*sin(phi0-phi3)*sin(phi1-phi2))/sin(phi3-phi2);
j12 = (l4*sin(phi0-phi2)*sin(phi3-phi4))/sin(phi3-phi2);
j21 = (l1*cos(phi0-phi3)*sin(phi1-phi2))/(L0*sin(phi3-phi2));
j22 = (l4*cos(phi0-phi2)*sin(phi3-phi4))/(L0*sin(phi3-phi2));
J = [j11 j12; 
    j21 j22]
inv_J = simplify(inv(J))

%雅可比矩阵的逆矩阵
inv_j11 = -cos(phi0 - phi2)/(l1*sin(phi1 - phi2));
inv_j12 = (L0*sin(phi0 - phi2))/(l1*sin(phi1 - phi2));
inv_j21 = cos(phi0 - phi3)/(l4*sin(phi3 - phi4));
inv_j22 = -(L0*sin(phi0 - phi3))/(l4*sin(phi3 - phi4));
inv_J = [inv_j11 inv_j12; 
         inv_j21 inv_j22]
inv_J = simplify(inv(J));
T1 = inv_J.'
T2 = simplify(inv(J).')

%

%测试求逆解效果
clear
phi1 = 0.2;
phi4 = 2.9;

    l5=0.108;
    l1=0.15;
    l2=0.25;
    l3=0.25;
    l4=0.15;
    YD = l4*sin(phi4);
    YB = l1*sin(phi1);
    XD = l5 + l4*cos(phi4);
    XB = l1*cos(phi1); 
    lBD = sqrt((XD - XB)*(XD - XB) + (YD - YB)*(YD - YB));
    A0 = 2*l2*(XD - XB);
    B0 = 2*l2*(YD - YB);
    C0 = l2*l2 + lBD*lBD - l3*l3;
    phi2 = 2*atan2((B0 + sqrt(A0*A0 + B0*B0 - C0*C0)),(A0 + C0));
    phi3 = atan2(YB-YD+l2*sin(phi2),XB-XD+l2*cos(phi2));
    XC = l1*cos(phi1) + l2*cos(phi2);
    YC = l1*sin(phi1) + l2*sin(phi2);
    L0 = sqrt((XC - l5/2)*(XC - l5/2) + YC*YC);
    phi0 = atan2(YC,XC - l5/2);

    j11 = (l1*sin(phi0-phi3)*sin(phi1-phi2))/sin(phi3-phi2);
    j12 = (l4*sin(phi0-phi2)*sin(phi3-phi4))/sin(phi3-phi2);
    j21 = (l1*cos(phi0-phi3)*sin(phi1-phi2))/(L0*sin(phi3-phi2));
    j22 = (l4*cos(phi0-phi2)*sin(phi3-phi4))/(L0*sin(phi3-phi2));
    J = [j11 j12; j21 j22]
    inv_J = inv(J)

F0 = 12;
Tp = 2;
F = [F0;Tp];

T = (J.') * F
F_calc = (inv_J.') * T
% 求逆阵
det = J(1,1)*J(2,2) - J(1,2)*J(2,1);
inv_J_calc = [J(2,2),-J(1,2);-J(2,1),J(1,1)]/det
F_calc_2 = (inv_J.') * T