# Trouble Estimating Motor Parameters with Least Squares in MATLAB

Basically, I'm trying to use Least-Squares to estimate the parameters of a DC motor.

My system can be modeled by the following matrix equation:

$$\begin{bmatrix}V_{input}(t)\\0\end{bmatrix}=\begin{bmatrix}\frac{\text{d}i}{\text{d}t}(t) && I(t) && \omega(t) && 0 && 0 \\ 0 && 0 && -I(t) && \frac{\text{d}\omega}{\text{d}t}(t) && \omega(t) \end{bmatrix} \times \begin{bmatrix}L\\R\\k_t\\J\\B\end{bmatrix}$$

From what I understand (admittedly probably not enough), I can estimate what the constants are for the vector $$\begin{bmatrix}L,R,k_t,J,B\end{bmatrix}^T$$ if I happen to know the values of the time-dependant variables for a handful of data points. If I take the equation to be of the form $$Y=M\times K$$, then the least-squares estimate at a particular discrete moment should be given by $$\hat K=(M^TM)^{-1}M^TY$$.

I have a motor set up to be driven by a sinusoidal voltage signal, offset so that the current doesn't reverse (for simplicity). I've measured the angular rate, the applied voltage, and the applied current. To get the necessary derivatives from the noisy signals, I built a sinusoid-fitting script in MATLAB which minimizes the average absolute difference between the fit signal and the noisy signal across all the measured data points. I'm no statistician, but the resulting fit eyeballed as "okay" so I moved on.

Larger version

Using a smooth fit curve for each of my measured parameters, I calculated the necessary derivatives and plugged everything into MATLAB. (I'm aware that K = inv(M.'*M)*(M.'*Y) isn't the most computationally-friendly, but I need to stick to the experimental procedure I listed in my write-up.)

Larger version

My output for $$\begin{bmatrix}L,R,k_t,J,B\end{bmatrix}^T$$ is absolute garbage though, and none of the values MATLAB spits out seem plausible. There are absurdly large values, very small values, and lots of negative values. (Physical properties shouldn't be negative.)

Larger version

I had planned to estimate $$\begin{bmatrix}L,R,k_t,J,B\end{bmatrix}^T$$ for each of my data vectors and then average those results, but I clearly won't get anything meaningful when my results are already useless.

Would anyone here happen to have an idea where I might have gone wrong?

I've stared at my code until I went cross-eyed without making too much progress, so I suspect that it might be a case of "garbage-in leads to garbage-out." My slightly-educated-guesses for what's happening are:

1. I've overlooked something fundamental in the math.
2. There's a data mismatch somewhere.
3. I'm going to be lost within the floating-point errors. (Symbolic MATLAB says the determinant for $$M^TM \equiv 0$$, but I know people use this kind of math fairly frequently. Therefore, I'm left to conclude that the calculation works but is ridiculously sensitive to changes in the measured values or something.)

Does anyone have any thoughts?

(If you're looking for a specific question, how about "under what experimental conditions would the math I've described work?")

• Would Computational Science be a better home for this question? – Qmechanic Dec 4 '20 at 7:42
• Matlab parameter estimation researchgate.net/profile/Dick_Ridder/publication/… – Eli Dec 4 '20 at 8:12
• Without going into details - are you trying to solve a system of two equations for 5 unknowns? – Vadim Dec 4 '20 at 8:12
• @Vadim I am, which I wouldn't usually think to be a good idea but I'm taking my lead from this paper which claims have done the same thing (if I interpreted correctly). – CoilKid Dec 4 '20 at 15:24
• @Qmechanic I posted here because I thought there might be some experimental physicists around who had done this type of least-squares estimation before. If you think this is more MATLAB than it is a math/physics thing, then I'll go ask it on SciComp.SE – CoilKid Dec 4 '20 at 17:46

I think I understand what you did, even without further explanations. At each time step $$t_i$$ you are solving in the least-squares sense a system of equations of the form $$Y(t_i) = M(t_i) K$$, with $$M(t_i) \in \mathbb{R}^{2\times 5}$$, separately, to get an estimate of a time-independent vector $$K \in \mathbb{R}^{5}$$, and then you are hoping to somehow combine the estimates.
$$\mathcal{Y} = \mathcal{M}K, \quad \mathcal{Y} = \begin{bmatrix}Y(t_1)\\ Y(t_2)\\ \vdots \\ Y(t_n)\end{bmatrix}, \quad \mathcal{M} = \begin{bmatrix}M(t_1)\\ M(t_2)\\ \vdots \\ M(t_n)\end{bmatrix}.$$
This will get you a single estimate of $$K$$ that minimizes the total error under a suitable model. The matrix $$\mathcal{M}$$ has full column rank generically, so this problem should be well-posed, unlike the previous ones. (But this is not an valid excuse to use bad numerics like inv(M'*M).)