# RTK with the Assistance of an IMU-Based Pedestrian Navigation Algorithm for Smartphones
## 2 Materials and Methods
### Implementation of RTK
#### 2.2.2 Theory of the Madgwick Algorithm  
We can calculate the quaternion with the measurements of gyroscopes, as mentioned above. We can also calculate the quaternion with the measurements of the accelerometers and magnetometers. Suppose we had a predefined reference vector \(\prescript{E}{\mathbf{\hat{d}}}\) in the field of the Earth frame. We can compute the reflect of the vector in the sensor frame using the quaternion:  
\[\prescript{S}{\mathbf{\hat{d}}}=\prescript{S}{E}{\mathbf{\hat{q}}^{*}}\otimes\prescript{ E}{\mathbf{\hat{d}}}\otimes\prescript{S}{E}{\mathbf{\hat{q}}} \tag{26}\]  
We can also get the value of vector \(\prescript{S}{\mathbf{\hat{s}}}\) in the measured field of the sensor frame. We can define an objective function to represent the difference between \(\prescript{S}{\mathbf{\hat{d}}}\) and \(\prescript{S}{\mathbf{\hat{s}}}\) as follows:  
\[\mathbf{f}(\prescript{S}{E}{\mathbf{\hat{q}}^{E}},\prescript{S}{\mathbf{\hat{d}}^{S}})= \prescript{S}{\mathbf{\hat{d}}}-\prescript{S}{\mathbf{\hat{s}}}=\prescript{S}{E}{\mathbf{ \hat{q}}^{*}}\otimes\prescript{E}{\mathbf{\hat{d}}}\otimes\prescript{E}{\mathbf{\hat{s} }}\,{\mathbf{\hat{q}}^{-S}}\,{\mathbf{\hat{s}}} \tag{27}\]  
The most accurate quaternion should go with the minimum of the objective function. Therefore, this is an optimization problem that can be solved using gradient descent, as Equation (28) shows:  
\[\prescript{S}{E}{\mathbf{\hat{q}}_{k+1}}=\prescript{S}{E}{\mathbf{\hat{q}}_{k}}-\mu \frac{\
abla f(\prescript{S}{E}{\mathbf{\hat{q}}_{k,k}}^{E}\,\prescript{S}{\mathbf{ \hat{q}}_{s}})}{\big{|}\big{|}\
abla f(\prescript{S}{E}{\mathbf{\hat{q}}_{k,k}}^{E }\,\prescript{S}{\mathbf{\hat{q}}_{s}})\big{|}\big{|}} \tag{28}\]  
In Equation (28) \(\mu\), stands for the variable step-size. The objective function is formed with two sub-functions solving the measurements of accelerometers and magnetometers, respectively. When it comes to the measurements of accelerometers, we substitute \(\prescript{E}{\mathbf{\hat{g}}}\) and normalized accelerometers measurements \(\prescript{E}{\mathbf{\hat{a}}}\) for \(\prescript{S}{\mathbf{\hat{d}}}\) and \(\prescript{S}{\mathbf{\hat{s}}}\)respectively, as shown in Equations (29) and (30). Then, we can get the first sub-function, as shown in Equation (31). \[{}^{E}\hat{\mathbf{g}}= \begin{bmatrix}0\\ 0\\ 0\\ 1\end{bmatrix}  \tag{29}\]  
\[{}^{S}\hat{\mathbf{a}}= \begin{bmatrix}0\\ a_{x}\\ a_{y}\\ a_{z}\end{bmatrix}  \tag{30}\]  
\[{}^{E}_{g}(\hat{\mathbf{g}}\hat{\mathbf{u}},^{E}\hat{\mathbf{g}},^{S}\hat{\mathbf{a}})= \begin{bmatrix} 2(q_{1}q_{3}-q_{0}q_{2})-a_{x}\\ 2(q_{0}q_{1}+q_{2}q_{3})-a_{y}\\ 1-2(q_{1}^{2}+q_{2}^{2})-a_{z}\end{bmatrix}  \tag{31}\]  
When it comes to the measurements of magnetometers, we substitute the Earth's magnetic field \({}^{E}\hat{\mathbf{b}}\) and normalized magnetometer measurements \({}^{E}\hat{\mathbf{a}}\) for \({}^{S}\hat{\mathbf{d}}\) and \({}^{S}\hat{\mathbf{s}}\), respectively, as shown in Equations (32) and (33). Then, we can get the second sub-function, as shown in Equation (34). \[{}^{E}\hat{\mathbf{b}}= \begin{bmatrix}0\\ b_{x}\\ 0\\ b_{z}\end{bmatrix}  \tag{32}\]  
\[{}^{S}\hat{\mathbf{m}}= \begin{bmatrix}0\\ m_{x}\\ m_{y}\\ m_{z}\end{bmatrix}  \tag{33}\]  
\[{}^{S}\hat{\mathbf{m}}= \begin{bmatrix}0\\ m_{y}\\ m_{z}\end{bmatrix}  \tag{34}\]  
\[{}^{S}\hat{\mathbf{m}}= \begin{bmatrix}0\\ m_{x}\\ m_{y}\\ m_{z}\end{bmatrix}  \tag{35}\]  
Then, we can form the objective function with two sub-functions as follows:  
\[{}^{S}_{E}\hat{\mathbf{q}}_{\
abla,t}= \begin{bmatrix}\hat{\mathbf{s}}\\ \hat{\mathbf{u}},^{E}\hat{\mathbf{b}},^{S}\hat{\mathbf{m}}\\ \hat{\mathbf{r}}_{b}(\hat{\mathbf{s}}\hat{\mathbf{q}},^{E}\hat{\mathbf{b}},^{S}\hat{\mathbf{m}}) \end{bmatrix}