MATLAB: Matlab 2018a fit function wrong coefficients
curve fittingMATLAB
Hi,
I tried to use the fit function for a rational fitting (rat24) and I got very strange results although the fitted curve was similar to my function.
So I tried to use it with a known function: y=2t and did the fitting and the output again was wrong.
So in the following example I expected p1=0 and p2=2 and I get different results.
What am I doing wrong?
Thanks,
Avihai
Best Answer
The 'Normalize','on' name-value pair tells the function to centre and scale the data. This is appropriate for some situations where the independent variable does not begin at 0, and the dependent variable is much greater than 0. (To then plot it, that information must be included or the result will be significantly different from that expected.) Changing to 'Normalize','off', will produce the anticipated result for the parameter estimates.
I suspect that the problem is that you have normalised the data, and then forgot that when you looked at the parameter values.
For example, since you didn't bother give us any data to work with, I've made my own to roughly follow yours. In this case I know the true parameters, "a" & "b". I've also added a small amount of noise just for fun.
x = linspace(180,380,8)';
a = 280; b=0.05; % approx constants
y = 1.0./(1+exp(-b*(x-a))) + 0.05*randn(size(x))
plot(x,y,'o')
If you plot this data, you'll see something similar to your original data points.
Now we are ready to fit. Always a good idea to give it a reasonable starting guess.
Temperature=x; Gas_Conversion=y;
ft = fittype( '1.0./(1+exp(-b*(x-a)))');
options = fitoptions('method','NonlinearLeastSquares'); % Must do this, in order to specify an initial guess
options.Normal = 'off'; % TURN OFF !!! (or re-scale the coefficients)
options.StartPoint = [280, 0.05]'; % default start point for a & b
f=fit(Temperature,Gas_Conversion, ft, options)
MyCoeffs=coeffvalues(f);
a= MyCoeffs(1);
b= MyCoeffs(2);
h = plot( f, Temperature,Gas_Conversion);
Now we get a reasonable plot with
f =
General model:
f(x) = 1.0./(1+exp(-b*(x-a)))
Coefficients (with 95% confidence bounds):
a = 278 (272.3, 283.7)
b = 0.06609 (0.04472, 0.08745)
sensible coefficients, close to what I generated the original data with.
Best Answer