# Fit coefficients are equal (to starting points) in 3000 different fits

13 views (last 30 days)
Gil on 5 Aug 2022
Commented: Cris LaPierre on 7 Aug 2022
Hey all,
I'm trying to get the fit coefficients data, for about 3000 fits with added data, starting with 10 points and adding another and another until I get to 2999 points.
My goal is to see how the fit coefficients, and eventually the gooddness of fit as well, changes over the change in the data. When I run my script the coefficients end up fixed as their start points. What did I do wrong?
Another question is do you maybe know how do I extract the R-square value out of the fit in a similar way? and the coefficients errors?
One last less urgent question - right now I need to fill manually the data array (x,y as follows) and cannot extract it from the workspace. Is there a way to use the workspace here?
I'm adding my script for clearer understanding.
function[x,y,f] = GraphFittingAnalysisFrom0 (x,y,f) % x,y are columns and f the fitting function.
prompt_x = ['x='];
prompt_y = ['y='];
prompt_f = ['fit type f='];
x = input(prompt_x);
y = input(prompt_y);
f=input(prompt_f);
Fit = fit(x,y,f);
A = coeffnames(Fit);
Num_of_coeff = size(A,1);
disp(['the number of coefficients is ', num2str(Num_of_coeff)]) % check by output
Start_point = zeros([Num_of_coeff 1]); % asking user to enter coefficients' start point
for i = 1: Num_of_coeff
Start_point(i) = input(['start point of ',A{i},' ']);
end
Coefficients = zeros([Num_of_coeff 2999]); % where values of coefficients will be saved
for n = 10:2999 % the problem is in the fits here -it does not change the coefficients and give them as their start points
x_n = x(1:n);
y_n = y(1:n);
options = fitoptions(f);
options.Robust = 'LAR';
options.StartPoint = Start_point;
options.MaxIter = 25000;
options.TolFun = 25000;
Fit_n = fit(x_n,y_n,f,options);
Val = coeffvalues(Fit_n);
Coefficients(:,n) = Val.'; % saving the coefficients values of the nth fit in the nth column of Coefficeints
end
disp (Coefficients)
tiledlayout(1,Num_of_coeff) % plotting the coefficients graphs
X=[1:2999];
for i = 1 : Num_of_coeff
Y=Coefficients(i,:);
nexttile
plot(X,Y)
title(A{i})
end
end
Cris LaPierre on 7 Aug 2022
My apologies. It looks like I was accidentally loading the wrong mat file on my computer (XYData instead of xyvectors)

Bruno Luong on 7 Aug 2022
Edited: Bruno Luong on 7 Aug 2022
Can you do check whereas they are identical with this command after the loop
isequal(Coefficients(:,10),Coefficients(:,2999))
fprintf('%e ', Coefficients(:,10)-Coefficients(:,2999))
fprintf('\n')
Gil on 7 Aug 2022
@Bruno Luong. Of course you're right! Thanks!!!!

R2021b

### Community Treasure Hunt

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

Start Hunting!