Why does lsqcurvefit result in complex parameters?
이전 댓글 표시
I want to fit experimental impedance data (real(impedance),imag(impedance),frequency).
The written function for getting the parameters has complex formulas for description of the impedance, but real-valued parameters. Unfortunately after the lsqcurvefit I get complex-valued parameters.
(The resnorm2 has a value of apprax 1e-6, which is not so bad.)
I want only real-valued parameters. What can I do to solve this problem?
My question is related to this question: https://ch.mathworks.com/matlabcentral/answers/320390-how-to-avoid-complex-eigenvalues-of-the-matrix-in-its-non-linear-regression-lsqcurvefit (but it was not helpful enough.) I've tried different things but without success till now and would be glad to get a solution from here.
---
In the code file "Fit3_exp.m" I wrote:
% Start Parameters (initial guess)
startPar = [0.45*1e-3, 1.4*1.0846e-7, 0.016*4.111, 0.7*1e-3, 11, 0.00020];
% Make the best fitting
fitPar = lsqcurvefit(@Fit3_f1, startPar, vertcat(f, f), vertcat(real(Z_exp), imag(Z_exp)),[0 0 0 0 0 0],[1e-3 1e6 1e6 1e6 1e6 1e6])
% Vector of fitted Zfit data
zFit3 = Fit3_f1(fitPar, vertcat(f, f));
% Plot Fitted Line
plot(zFit3(1:length(zFit3)/2),-zFit3(1+length(zFit3)/2:end), 'r','DisplayName','computed fitting data')
In the function file "Fit3_f1.m" I wrote:
function Z_fit3 = Fit3_f1(par,f)
file='G:\users\DA\Ueberblick EIS.xlsx';
exp_data_Freq = xlsread(file,'A4','A3:A73');
f = exp_data_Freq(:,1);
f = f(1:length(f)/1); %
w = 2*pi*f ;
R0_fit0 = par(1);
L_fit0 = par(2);
R_bat_fit0 = par(3);
R_ct_fit0 = par(4);
C_dl_fit0 = par(5);
s_w_fit0 = par(6);
L_bat_fit0 = 1i*w*L_fit0;
R_el_fit0 = (L_bat_fit0*R_bat_fit0)./(L_bat_fit0+R_bat_fit0);
Z_d_fit0 = (s_w_fit0 ./ sqrt(w)) * (1-1i);
Z_c_fit0 = -1i ./ (C_dl_fit0*w);
R_dl_fit0 = ((Z_c_fit0 .* (R_ct_fit0 + Z_d_fit0)))./(R_ct_fit0 + Z_d_fit0 + Z_c_fit0);
Z_fit0 = R0_fit0 + R_el_fit0 + R_dl_fit0;
Z_fit3 = vertcat(real(Z_fit0), imag(Z_fit0));
I get now only real-valued Parameters, however the fitting is really poor (resnorm2 = 3.5017e-04).
Are there mistakes in my formulas (e.g. with refering to the correct fit function (with indices) or is something else wrong)?
댓글 수: 5
Torsten
2018년 9월 26일
Fit real and imaginary part of your data to real and imaginary part of your fit function. This way, you only work with real numbers, and the estimated parameters will remain real-valued.
Torsten
2018년 9월 27일
I don't see an obvious programming error, but you could test the fitting procedure by producing artificial data from your model, perturb these data slightly and try to reproduce the parameters you have used to generate these data.
If this works, you can be sure, that technically, your code is correct.
Then you can try "multistart" or modify your model.
Best wishes
Torsten.
hamp
2018년 10월 3일
Torsten
2018년 10월 4일
If you supply real-valued experimental data (real(Z_exp), imag(Z_exp)), return real-valued model data (real(Z_fit0), imag(Z_fit0)) and start with real-valued parameters, the parameters will remain real-valued throughout the fitting process.
답변 (2개)
Walter Roberson
2018년 9월 27일
0 개 추천
It is because you are ignoring the second input parameter.
Note: for efficiency, do not read files in the objective function. Read the files beforehand and pass them into the objective function by creating an anonymous function that refers to them.
댓글 수: 2
hamp
2018년 9월 27일
Walter Roberson
2018년 10월 3일
I do not have your data to test with so I created some random data and changed to code to work with the complex values directly. The results I got back were real-valued for fitPar.
fit4_driver.m:
file='G:\users\DA\Ueberblick EIS.xlsx';
%f = xlsread(file,'A4','A3:A73');
f = rand(71,1);
%realpart = xlsread(file,'TS40','B3:B73');
%imagpart = xlsread(file,'TS40','C3:C73');
realpart = rand(71,1);
imagpart = randn(71,1);
Z_exp = complex(realpart, imagpart);
% Start Parameters (initial guess)
startPar = [0.45*1e-3, 1.4*1.0846e-7, 0.016*4.111, 0.7*1e-3, 11, 0.00020];
% Make the best fitting
fitPar = lsqcurvefit(@Fit4_f1, startPar, f, Z_exp, [0 0 0 0 0 0], [1e-3 1e6 1e6 1e6 1e6 1e6]);
% Vector of fitted Zfit data
zFit4 = Fit4_f1(fitPar, f);
% Plot Fitted Line
plot(real(zFit4),-imag(zFit4), 'r','DisplayName','computed fitting data')
Fit4_f1.m:
function Z_fit0 = Fit4_f1(par,f)
f = f(1:length(f)/1); %
w = 2*pi*f ;
R0_fit0 = par(1);
L_fit0 = par(2);
R_bat_fit0 = par(3);
R_ct_fit0 = par(4);
C_dl_fit0 = par(5);
s_w_fit0 = par(6);
L_bat_fit0 = 1i*w*L_fit0;
R_el_fit0 = (L_bat_fit0*R_bat_fit0)./(L_bat_fit0+R_bat_fit0);
Z_d_fit0 = (s_w_fit0 ./ sqrt(w)) * (1-1i);
Z_c_fit0 = -1i ./ (C_dl_fit0*w);
R_dl_fit0 = ((Z_c_fit0 .* (R_ct_fit0 + Z_d_fit0)))./(R_ct_fit0 + Z_d_fit0 + Z_c_fit0);
Z_fit0 = R0_fit0 + R_el_fit0 + R_dl_fit0;
%Z_fit4 = vertcat(real(Z_fit0), imag(Z_fit0));
end
hamp
2018년 10월 3일
댓글 수: 4
Walter Roberson
2018년 10월 3일
The message is correct. You have
Z_fit4 = vertcat(real(Z_fit0), imag(Z_fit0));
so you are producing a 284 x 1 output in response to a 142 x 1 input. Your fitting function must produce exactly one output for every input.
hamp
2018년 10월 4일
Walter Roberson
2018년 10월 4일
function Z_fit4 = Fit4_f1(something)
means that the function name is Fit4_f1 and that whenever it is invoked, the value to be returned is whatever has been assigned to the variable Z_fit4. lsqcurvefit requires that the output be the same size as the input. Your current code is calculating an temporary variable Z_fit0 that is the right size, but splits it into real and imaginary parts and so is returning twice as much data as expected.
The current code is also not taking into account that half of the input corresponds to imaginary parts.
카테고리
도움말 센터 및 File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!