In order to check we are getting the correct result this page starts with a euler angle, converts it to a quaternion, then converts it back to to euler angle. When we have done the complete rond trip we end up with the same angles which gives confidence that the method is correct.

So lets start with the following euler angle:

heading = 10 degrees

attitude = 20 degrees

bank = 30 degrees

We then convert to quaternion using the first method on this page. Note that most maths libraries use radians so you will probably need to convert degrees to radians by multiplying by PI/180 = 0.01745.

c1
= cos(5 deg) = 0.9962

c2 = cos(10 deg) = 0.9848

c3
= cos(15 deg) = 0.9659

s1 = sin(5 deg) = 0.0872

s2
= sin(10 deg) = 0.1736

s3 = sin(15 deg) = 0.2588

So using the formular gives the following result:

qw
= 0.9437

qx = 0.2685

qy = 0.1277

qz
= 0.1448

We now convert back to euler angles using the method on this page.

First we work out the heading part: this involves using the atan2 function. Implemetations of this function may vary, so be careful about the order of the operands, this page describes the issues.

tan(heading y part): usually the first operand= 0.1633

tan(heading x part): usually the second operand= 0.9254

I
used atan(y/x) instead of atan2(y,x) as I was looking up the
values in 4 figure tables instead of using a computer program.
However, because the angles are all in the first quadrant, I
believe these are equivalent since in fist quadrant:

tan(a)
= sin(a)/cos(a) = opposite/adjacent = y/x

and

atan2(y,x)
= atan2(opposite,adjacent)

tan(heading) = 0.1764

heading = 10 degrees

Calculating the attitude:

sin(attitude) = 0.3419

attitude = 20 degrees

Calculating the bank:

tan(bank y part) = 0.4698

tan(bank x part) = 0.8138

tan(bank) = 0.5773

bank = 30 degrees

An OpenOffice.org with these calulations is here