Inconsistency in fitting function

1 view (last 30 days)
Sajid Afaque
Sajid Afaque on 3 Feb 2023
Commented: Sajid Afaque on 8 Feb 2023
Hello I am trying to fit a function
I have two similar kind of data, lets name two type of data
  1. a_data
  2. 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 Comments
Torsten
Torsten on 3 Feb 2023
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.
Sajid Afaque
Sajid Afaque on 8 Feb 2023
Thanks for the feedback. I have rephrased the questions with snippets. Hopefully now its able to clearly explain

Sign in to comment.

Answers (1)

Askic V
Askic V on 3 Feb 2023
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.
  1 Comment
Sajid Afaque
Sajid Afaque on 8 Feb 2023
Thanks for the time. I calculate error after each fit. The idea behind 200 initialisation was to have the best fitting resulting in minimum error out of this 200.
But as you said instead of using random value i will also try to use the calculated value for next trail.
I figured out that the slight change in function definition was helping to get a better fit
FittedCurve = a + (a) .* exp(-b .* (xdata-c).*(xdata-c)) + d.*abs(xdata)
% instead of the earlier one FittedCurve = a + (1-a) .* exp(-b .* (xdata-c).*(xdata-c)) + d.*abs(xdata)

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!