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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With