Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Least Squares Minimization Complex Numbers

I've been using my Matlab, but it's my vision to eventually switch over to doing all of my analysis in Python since it is an actual programming language and a few other reasons.

The recent problem I've been trying to tackle is to do least squares minimization of complex data. I'm an engineer and we deal with complex impedance pretty often, and I'm trying to use curve fitting to fit a simple circuit model to measured data.

The impedance equation is as follows:

Z(w) = 1/( 1/R + j*w*C ) + j*w*L

I'm then trying to find the values of R, C, and L such that the least squares curve is found.

I've tried using the optimization package such as optimize.curve_fit or optimize.leastsq, but they don't work with complex numbers.

I then tried making my residual function return the magnitude of the complex data, but this did not work either.

like image 566
Mark Avatar asked May 25 '13 01:05

Mark


1 Answers

Referring to unutbu answer's, there is no need to reduce the available information by taking the magnitude squared in function residuals because leastsq does not care whether the numbers are real or complex, but only that they are are expressed as a 1D array, preserving the integrity of the functional relationship.

Here is a replacement residuals function:

def residuals(params, w, Z):
    R, C, L = params
    diff = model(w, R, C, L) - Z
    z1d = np.zeros(Z.size*2, dtype = np.float64)
    z1d[0:z1d.size:2] = diff.real
    z1d[1:z1d.size:2] = diff.imag
    return z1d

This is the only change that need be made. The answers for the seed 2013 are: [2.96564781, 1.99929516, 4.00106534].

The errors relative to to unutbu answer's are reduced by significantly more than an order of magnitude.

like image 51
Mike Sulzer Avatar answered Sep 19 '22 23:09

Mike Sulzer