File Exchange

image thumbnail


version 1.7 (142 KB) by

quaternion class, vectorized, converts among rotation representations, numerical Euler propagation



View License

quaternion.m is a matlab class that implements quaternion mathematical operations, 3 dimensional rotations, transformations of rotations among several representations, and numerical propagation of Euler’s equations for rotational motion. All quaternion.m class methods except PropagateEulerEq are fully vectorized.

Comments and Ratings (23)

s = 0.5 / sqrt(trace+ 1.0); instead of s = 0.5 / sqrtf(trace+ 1.0);

Nathan Pust

I am revisiting my comment below (21 March) with regard to Chris Jennings comment. If you extend my example to output q2.RotationMatrix (as shown below), you can see that the second and third columns are negative compared to R. Multiplying the vector [0 0 1] by both R and R2 would result in different vectors. Therefore, they are not equivalent.

ans =
-1 0 0
0 0 1
0 1 0


For your example q = [0 0 -0.707 +0.707]
the rotation angle is 180 degrees, so there are two symmetical but opposite rotations that would achieve the same result. I haven't delved to check this, but perhaps both answers are equally right?

Nathan Pust

I believe that I have found a bug in this submission. I believe the rotation matrix to quaternion conversion is oversimplified and doesn't account for some cases. Look at the following execution and output. q and q2 should be equivalent but are not.

>> a = sqrt(2)/2;
>> q = quaternion(0,0,-a,a)

q = (0 ) + i(0 ) + j(-0.70711 ) + k(0.70711 )
>> R = q.RotationMatrix

R =

-1 0 0
0 0 -1
0 -1 0

>> q2 = quaternion.rotationmatrix(R)

q2 = (0 ) + i(0 ) + j(0.70711 ) + k(0.70711 )
q2 = quaternion.rotationmatrix(R)

I replaced the code in RotMat2e with a MATLAB port of the C++ code found here: Then everything works for all of the cases that I have tried so far.

Matthew Kelly

Matthew Kelly (view profile)

Nice job!

Feature request: It would be great to compute the angular velocity vector that is associated with the slerp method. For example:

[q,w] = q1.slerp(q2,t,tRate);

I'm starting to appreciate how well this is written. Thank you Mr Tincknell

very impressive tool. quaterniontest and quaterniondemo2 work great, but no mention of how to run quaterniondemo (which fails due to no inputs for time, sap)?

I have a question: how do you use the function "Integral" (line 1474 in quaternion.m)?
I'm trying with:

q0 = quaternion.rotationmatrix(R);
q = quaternion.integrateomega.Integral(q0,t,w);

But I keep getting this error:

Not enough input arguments.

Error in quaternion/Integral (line 1520)
[w, dim] = finddim( w, 3 );

Error in quaternion.integrateomega (line 2188)
q = Integral( quaternion(1,0,0,0), varargin{:} );

Error in test (line 35)
q = quaternion.integrateomega.Integral(q0,t,w);


Good idea, well executed. Also
Good pdf documentation provided.

Nit-picking: MATLAB help documentation could be much improved. Test could be put into unit-test framework.

Excellent code -- had one issue thus far, see example:

aa = zeros(3,3,2);
aa(:,:,1) = RotationMatrix(quaternion.rand);
aa(:,:,2) = RotationMatrix(quaternion.rand);

Warning: repmat(A,M) or repmat(A,M,N) where M or N is an empty array will return an error in a
future release. Replace empty array inputs with 1 instead.
> In quaternion>quaternion.normalize at 983
In quaternion>quaternion.rotationmatrix at 2092
In quaternion>quaternion.normalize at 983
In quaternion>quaternion.rotationmatrix at 2092

I think the fix is simply to make nel = 1, when isempty(nel) is true before calling the repmat.


Amy (view profile)

I'd like to commend you for a powerful set of scripts. I learned a lot by running the demo and changing the parameters. Great contribution.
Is there a way for me to use your code to simulate a wobbling rigid body? I think about the out-of-balance case like an unbalanced tire rotating in space.

Rody Oldenhuis

Rody Oldenhuis (view profile)


spencer (view profile)

Are the euler angles defined as "body-fixed" or "world-fixed"?


Windo (view profile)

This is great stuff. Concise and functional. However, I have a problem with function 'unitvector'. It is used in several places in the code but it is nowhere to be seen. Here's an example error message when trying to use 'OmegaAxis':

1492 [axis, omega] = unitvector( omegav, 1 );
Undefined function 'unitvector' for input arguments of type 'double'.

Error in quaternion/OmegaAxis (line 1492)
[axis, omega] = unitvector( omegav, 1 );


Felix (view profile)

Thank you for your nice work.

When i use the function "interp1", MATLAB warns the function "isrow" undefined. Is this a bug?


Super great ! Thanks a lot


nice work

Ted Shultz

This is a nice full featured quaternion class with excellent documentation and clean code.

Ted Shultz



Changes: revised error messages, corrected repmat syntax
New methods: ComplexMatrix, complexmatrix, integrateomega, Integral, ModifiedRodrigues, modifiedrodrigues, Rodrigues, rodrigues, RotateTensor


Added missing unitvector function for OmegaAxis method


change normalize, rand; fix isequal*, rotateutov; add dot, interp1, randRot


added quaternion.OmegaAxis to get angular velocity vectors from time series of quaternions


A few new methods and small bug fixes


fixed bugs, added some new methods

MATLAB Release
MATLAB 8.2 (R2013b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video