Inconsistency in fitting function
2 次查看(过去 30 天)
显示 更早的评论
Hello I am trying to fit a function
I have two similar kind of data, lets name two type of data
- a_data
- b_data
model is able to fit a_data well each time, but it fails to fit b_data most of the times.
I dont understand why ? The model doesnt throws any error but it results in bad fitting
probably the model is getting struck in bad local minima ,which results in bad fit, but i am not sure of it.
load("b_data.mat")
v = b_data(:,1); %%same in case of b_data
c = b_data(:,2);
function_type=2; %--> a,b,c & d parameter
mcc_best = 0;
figure;
plot(v,c);
hold on;
for i=1:1:200
if function_type == 1
[estimates, model] = fit_C_U_function(v, c);
end
if function_type == 2
[estimates, model] = fit_C_U_function_plus(v, c);
end
[sse, FittedCurve] = model(estimates);
sst = c*transpose(c);
mcc = 1- sse./sst;
if mcc>mcc_best
mcc_best = mcc;
estimates_best = estimates;
FittedCurve_best = FittedCurve;
end
end
plot(v,FittedCurve_best);
function [estimates, model] = fit_C_U_function_plus(xdata, ydata)
% Call fminsearch with a random starting point.
start_point = rand(1, 4);
model = @expfun;
options = optimset('Display','Off');
estimates = fminsearch(model, start_point,options);
function [sse, FittedCurve] = expfun(params)
a = params(1);
b = params(2);
c = params(3);
d = params(4);
FittedCurve = a + (1-a) .* exp(-b .* (xdata-c).*(xdata-c)) + d.*abs(xdata);
ErrorVector = FittedCurve - ydata;
sse = sum(ErrorVector .^ 2);
end
end
3 个评论
Torsten
2023-2-3
Taking random initial values for a four-parameter model and hoping for a perfect fit is very optimistic.
Only a few things in this world work automatically - for most of them, one has to invest some time and effort.
回答(1 个)
Askic V
2023-2-3
One way to overcome (not 100% guarantee though) this would be the following. If you really have no clue what the coefficient values would be,then you can use random values. In all other function calls inside the loop, you should try to send calculated values as initial values.
So your function
function [estimates, model] = fit_C_U_function_plus(xdata, ydata)
should be
function [estimates, model] = fit_C_U_function_plus(xdata, ydata, initial_val)
I don't see a point why would you use random initialization 200 times, it is much better to use previously calculated values.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!