# What's the best way to implement a least-squares estimation of a motor system 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? I'm honestly not sure what I'm missing. (This is based upon a modification from this paper.)

Note: this question was reposted to SciComp.SE upon the suggestion that Physic.SE might not be as well-suited for it.

Edit: Using the pseudo-inverse and letting MATLAB do the $$(M^TM)^{-1)M^T$$ part more efficiently has helped, but my output still looks like garbage.

Larger version

• With such methods it gets tricky when you try to fit or interpolate the data to make it smoother as in your case. The reason for that is that you inadvertedly alter the dynamics of your measured system. Some of this might be noise or other useless stuff that you have to throw away, so you can use a filter for that. Also, ensure you are choosing the right input to the motor such as matrix $M^TM$ is invertible. Any arbitrary input will not do apparently, as it says at the bottom of pg 982 on the paper. This might cause large errors as the matrix may be very close to singular. – kostas1335 Dec 12 '20 at 13:34