When using the "fit" command with a startpoint vector, matlab will sometimes not fit the data and will instead just assume my startpoint is the best fit

I was fitting some data to a standard hyperbolic equation and I've been running into an issue. When I run the fit without a startpoint vector, I get a cfit object of wildly variable closeness of fit (as expected), but when I include a startpoint vector, I get out a cfit object with exactly my startpoint vector, regardless of how bad a fit that produces. I have several thousand data points and I'm trying to fit 3 parameters.
I had a similar issue with a much more complex equation that had several constants. I initially tried to have all the constants as variables and then just set the high and low bounds for those variables to be exactly the value of the constants, but instead matlab just returned a cfit with exactly my startpoint values for all the variables to be fit. When I instead put the values of the constants directly into the fit equation, the issue went away, but that's not an option for the hyperbolic fit. I've pasted code from both situations below, I haven't included my data because I don't know how to attach files.
Hyperbolic code:
y = "C/(x-theta) + yd";
S = [9.086e-09 0.04965 -5.991e-10];
[fit2, gof] = fit(T(1:100), X(1:100), y, 'StartPoint', S)
Brillouin code (faulty):
temp = 2;
y = sprintf(['Ms*((2*S+1)/(2*S) * coth(((2*S+1)/2)*Kd*x/T) - 1/(2*S) * ' ...
'coth(Kd*(x/T)/2))']);
S = [1.34e-4 .0045 1 temp]
R = [0 .0045 .8 0]
[fit2, gof] = fit(H2, M2, y, 'StartPoint', S, 'Lower', S-R, 'Upper', S+R)
Brillouin code ("fixed"):
temp = 2;
y = sprintf(['Ms*((2*S+1)/(2*S) * coth(((2*S+1)/2)*%e*x/%d) - 1/(2*S) * ' ...
'coth(%e*(x/%d)/2))'],1.34e-4, temp,1.34e-4, temp);
[fit2, gof] = fit(H2, M2, y)

4 Comments

What do you get back when you evaluate your equation with your startvector before calling "fit" ?
What is the exitflag from "fit" in the cases where your startvector is returned as optimal ?
I'm not quite sure what you mean by "evaluate your equation with your startvector before calling 'fit'". It's a function so it'll have a different value for every x value.
Exit flags for both are 1 and it gives the message 'Success. Fitting converged to a solution.'
Thank you for your help
Please attach the data .mat files needed to provide input to your code and run the fit.
This is the data I'm using for the hyperbolic fit, I don't still have the data for the other fit, and the hyperbolic one is the main concern. Thank you for your help.

Sign in to comment.

 Accepted Answer

Numerical problems are created when your T and X data are very different orders of magnitude. Change the units of your X to something 1e10 times smaller:
load 'Hyperbolic Fit Data.mat'
T=A(:,1); X=A(:,2)*1e10;
y = "C/(x-theta) + yd";
S = [9.086e1 0.04965 -5.991];
[fit2, gof] = fit(T, X, y, 'StartPoint', S)
fit2 =
General model: fit2(x) = C/(x-theta) + yd Coefficients (with 95% confidence bounds): C = 93.94 (93.85, 94.04) theta = -0.6276 (-0.6305, -0.6247) yd = -6.189 (-6.191, -6.187)
gof = struct with fields:
sse: 5.6550 rsquare: 0.9999 dfe: 2934 adjrsquare: 0.9999 rmse: 0.0439
plot(fit2,T,X)

2 Comments

Thank you so much! This has stymied me for longer than I care to admit and it never once occured to me that the answer could be so simple, but now that I know it makes total sense. Know that you've brightened my day.
Glad to hear it, but please Accept-click the answer to indicate that it resolved your question.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Asked:

on 25 Jan 2024

Commented:

on 25 Jan 2024

Community Treasure Hunt

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

Start Hunting!