I found a way to rotate a 3D vector using a given unit quaternion. Thanks to this answer.
Now, let's say I want to rotate a gravity vector: $\overrightarrow{g} = \begin{bmatrix} g_x\\ g_y\\ g_z\\ \end{bmatrix} = \begin{bmatrix} 0\\ 0\\ -9.81\\ \end{bmatrix}$, using a unit quaternion $ \mathbf q = [q_w, q_x, q_y, q_z] $ i.e. $ \lvert\lvert q\lvert\lvert^2 = q_w^2 + q_x^2 + q_y^2 + q_z^2 = 1 $
So, if I represent the rotated vector as $\overrightarrow{g'} = \begin{bmatrix} g'_x\\ g'_y\\ g'_z\\ \end{bmatrix}$
As $ g_x = 0 $ and $ g_y = 0 $, the simplified equations for the rotated vector looks like:
$$ g'_x = 2 g_z (q_x q_z + q_w q_y) $$
$$ g'_y = 2 g_z (q_y q_z - q_w q_x) $$
$$ g'_z = g_z (1 - 2 q_x^2 - 2 q_y^2) $$
where $ g_z = -9.81 $
Now, I need to calculate the Jacobian (as a part of my Extended Kalman Filter) of these equations w.r.t. $ q_w, q_x, q_y $ and $ q_z $:
$$ \frac{\partial g'_x}{\partial q_w} = 2 g_z q_y $$
$$ \frac{\partial g'_x}{\partial q_x} = 2 g_z q_z $$
$$ \frac{\partial g'_x}{\partial q_y} = 2 g_z q_w $$
$$ \frac{\partial g'_x}{\partial q_z} = 2 g_z q_x $$
$$ \frac{\partial g'_y}{\partial q_w} = - 2 g_z q_x $$
$$ \frac{\partial g'_y}{\partial q_x} = - 2 g_z q_w $$
$$ \frac{\partial g'_y}{\partial q_y} = 2 g_z q_z $$
$$ \frac{\partial g'_y}{\partial q_z} = 2 g_z q_y $$
But I'm confused about partial derivatis for $ g'_z $. Because in the equation of $ g'_z $,
- if I keep the $ 1 $ in $ g'_z $ as it is then
$$ \frac{\partial g'_z}{\partial q_w} = \frac{\partial (g_z(1 - 2 q_x^2 - 2 q_y^2))}{\partial q_w} = 0 $$
- if I replace the $ 1 $ in $ g'_z $ with $ q_w^2 + q_x^2 + q_y^2 + q_z^2 $ then
$$ \frac{\partial g'_z}{\partial q_w} = \frac{\partial (g_z(1 - 2 q_x^2 - 2 q_y^2))}{\partial q_w} = \frac{\partial (g_z(q_w^2 - q_x^2 - q_y^2 + q_z^2))}{\partial q_w} = 2 g_z q_w $$
- if I keep the $ 1 $ in $ g'_z $ as it is but do something like
$$ \begin{align} \frac{\partial g'_z}{\partial q_w} &= \frac{\partial (g_z(1 - 2 q_x^2 - 2 q_y^2))}{\partial q_w} \\ &= \frac{\partial g_z}{\partial q_w} - 2 g_z \frac{\partial q_x^2}{\partial q_w} - 2 g_z \frac{\partial q_y^2}{\partial q_w}\\ &= \frac{\partial g_z}{\partial q_w} - 2 g_z \frac{\partial (1 - q_w^2 - q_y^2 - q_z^2)}{\partial q_w} - 2 g_z \frac{\partial (1 - q_w^2 - q_x^2 - q_z^2)}{\partial q_w} \\ &= 0 - 2 g_z (-2 q_w) - 2 g_z (-2 q_w) \\ &= 8 g_z q_w \\ \end{align} $$
So, which one is the correct way to take the partial derivative here?
I have the same confusion for $ \frac{\partial g'_z}{\partial q_x} $, $ \frac{\partial g'_z}{\partial q_y} $, and $ \frac{\partial g'_z}{\partial q_z} $.
If I keep the $ 1 $ in $ g'_z $ as it is then
$$ \frac{\partial g'_z}{\partial q_x} = -4 g_z q_x $$
$$ \frac{\partial g'_z}{\partial q_y} = -4 g_z q_y $$
$$ \frac{\partial g'_z}{\partial q_z} = 0 $$
But if I replace the $ 1 $ in $ g'_z $ then it kind of feels like a rabbit hole!
Do you have any recommendations for additional reading? Because I'm still confused!
– Milan Jul 19 '24 at 20:27