MATLAB Answers

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

12 ビュー (過去 30 日間)
Matti Rademacher
Matti Rademacher 2019 年 6 月 26 日
Commented: Matti Rademacher 2019 年 6 月 26 日
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 件のコメント

表示 3 件の古いコメント
Matti Rademacher
Matti Rademacher 2019 年 6 月 26 日
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
[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
% 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;
% Define end of while loop
if t_err == 0 || ct_err == rp_err, tf_err = 1; 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';
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;
Matti Rademacher
Matti Rademacher 2019 年 6 月 26 日
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.

サインイン to comment.

件の回答 (1)

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

  7 件のコメント

表示 4 件の古いコメント
Matti Rademacher
Matti Rademacher 2019 年 6 月 26 日
"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.
Torsten 2019 年 6 月 26 日
So mag(1,1) is equal to 4 in your code ?
Matti Rademacher
Matti Rademacher 2019 年 6 月 26 日
Yes, but anyway I think I found the problem's source: the fitoption normalize. This could be an explanation for complex values.

サインイン to comment.

サインイン してこの質問に回答します。

Translated by