I am trying to understand the derivation process of Madgwicks fillter which can be found in this paper: https://www.samba.org/tridge/UAV/madgwick_internal_report.pdf
Lets name the the above paper as paper $[1]$ in the future context of my question.
I have a normalised quaternion $^S_E\hat{q}$ defined as:
$$^S_E\hat{q}=q_1+q_2i+q_3j+q_4k$$
where $q_1$, $q_2$, $q_3$ and $q_4$ are real numbers and $i$, $j$ and $k$ are imaginary numbers. The conjugat of $^S_E\hat{q}$ is $^S_E\hat{q}^*$ and it is defined as:
$$^S_E\hat{q}^*=q_1-q_2i-q_3j-q_4k$$
I also have the quaternion $^E\hat{g}$ defined as:
$$^E\hat{g}=0+0i+0j+1k$$
as well as a quaternion $^S\hat{a}$ defined as:
$$^S\hat{a}=0+a_xi+a_yj+a_zk$$
where $a_x$, $a_y$, $a_z$ are real numbers. In paper $[1]$ function $f_g$ is defined as a multiplication of $^S_E\hat{q}^*$, $^E\hat{g}$ and $^S_E\hat{q}$ minus $^S\hat{a}$:
$$f_g=^S_E\hat{q}^*\cdot^E\hat{g}\cdot^S_E\hat{q}-^S\hat{a}$$
When I try to obtain the expresin for $f_g$ using the above equation I get:
$$f_g=0+(2(q_2q_4-q_1q_3)-a_x)i+(2(q_1q_2+q_3q_4)-a_y)j+(q_1^2-q_2^2-q_3^2+q_4^2-a_z)k$$
The problem is, in paper $[1]$ the expression for $f_g$ (which is shown in equation number $(25)$ of the paper) is:
$$f_g=0+(2(q_2q_4-q_1q_3)-a_x)i+(2(q_1q_2+q_3q_4)-a_y)j+(2(0.5-q_2^2-q_3^2)-a_z)k$$
As you can see, there is a difference between mine and the papers function $f_g$. Namely, the real part that is multiplied by the imaginary number $k$ is different. This is the problem I am facing. I tried doing the math three times and I always get the same result. Please help me find the mistake I am making. For starters, I need to know is paper $[1]$ wrong or am I wrong. Thank you for your time.
Quaternion multiplication is known as Hamilton product. Let $\mathbf{q} = q_1 + q_2 \mathbf{i} + q_3 \mathbf{j} + q_4 \mathbf{k}$ and $\mathbf{s} = s_1 + s_2 \mathbf{i} + s_3 \mathbf{j} + s_4 \mathbf{k}$. The product $\mathbf{q} \mathbf{s}$ is $$\begin{aligned} \mathbf{q} \mathbf{s} & = ( q_1 s_1 - q_2 s_2 - q_3 s_3 - q_4 s_4 ) \\ ~ & + ( q_1 s_2 + q_2 s_1 + q_3 s_4 - q_4 s_3 ) \mathbf{i} \\ ~ & + ( q_1 s_3 - q_2 s_4 + q_3 s_1 + q_4 s_2 ) \mathbf{j} \\ ~ & + ( q_1 s_4 + q_2 s_3 - q_3 s_2 + q_4 s_1 ) \mathbf{k} \\ \end{aligned}$$ as described in equation (4) in the linked PDF.
Given the above $\mathbf{q}$, $\mathbf{q}^* = q_1 - q_2 \mathbf{i} - q_3 \mathbf{j} - q_4 \mathbf{k}$, $\mathbf{b} = 1 \mathbf{k}$, and $\mathbf{a} = a_x \mathbf{i} + a_y \mathbf{j} + a_z \mathbf{k}$, then $$\begin{aligned} \mathbf{q}^* \mathbf{b} \mathbf{q} - \mathbf{a} & = 0 \\ ~ & + \bigl( 2 ( q_2 q_4 - q_1 q_3 ) - a_x \bigr) \mathbf{i} \\ ~ & + \bigl( 2 ( q_1 q_2 + q_3 q_4 ) - a_y \bigr) \mathbf{j} \\ ~ & + \bigl( q_1^2 - q_2^2 - q_3^2 + q_4^2 - a_z \bigr) \mathbf{k} \\ \end{aligned}$$ This matches your result, and is correct (verified with wxMaxima).
Because $\mathbf{q}$ describes an orientation (or rotation), it is an unit quaternion, for which $q_1^2 + q_2^2 + q_3^2 + q_4^2 = 1$. This is mentioned in the PDF just before equation (1).
Thus, $$\begin{aligned} q_1^2 - q_2^2 - q_3^2 + q_4^2 - a_z & = q_1^2 + q_2^2 + q_3^2 + q_4^2 - 2 q_2^2 - 2 q_3^2 - a_z \\ ~ & = 1 - 2 q_2^2 - 2 q_3^2 - a_z \\ ~ & = 2 \left( \frac{1}{2} - q_2^2 - q_3^2 \right) - a_z \\ \end{aligned}$$ which means your solution and the solution shown in equation (25) in the PDF are equivalent, because $\mathbf{q}$ is an unit quaternion.