File Exchange

## quaternion.m

version 1.7 (142 KB) by

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

Updated

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.

Eike Grundkoetter

### Eike Grundkoetter (view profile)

Eike Grundkoetter

### Eike Grundkoetter (view profile)

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

Nathan Pust

### Nathan Pust (view profile)

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.

q2.RotationMatrix
ans =
-1 0 0
0 0 1
0 1 0

JACINTO COLAN

Dustin Horenburg

Chris Jennings

### Chris Jennings (view profile)

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

### Nathan Pust (view profile)

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: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/. 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);

Chris Jennings

### Chris Jennings (view profile)

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

Chris Jennings

### Chris Jennings (view profile)

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)?

andrea antonello

### andrea antonello (view profile)

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);

Matthew

### Matthew (view profile)

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.

Matthew Anderson

### Matthew Anderson (view profile)

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

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

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.

Matthew Anderson

Amy

### 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

spencer

### spencer (view profile)

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

Windo

### 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

### 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?

Nicolas

### Nicolas (view profile)

Super great ! Thanks a lot

Alessandro

nice work

Ted Shultz

### Ted Shultz (view profile)

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

Ted Shultz