Why MATLAB gives very bad fitting parameters?

15 visualizaciones (últimos 30 días)
Jaladhar Mahato
Jaladhar Mahato el 20 de Jul. de 2018
Comentada: Jaladhar Mahato el 22 de Jul. de 2018
Why MATLAB gives very bad fitting parameters when fittype and fit are used together. Below is the one example where I have a 3D matrix ( Auto) and in which I am fitting the Auto(p,q,:) elements using a for a loop. I am calculating one parameter ( D(p,q)=fit1.D) from the fitting. Sometimes I used catch and try to avoid the breakdown of the loop.
However, the point is that I think MATLAB is not efficient in curve fitting. I used ( Origin) which gave nice good fitting but the same data and the same equation does not give me expected parameters in MATLAB. I could use the curve fitting toolbox but I want to recursively fit the data and want to extract parameters in a loop which refrain me for using the toolbox.
clear all; close all; clc;
load('Auto.mat');
l2= size(Auto);
for p=1:l2(1)
for q=1:l2(2)
% This is part for the data which has values less than 0.6 in the
% first two matrix in the AutoX3 matrix
y=reshape(Auto(p,q,1:end),[],1);
if y(1,:)<0.6 || y(2,:)<0.55
y(:,:)=NaN;
end
x=(0:0.5:(length(y)-1)*0.5)'; % The X-axis
% The fitting equation %
f = fittype('(A./(sqrt(1+ ((x*D)./(0.16.^2)))))');
% Try and catch to successfully continue the loop%
try
fit1 = fit(x,y,f,'StartPoint',[0.1 0.1]);
RateCons(p,q)=fit1.D;
Amplitude(p,q)=fit1.A;
catch
RateCons(p,q)=NaN;
Amplitude(p,q)=NaN;
end
end
end
  7 comentarios
Walter Roberson
Walter Roberson el 21 de Jul. de 2018
For example, at p=32, q=33, then A and D are determined by fit to be 0.929878332309704, 0.00491910153972962 . When I convert the residue formula into symbolic form using rationals instead of floating point numbers, and use a symbolic package to minimize, I get .929869232279317, 0.00491889395158922 as the global best, which differ in positions from fit's results by about 1E-5 and 2E-7 respectively.
But the exact best position is subject to numeric roundoff problems, and working numerically in MATLAB I get slightly better residue at 0.929869232278125901, 0.00491889395270517921. We are talking about differences in residue on the order of 4E-17 .
The overall residue surfaces have a fairly consistent shape of being more or less a tilted parabola sheet outside the major basin of attraction; any minimizer that looked at the gradient would get centered towards the middle of the parabola pretty quickly, and would follow that line into the major basin of attraction. The basin of attraction is then an obvious paraboloid, so a gradient following minimizer would get pulled to a small distance from the global minima residue in a small number of iterations.
In the locations I tested, I saw no evidence at all of any important local minima: there was no competing minima that a reasonable algorithm might accidentally get caught in.
Which is not to say that fit() is immune to having problems with competing local minima: it does, and there have been a few cases discussed in the last month or so where fit could reasonably accidentally get stuck in the wrong minima, especially cases involving sum of exponentials. fit can end up with a pretty high confidence that it has found the right parameters, and yet be wrong because the other better set of parameters might be difficult to get close to. But this function is not one of those cases: there is an unambiguous area of global minima and in each trial with your data that I investigated, fit() got close to it without difficulty.
Jaladhar Mahato
Jaladhar Mahato el 22 de Jul. de 2018
Dear Walter, thanks for your elaboration. I think after setting the bound I should not get unwanted parameters as my function sometimes involve multiplication of only one exponential (not the sum of exponential).

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Jul. de 2018
%clear all; close all; clc;
load('Auto.mat');
%arbitrary constant as part of the equation. I do not know where it comes from
C = 0.16^2;
l2= size(Auto);
RateCons = NaN(l2(1), l2(2));
Amplitude = NaN(l2(1), l2(2));
x=(0:0.5:(l2(3)-1)*0.5)'; % The X-axis
LBA = -inf;
LBD = -C/x(end); %below this, the calculation goes complex
UBA = inf;
UBD = inf;
LB = [LBA, LBD];
UB = [UBA, UBD];
% The fitting equation %
f = fittype(@(A,D,x) (A./(sqrt(1+ ((x*D)./C)))));
opts = fitoptions(f);
opts.Lower = LB;
opts.Upper = UB;
opts.StartPoint = [0.1 0.1];
for p=1:l2(1)
for q=1:l2(2)
% This is part for the data which has values less than 0.6 in the
% first two matrix in the AutoX3 matrix
y=reshape(Auto(p,q,1:end),[],1);
if any(isnan(y(:))) || y(1,:)<0.6 || y(2,:)<0.55
%we used to assign NaN here, but now we initialize everything to NaN
else
% Try and catch to successfully continue the loop%
try
fit1 = fit(x,y,f,opts);
RateCons(p,q)=fit1.D;
Amplitude(p,q)=fit1.A;
catch ME
%we used to assign NaN here, but now we initialize everything to NaN
end
end
end
end
  1 comentario
Jaladhar Mahato
Jaladhar Mahato el 21 de Jul. de 2018
Thanks Walter Roberson. The output looks decent. I have to verify it with the known example. Thanks again.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by