I have a 6x3 Matrix M_start which provides coordinates in xyz. I also have an end Matrix 6x3 M_end, which provides the coordinates of the desired matrix. I now need to know which rotations around x y and z have to be applied to get from M_start to M_end. The Equation is [RzRyRx][M_start]=[M_end]
For this, I tried to use the 3x3 RotationMatrix RzRyRx, which Wikipedia provides and used it on M_start to get a set of 18 Equations. Since i have only 3 variables only 3 equations are needed. These Equations i tried to solve with SymPy's solve() and nonlinsolve(). Sadly, the solution takes forever in the case of solve, and nonlinsolve provides an output which i dont really understand.
"ConditionSet((a, b, c), Eq((2.02015sin(c) - 0.708642cos(c))cos(b) + 0.6677sin(b) - 1.0154, 0) & Eq(-0.1967*((sin(c) - 1.122cos(c))sin(b) - 0.6853cos(b))sin(a) - 0.1967(1.122sin(c) + cos(c))cos(a) - 0.0285, 0) & Eq(0.2201sin(a)sin(c) + 0.1967sin(a)cos(c) - 0.1967sin(b)*sin(c)cos(a) + 0.2207sin(b)*cos(a)cos(c) + 0.1348cos(a)*cos(b) - 0.323, 0), ProductSet(Complexes, Complexes, Complexes"
Here is my code:
from sympy import symbols, cos, sin, Eq, solve, nonlinsolve
a, b, c = symbols('a b c')
# Gleichungen für die Werte
eq1 = Eq(0.2201*sin(a)*sin(c) + 0.1967*sin(a)*cos(c) - 0.1967*sin(b)*sin(c)*cos(a) + 0.2207*sin(b)*cos(a)*cos(c) + 0.1348*cos(a)*cos(b), 0.323)
eq2 = Eq(-0.1967*(cos(a)*(cos(c) + 1.122*sin(c)) + sin(a)*(-0.6853*cos(b) + sin(b)*(-1.122*cos(c) + sin(c)))), 0.0285)
eq3 = Eq(0.6677*sin(b) + cos(b)*(-0.708642*cos(c) + 2.02015*sin(c)), 1.0154)
solution = nonlinsolve((eq1,eq2,eq3), (a, b, c))
print(solution)
Do you have any other ideas how to solve for the angles a b c or what I am making wrong with this method?
Here is a more generic solution to your problem.
First, let's rename your matrices
m1andm2. You can see each of these matrices as a could of 3D points. You want to find a rotationRwhich maximises correlation between these two sets (in your specific case, you assume this correlation can be perfect because you knowm1is a rotation ofm2). This problem is called the Procustes problem, and can be solved using the Kabsch algorithm.You can simply compute the desired matrix using a solver for this problem in
scipy:For more details on how this works, you can read this subsection of a blog post on the Procrustes problem. It explains that you can solve the Procrustes problem by using the Singular Value Decomposition of the correlation matrix of
m1andm2and gives an analytical proof for that.