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에서 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!