# How to maximize a function over a rotation matrix?

I have list of pairs of points: $\{(x_i,y_i)\}_{1\le i\le n}$, $x_i \in \mathbb{R}^3$, $y_i \in\mathbb{R}^2$ and would like to optimize over a rotation $R \in SO(3)$, a translation $t \in \mathbb{R}^3$ and scalars $a,b \in \mathbb{R}$ such that $\sum_{i=1}^n{\|f(x_i|R,t,a,b) - y_i\|^2}$ is minimal. The model function $f$ is non-linear and varies in my applications. How can I find optimal parameter values $R,t,a,b$ for a given dataset with Mathematica (v9)?

Edit: An example for $f$ would be:

f[{x_, y_, z_}, R_, t_, a_, b_] := Module[{qx, qy, qz},
{qx, qy, qz} = R.(z/a {x, y, a}) + t;
b {qx/qz, qy/qz}
]


Here $a,b>0$ would be an additional constraint.

A few data points as an example. A longer list can be found here.

{
{21.389, -24.551, 2.226, -5.25873, 3.72399},
{32.204, -7.055, 0.727, -28.5502, 20.8323},
{11.524, -12.171, 2.116, -9.31497, 13.5392},
{251.154, -8.349, 2.333, 21.8644, -62.8753},
{-12.07, -10.771, 2.09, -1.56909, 11.4189},
{-95.896, 7.367, 1.974, -56.4495, -28.5834},
{87.725, -2.845, 1.108, 4.35673, -6.32492},
{188.811, 99.661, 2.052, -35.3311, -40.9137}
}

-
You will want to look up FindGeometricTransform[]; it will be able to find the best rigid transformation between your two sets of points. – J. M. Jun 22 '13 at 11:17
Thank you for the hint. The qestion was probably to easy. My model is a bit more complex than just Rx+t. It is more f(x|R,t,...). – Danvil Jun 22 '13 at 11:21
In that case, you really should edit your question to talk about your actual problem, and maybe include sample data and expected results... – J. M. Jun 22 '13 at 11:23
Some more definiteness would be appreciated; in particular, what would your $f$ typically look like? – J. M. Jun 22 '13 at 11:54
...and some example points would be, y'know, cool too... – J. M. Jun 22 '13 at 12:25

I started with a parameterization of the rotation matrix in terms of $t=Tan[\theta]$ and $n =\{n1,n2,n3\}$, where $\theta$ is the angle of rotation and $n$ is the axis of rotation.

R[t_, {n1_, n2_, n3_}] := With[{n = {{0, -n3, n2}, {n3, 0, -n1}, {-n2, n1, 0}},
id = IdentityMatrix[3]}, Inverse[id - t n].(id + t n)]


Then I use the example model function $f$ to determine a suitable form to pass to NonlinearModelFit.

f[{x_, y_, z_}, R_, t_, a_, b_] := Module[{qx, qy, qz},
{qx, qy, qz} = R.(z/a {x, y, a}) + t;
b {qx/qz, qy/qz}]

form = Total[f[{x, y, z}, R[t, {n1, n2, n3}], {t1, t2, t3}, a, b]^2];


Then import the data. (There appears to be ~ 36000 entires and the computations appeared to be taking forever, so I used only the first 500.)

data = Join[#[[1 ;; 3]], {#[[4]]^2 + #[[5]]^2}] & /@ Import[...][[1 ;; 500]];


Finally solve it using NonlinearModelFit.

nlm = NonlinearModelFit[data, {form, a > 0, b > 0},
DeleteCases[Variables[form], x | y | z], {x, y, z}, Method -> "NMinimize"];


This is the optimal value for $R$ that was obtained with the 500 points.

R[t, {n1, n2, n3}] /. nlm["BestFitParameters"]
(* {{-0.164503, -0.185123, -0.968849}, {0.00374213,
0.982106, -0.188291}, {0.986369, -0.0346, -0.160867}} *)


The other parameters.

{a, b, {t1, t2, t3}} /. nlm["BestFitParameters"]
(* {0.598143, 703.907, {1.72821, 115.142, 280.814}} *)

-
Thank you for the answer! The parameters do not make sense, but let's see. – Danvil Jun 30 '13 at 9:15