# dcm2quat

Convert direction cosine matrix to quaternion

## Syntax

```q = dcm2quat(n) q = dcm2quat(n,action) q = dcm2quat(n,action,tolerance) ```

## Description

`q = dcm2quat(n)` calculates the quaternion, `q`, for a given direction cosine matrix, `n`. Input `n` is a 3-by-3-by-`m` matrix of orthogonal direction cosine matrices. The direction cosine matrix performs the coordinate transformation of a vector in inertial axes to a vector in body axes. `q` returns an `m`-by-4 matrix containing `m` quaternions. `q` has its scalar number as the first column.

This function applies only to direction cosine matrices that are orthogonal with determinant +1.

`q = dcm2quat(n,action)` performs `action` if the direction cosine matrix is invalid (not orthogonal).

• Warning — Displays warning and indicates that the direction cosine matrix is invalid.

• Error — Displays error and indicates that the direction cosine matrix is invalid.

• None — Does not display warning or error (default).

`q = dcm2quat(n,action,tolerance)` uses a `tolerance` level to evaluate if the direction cosine matrix, `n`, is valid (orthogonal). `tolerance` is a scalar whose default is `eps(2)` (`4.4409e-16`). The function considers the direction cosine matrix valid if these conditions are true:

• The transpose of the direction cosine matrix times itself equals `1` within the specified tolerance (```transpose(n)*n == 1±tolerance```)

• The determinant of the direction cosine matrix equals `1` within the specified tolerance (`det(n) == 1±tolerance`).

## Examples

Determine the quaternion from direction cosine matrix:

```dcm = [0 1 0; 1 0 0; 0 0 -1]; q = dcm2quat(dcm) q = 0 0.7071 0.7071 0```

Determine the quaternions from multiple direction cosine matrices:

```dcm = [ 0 1 0; 1 0 0; 0 0 -1]; dcm(:,:,2) = [ 0.4330 0.2500 -0.8660; ... 0.1768 0.9186 0.3536; ... 0.8839 -0.3062 0.3536]; q = dcm2quat(dcm) q = 0 0.7071 0.7071 0 0.8224 0.2006 0.5320 0.0223```

Determine the quaternion from a direction cosine matrix validated within tolerance:

```dcm = [0 1 0; 1 0 0; 0 0 -1]; q = dcm2quat(dcm,'Warning',0.1) q = 0 0.7071 0.7071 0``` 