Compute unknown matrices that minimize a sum This problem is about working with smart-phone accelerometers.

To calibrate accelerometer, I need to find three unknown matrices T, K and B that minimize this sum:
$$\sum_{i=0}^N(|g|^2 - |TK(a_i + B)|^2)^2,$$
where $a_i$ is a known 3x1 vector and |$g$| is a known constant and unknown matrices are:
$$T = \begin{bmatrix} 1 &amp; t_1 &amp; t_2 \\ 0 &amp; 1 &amp; t_3 \\ 0 &amp; 0 &amp; 1\end{bmatrix},\ K=\begin{bmatrix} k_1 &amp; 0 &amp; 0 \\ 0 &amp; k_2 &amp; 0 \\ 0 &amp; 0 &amp; k_3\end{bmatrix},\ B=\begin{bmatrix} b_1 \\ b_2 \\ b_3\end{bmatrix}.$$

I have read in an article one can solve such problems by Levenberg-Marquardt
(LM) algorithm. Unfortunately I cannot figure out how. Can anyone suggest a solution to this minimization problem? And I need to implement the algorithm in python so if you know any good python library please  name it.
I've found the answer. You can solve such problems using scipy.optimize.root which includes  Levenberg-Marquardt (LM) algorithm.