Complex values computed by fit.m function (Curve fitting Tool)

Hi,
I am trying to use the fit.m function, but matlab throws the "complexValueComputed" exception, even though my x variable could not created complex values.
My x variable is: x = 1 - T./Tcrit and T is an array with max(Tsat) < Tcrit.
My y variable is an irrational function: y = a1 + a2 .*x.^(1/3) + a3 .* x.^(2/3)+ a4 .* x.^(3/3)+...
So there is just a possibilty to create complex values for Tsat > Tcrit, but this is not possible.
I even try to solve this problem by using the abs() function for my x variable, but nothing changed.
The error message is: "Complex value computed by model function, fitting cannot continue.Try using or tightening upper and lower bounds on coefficients."
and appears in fit.m in line 116 or for the iFit.m function in line 348.
Any help for resolving this problem would be greatly appreciated!

6 个评论

Your y variable becomes complex-valued if any of the a_i becomes negative.
I am sorry, I missed the x.
y should be: y = a1 + a2 .* x.^(1/3) + a3 .* x.*^(2/3) + .... so it depends on x.
You must have xdata, ydata and a fitting function.
I guess that x = xdata and y = fitting function(x). What are your ydata ?
And it would be helful if you included the relevant part of your code.
Yes you are right, but there are many single file, but I try to write down the important information.
The saturation pressure line should be fitted with the help of the saturation temperature values. psat and Tsat are the original values from a data bank.
...
RegRobApproach = {'Bisquare'};
approach_p{1,1}.formula=16; %with this approach=16 later the described formula a1 + a2 .* x.^(1/3) + a3 .* x.*^(2/3) + ... will be constructed
approach_p{1,1}.Dcalc = 'D = D;'; %dependent variable = psat
approach_p{1,1}.IDcalc = 'ID = 1 - ID ./ Tcrit;'; %independet variabel = Tsat; it does not work, even with 'ID = abs(1-ID./Tcrit)';
.
.
.
%loading original data:
%psat is an 1368x1 array (double) with pressure values
%Tsat is an 1368x1 array (double) with associated temperature values
D = psat %dependent variable
ID = Tsat %independet variable
eval(approach{1,1}.Dcalc);
eval(approach{1,1}.IDcalc);
[FData{1,1}, FData{2,1}] = prepareCurveData(ID,D);
xData = FData{1,1};
yData = FData{2,1};
tf_err = 0; % Condition to terminate while loop
ct_err = 0; % Counter of attempts
t_err = 0; % Status indicating error
while tf_err==0
% Handle error that may occur during fitting procedure
try
% Write fitting formula and fitting approach with the approach number 16
[fFormula, ft, ftOpts] = writeFitFormula(tp,FFormula(1:2,k_mag),RegRobApproach);
%k_mag is just the number of a parfor loop;
%function writeFitFormula see below
% Fit data
[fitresult, gof] = fit(xData, yData, ft, ftOpts);
t_err = 0;
catch e
fprintf(1,'\n%s', e.identifier);
ct_err = ct_err + 1;
t_err = 1;
end
% Define end of while loop
if t_err == 0 || ct_err == rp_err, tf_err = 1; end
end
function [fFormula, ft, ftOpts] = writeFitFormula(tp,mag,RegRobApproach)
%tp and mag are just number for different cases
.
.
.
fFormula = 'c + a1 .* x.^(1/3) + a2 .* x.*^(2/3) + a2 .* x.*^(3/3) + a2 .* x.*^(4/3)'; %for example; the define the number of coefficients
% Create the fitting approach
ft = fittype(fFormula,'independent',{'x'},'dependent',{'y'});
% Define the fitting options
%
ftOpts = fitoptions( 'Method', 'NonlinearLeastSquares' );
ftOpts.Display = 'Off';
ftOpts.Normalize = 'On'; %eingefügt
switch RegRobApproach
case 'LAR'
ftOpts.Robust = 'LAR';
case 'Bisquare'
ftOpts.Robust = 'Bisquare';
otherwise
end
ftOpts.StartPoint = zeros(1, mag(1,1)+1)+((rand(1)-0.5)*1E4);
ftOpts.Lower = zeros(1,mag(1,1)+1) - 1E9;
ftOpts.Upper = zeros(1,mag(1,1)+1) + 1E9;
%ftOpts.Algorithm = 'Trust-Region';
%ftOpts.Algorithm = 'Levenberg-Marquardt';
%ftOpts.DiffMinChange = 1.0000e-8;
%ftOpts.DiffMaxChange = 1.0000e-1;
ftOpts.MaxFunEvals = 6000;
ftOpts.MaxIter = 4000;
ftOpts.TolFun = 1.0000e-20;
ftOpts.TolX = 1.0000e-20;
end
I know that the eval-function is not the best solution, but the preworker of the scripts used it everywhere and it is not so easy to replace them all.

请先登录,再进行评论。

回答(1 个)

Torsten
Torsten 2019-6-26
编辑:Torsten 2019-6-26
Since your fitting problem is linear in the coefficients a1,a2,a3,..., this is all you need to get the best fit parameters:
A = [ones(1368,1), Tsat.^(1/3), Tsat.^(2/3), Tsat.^(3/3), Tsat.^(4/3)];
b = psat;
sol = A\b;
a1 = sol(1)
a2 = sol(2)
a3 = sol(3)
a4 = sol(4)
a5 = sol(5)
Best wishes
Torsten.

7 个评论

But this does not explain, why this matlab function throws this error message? Maybe there is a problem with the jacobi matrix?
You say that xdata and ydata are real-valued and that your fitting function can't produce complex results. If this is the case, the fitting tool will never produce complex fitting parameters.
Since you don't provide executable code and since I don't have a licence for the curve-fitting toolbox, I can't test what's wrong with your settings.
The easiest way is to write a function with the fitting expression, call this function with the xdata vector and the initial value vector for [a1 a2 ...] and see what it returns.
Thats right, but I though that there is maybe a possibilty to get this error message, even if my fitting function theoretically can't produce it. Maybe because of some entries of the jacobi matrix, this was just an idea of mine.
With your function above I get these values:
a1 =2.8976e+09 a2 =-1.8470e+09 a3 = 4.4305e+08 a4 = -4.7417e+07 a5 = 1.9111e+06
and Matlab has no problem to work with it.
Try this line
ft = fittype(@(a1,a2,a3,a4,a5,x) a1+a2*x.^(1/3)+a3*x.^(2/3)+a4*x.^(3/3)+a5*x.^(4/3),'independent',{'x'},'dependent',{'y'});
instead of yours.
By the way:
x.*^(2/3), x.*^(3/3) and x.*^(4/3) doesn't make sense.
"By the way:
x.*^(2/3), x.*^(3/3) and x.*^(4/3) doesn't make sense."
Yeah I know, it was just a little failure.
Now the error message ist "toofewstartpoints", even though the fit.m function should generate random start points, if no ones are given.
So mag(1,1) is equal to 4 in your code ?
Yes, but anyway I think I found the problem's source: the fitoption normalize. This could be an explanation for complex values.

请先登录,再进行评论。

类别

帮助中心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!

Translated by