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
10 views (last 30 days)
Show older comments
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
Accepted Answer
Matt J
on 25 Jan 2024
Edited: Matt J
on 25 Jan 2024
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)
plot(fit2,T,X)
2 Comments
Matt J
on 25 Jan 2024
Glad to hear it, but please Accept-click the answer to indicate that it resolved your question.
More Answers (0)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!