MATLAB: Dcm2quat is returning invalid quaternions

dcm2quatdirection cosine matrixeuler parametersquaternionrotation

I am using dcm2quat to convert multiple direction cosine matrices to quaternions. For several, it is working fine, but for others I am getting results that violate the fundamental constraint on quaternions–that q1²+q2²+q3²+q4²=1.
Here is one example of a DCM I'm having trouble with:
>> ACB
ACB =
0.4970 0.8638 -0.0824
0.8677 -0.4951 0.0435
0.0032 0.0932 0.9956
>> q = dcm2quat(ACB)
q =
0.7067 -0.0176 0.0303 -0.0014
>> q(1)^2+q(2)^2+q(3)^2+q(4)^2
ans =
0.5006
This is a valid DCM. Its transpose is equal to its inverse, multiplying it by its transpose yields the identity matrix, and the sum of the squares of each row and column is 1. Why is MATLAB returning invalid quaternions?
Edit: Also tried running quat2dcm on the result from dcm2quat and got a different DCM than I started with. Something's very wrong.

Best Answer

ACB is not a valid direction cosine matrix. You can see that its determinant is not close to 1:
>> ACB = [ 0.4970 0.8638 -0.0824
0.8677 -0.4951 0.0435
0.0032 0.0932 0.9956]
ACB =
0.4970 0.8638 -0.0824
0.8677 -0.4951 0.0435
0.0032 0.0932 0.9956
>> det(ACB)
ans =
-0.9999
Hence the problems occur when feeding this matrix into the dcm2quat function. So whatever process you are using to construct this dcm is flawed.