# quatrotate

Rotate vector by quaternion

## Syntax

`n = quatrotate(q,r)`

## Description

`n = quatrotate(q,r)` calculates the rotated vector, `n`, for a quaternion, `q`, and a vector, `r`. `q` is either an `m`-by-4 matrix containing `m` quaternions, or a single 1-by-4 quaternion. `r` is either an `m`-by-3 matrix, or a single 1-by-3 vector. `n` returns an `m`-by-3 matrix of rotated vectors. Each element of `q` and `r` must be a real number. Additionally, `q` has its scalar number as the first column.

The quaternion has the form of

$q={q}_{0}+i{q}_{1}+j{q}_{2}+k{q}_{3}$

The vector has the form of

$v=i{v}_{1}+j{v}_{2}+k{v}_{3}$

The rotated vector has the form of

${v}^{\prime }=\left[\begin{array}{c}{v}_{1}\\ {v}_{2}\\ {v}_{3}\end{array}\right]=\left[\begin{array}{ccc}\left(1-2{q}_{2}^{2}-2{q}_{3}^{2}\right)& 2\left({q}_{1}{q}_{2}+{q}_{0}{q}_{3}\right)& 2\left({q}_{1}{q}_{3}-{q}_{0}{q}_{2}\right)\\ 2\left({q}_{1}{q}_{2}-{q}_{0}{q}_{3}\right)& \left(1-2{q}_{1}^{2}-2{q}_{3}^{2}\right)& 2\left({q}_{2}{q}_{3}+{q}_{0}{q}_{1}\right)\\ 2\left({q}_{1}{q}_{3}+{q}_{0}{q}_{2}\right)& 2\left({q}_{2}{q}_{3}-{q}_{0}{q}_{1}\right)& \left(1-2{q}_{1}^{2}-2{q}_{2}^{2}\right)\end{array}\right]\left[\begin{array}{c}{v}_{1}\\ {v}_{2}\\ {v}_{3}\end{array}\right]$

## Examples

Rotate a 1-by-3 vector by a 1-by-4 quaternion:

```q = [1 0 1 0]; r = [1 1 1]; n = quatrotate(q, r) n = -1.0000 1.0000 1.0000```

Rotate a 1-by-3 vector by a 2-by-4 quaternion:

```q = [1 0 1 0; 1 0.5 0.3 0.1]; r = [1 1 1]; n = quatrotate(q, r) n = -1.0000 1.0000 1.0000 0.8519 1.4741 0.3185```

Rotate a 2-by-3 vector by a 1-by-4 quaternion:

```q = [1 0 1 0]; r = [1 1 1; 2 3 4]; n = quatrotate(q, r) n = -1.0000 1.0000 1.0000 -4.0000 3.0000 2.0000```

Rotate a 2-by-3 vector by a 2-by-4 quaternion:

```q = [1 0 1 0; 1 0.5 0.3 0.1]; r = [1 1 1; 2 3 4]; n = quatrotate(q, r) n = -1.0000 1.0000 1.0000 1.3333 5.1333 0.9333```

## References

[1] Stevens, Brian L., Frank L. Lewis, Aircraft Control and Simulation, Wiley–Interscience, 2nd Edition.