# Local, symbolic sensitivity analysis of Euler Angles

**URL:** <https://discourse.julialang.org/t/local-symbolic-sensitivity-analysis-of-euler-angles/68984>\
**Category:** Specific Domains\
**Tags:** symbolic\
**Created:** [September 30, 2021, 9:52am UTC](https://discourse.julialang.org/t/local-symbolic-sensitivity-analysis-of-euler-angles/68984 "2021-09-30T09:52:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JanKap](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jankap/32/22763_2.png) [@JanKap](https://discourse.julialang.org/u/JanKap)\
**Post date:** [September 30, 2021, 9:52am UTC](https://discourse.julialang.org/t/local-symbolic-sensitivity-analysis-of-euler-angles/68984/1 "2021-09-30T09:52:50Z")

</div>

Hey all 🙂

I’m trying to wrap my head around a supposedly simple problem, but it’s not working yet.

It is known (see [https://www.nxp.com/files-static/sensors/doc/app\_note/AN3461.pdf](https://www.nxp.com/files-static/sensors/doc/app_note/AN3461.pdf)) that the relationship between the measured (e.g. by an accelerometer) acceleration of earth’s gravity a=\left[a\_x,a\_y,a\_z\right]^T relates to Euler ZYX angles ([Euler angles - Wikipedia](https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix)) via

\begin{bmatrix} a\_x\\a\_y\\a\_z \end{bmatrix} = \begin{bmatrix} \sin\theta\\-\cos\theta \sin\phi\\-\cos\theta \cos\phi\end{bmatrix} (1).

\theta is the so called Pitch angle and \phi the Roll angle. They can be calculated from (1) via

\begin{eqnarray} \tan\phi&=\frac{-a\_y}{-a\_z}\quad (2) \\\tan\theta&=\frac{a\_x\cos\phi}{-a\_z} \quad (3) \\ &=\frac{a\_x}{\sqrt{a\_y^2+a\_z^2}} \quad (4) \end{eqnarray}

What I’d like to calculate is a local sensitivity function for J\_\phi(\phi,\theta), i.e. I’d like to plot a surface plot with ||{J}|| on the z-axis and \phi and \theta on the x and y axis respectively.  
To my understanding, I’d need to calculate the partial derivatives of (2). The problem here is, that (2) is no function of \phi or \theta but of a\_y and a\_z. So there are more calculation steps required. However, (hopefully) all information are shown in (1).

Can Julia do these term conversion to eliminate all a\_{x,y,z} terms? I do not know if that’s mathematically possible. But then I would not know how to calculate the sensitivity function.

I tried to use (3) as a starting point and calculated the partial derivatives, but there are still dependencies of a\_z and a\_x.

Can Julia do (in some magic way) a sensitivity analysis of (1) directly?

From a Monte Carlo analysis (sampling a randomly across the unit sphere’s surface and applying (2) and (3) to calculate the angles) and practical experiments, some shape like here (with different scaling on z) would be expected:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/6/b65049043a12b006c7e180321819342600f86e7e.png)

Disclaimer: I don’t know if that task is solvable analytically, but some paper are doing a sensitivity analysis without showing the steps (and then applying simplification like setting a\_x=0 to plot it), e.g. [Autocalibration of MEMS Accelerometers | IEEE Journals & Magazine | IEEE Xplore](https://doi.org/10.1109/TIM.2008.2006137)

Thank you very much!  
Jan

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 30, 2021, 1:45pm UTC](https://discourse.julialang.org/t/local-symbolic-sensitivity-analysis-of-euler-angles/68984/2 "2021-09-30T13:45:51Z")

</div>

> [@JanKap](#):
>
> What I’d like to calculate is a local sensitivity function for J\_\phi(\phi,\theta) Jϕ(ϕ,θ)J\_\phi(\phi,\theta) , i.e. I’d like to plot a surface plot with ||{J}|| ||J||||{J}|| on the z-axis and \phi ϕ\phi and \theta θ\theta on the x and y axis respectively.

Sure, you just make a grid of theta and phi values and then calculate the norm of J for each grid point and plot it

---

<div class="post-metadata">

**Author:** ![JanKap](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jankap/32/22763_2.png) [@JanKap](https://discourse.julialang.org/u/JanKap)\
**Post date:** [September 30, 2021, 1:57pm UTC](https://discourse.julialang.org/t/local-symbolic-sensitivity-analysis-of-euler-angles/68984/3 "2021-09-30T13:57:28Z")

</div>

Thanks 🙂 The problem: I don’t have J(\cdot) yet and I’m wondering if Julia can calculate that via Symbolic.jl or similar tools.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [September 30, 2021, 4:04pm UTC](https://discourse.julialang.org/t/local-symbolic-sensitivity-analysis-of-euler-angles/68984/4 "2021-09-30T16:04:19Z")

</div>

What exactly is J? You have not defined it yet.

---

<div class="post-metadata">

**Author:** ![Arrigo\_Benedetti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arrigo_benedetti/32/25545_2.png) [@Arrigo\_Benedetti](https://discourse.julialang.org/u/Arrigo_Benedetti)\
**Post date:** [September 30, 2021, 5:33pm UTC](https://discourse.julialang.org/t/local-symbolic-sensitivity-analysis-of-euler-angles/68984/5 "2021-09-30T17:33:44Z")

</div>

If J is the Jacobian and you don’t really need its symbolic expression, why don’t you just use `ForwardDiff.jl`?

---

<div class="post-metadata">

**Author:** ![JanKap](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jankap/32/22763_2.png) [@JanKap](https://discourse.julialang.org/u/JanKap)\
**Post date:** [October 1, 2021, 10:09am UTC](https://discourse.julialang.org/t/local-symbolic-sensitivity-analysis-of-euler-angles/68984/6 "2021-10-01T10:09:23Z")

</div>

Sorry, it is indeed misleading in my original post. Can’t edit anymore unfortunately.

J shall be the sensitivity function for my angle \phi. Something like J\_\phi(\phi,\theta)=\frac{\partial f(\phi,\theta)}{\partial\phi}. The issue is that I don’t know a formulation for f(\phi,\theta), i.e. f being a function of the angles only, and not of a\_{x,y,z}. f is currently unknown to me. It should follow from (1) or (2) and (3).

My first question is if Julia can use (1) to calculate that missing f(\phi,\theta) via Symbolics.jl. I can’t do it manually, maybe it is not possible.

Second question would then be to how to create the sensitivity function J. Either from f(\phi,\theta) (if that expression can be found, then sensitivity is a straight forward partial derivative) via Symbolics.jl or ForwardDiff.jl or directly from (1) but I don’t know how to formulate that problem in Symbolics.jl or ForwardDiff.jl.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [October 1, 2021, 11:24am UTC](https://discourse.julialang.org/t/local-symbolic-sensitivity-analysis-of-euler-angles/68984/7 "2021-10-01T11:24:15Z")

</div>

I think you want the magnitude of the projection of the gravitational acceleration onto an orientation vector for the accelerometer.

Also your equation (1) is missing a values on the right hand side.

Let a coordinate system be oriented with z along the gravitational direction. Then [0,0,g] is the accel vector. Let the accelerometer measure acceleration along a unit length vector [a,b,c]. Then the measured acceleration is the dot product of the two. [0,0,g] \cdot [a,b,c]

You can express [a,b,c] in terms of Euler angles, and then you still take the dot product… The sensitivity function you seek is the partial of the dot product wrt the angle of interest.

The dot product is just g\*c using this coordinate system.
