I tried that, but without results.
Kx = sym('Kx');
Kr = sym('Kr');
Kx_hat = sym('Kx_hat');
Kr_hat = sym('Kr_hat');
gamma_x = sym('gamma_x');
gamma_r = sym('gamma_r');
Lambda = sym('Lambda');
e = sym('e');
P = sym('P');
DKx = Kx_hat - Kx;
DKr = Kr_hat - Kr;
V = e'*P*e + trace(DKx'*(gamma_x^-1)*DKx*abs(Lambda)) + trace(DKr'*(gamma_r^-1)*DKr*abs(Lambda));
diff_V = diff(V,e,DKx,DKr)