Curve Fit data using FIT

1 Ansicht (letzte 30 Tage)
Jason
Jason am 16 Mai 2016
Bearbeitet: the cyclist am 5 Dez. 2019
Hi, I am trying to fit some data to the following function below:
I can get it to fit in mathcad but cannot get it working in matlab. This is my coding.
%Define equation to use (Square - Gaussian)
myEqn='(-a/(1+exp(c*((x-b)/h).^2)-1))+d'
f = fit(x,y,myEqn,'Normalize','off', 'StartPoint',[Bhi,2,cx,20,Bhi]);
%Results of fit
coeffs=coeffvalues(f);
a=coeffs(1)
c=coeffs(2)
b=coeffs(3)
h=coeffs(4)
d=coeffs(5)
I have assumed the coefficients match up as shown above.
Then view the fit at higher resoluton:
% %Increase resolution of x data
xdataFine=(linspace(x(1),x(end),500))'
% %plot high res fit
myEqn = (-a/(1+exp(c*((xdataFine-b)/h).^2)-1))+d;
plot(xdataFine,myEqn,'k--')
hold off
Thanks for any help. Jason
  7 Kommentare
dpb
dpb am 19 Mai 2016
Bearbeitet: dpb am 19 Mai 2016
I've still not had time to do anything; is much different solution than cyclist got and you're right, it's much better result.
Wonder if Mathcad might be using a weighted regression? Any info available from in on how it does the fitting?
ADDENDUM
W/ cyclist's starting point I tried lsqnonlin w/o bounds and it returns almost identically the same solution as does nlinfit.
Jason
Jason am 20 Mai 2016
The method used by Mathcad is shown below.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

the cyclist
the cyclist am 19 Mai 2016
Bearbeitet: the cyclist am 5 Dez. 2019
I used nlinfit from the Statistics and Machine Learning Toolbox. Here is the code, and the resulting plot:
xy = [ ...
1 317.3564
2.01 320.5743
3.02 320.5941
4.03 322.0396
5.04 320.9703
6.05 317.4257
7.06 320.099
8.07 320.2574
9.08 319.3762
10.09 320.3168
11.1 316.3069
12.11 317.7723
13.12 320.198
14.13 316.1782
15.14 316.8713
16.15 315.7723
17.16 314.1782
18.17 312.9505
19.18 311.8614
20.19 314.7327
21.2 307.802
22.21 308.8416
23.22 307.2178
24.23 304.4059
25.24 301.2871
26.25 297.396
27.26 294.802
28.27 291.9208
29.28 285.5149
30.29 279.7822
31.3 270.5446
32.31 261.9802
33.32 246.7228
34.33 224.8119
35.34 194.3267
36.35 160.7129
37.36 127.1089
38.37 103.9406
39.38 88.6139
40.39 77.505
41.4 70.0594
42.41 65.0792
43.42 61.3465
44.43 53.604
45.44 48.5248
46.45 44.495
47.46 42.6337
48.47 42.604
49.48 38.198
50.49 37.198
51.5 35.6535
52.51 34.4059
53.52 30.6139
54.53 33.5149
55.54 34.3168
56.55 33.9802
57.56 34.0198
58.57 37.1386
59.58 37.6634
60.59 38.9109
61.6 42.802
62.61 46.1782
63.62 51.1584
64.63 54.4455
65.64 60.8614
66.65 67.7624
67.66 78.099
68.67 98.2673
69.68 119.3762
70.69 148.7129
71.7 183.6733
72.71 214.8416
73.72 238.5743
74.73 251.7624
75.74 257.703
76.75 267.6337
77.76 274.0594
78.77 280.6436
79.78 287.8614
80.79 290.802
81.8 295.8713
82.81 300.8614
83.82 306.6832
84.83 306.1287
85.84 308.2772
86.85 306.505
87.86 309.802
88.87 312.2772
89.88 312.5248
90.89 315.396
91.9 319.1089
92.91 318.6634
93.92 320.5248
94.93 321.0891
95.94 319.4752
96.95 323.7822
97.96 322.6832
98.97 327.3663
99.98 324.2178
100.99 326.7723
102 325.297
];
x = xy(:,1);
y = xy(:,2);
% Define function that will be used to fit data
% (F is a vector of fitting parameters)
f = @(F,x) (-F(1)./(1+exp(F(2).*((x-F(3))./F(4)).^2)-1))+F(5);
F_fitted = nlinfit(x,y,f,[320 0.1 54 17 320]);
% Display fitted coefficients
disp(['F = ',num2str(F_fitted)])
% Plot the data and fit
figure
plot(x,y,'*',x,f(F_fitted,x),'g');
legend('data','fit')

Alex Sha
Alex Sha am 4 Dez. 2019
the fit function "myEqn='(-a/(1+exp(c*((x-b)/h).^2)-1))+d'" is not correct, refer to:
eq.png
the function should be: myEqn='(-a/(1+exp(c*(((x-b)/h).^2)-1)))+d'
the results:
Root of Mean Square Error (RMSE): 7.86055008744128
Sum of Squared Residual: 6240.61301539449
Correlation Coef. (R): 0.997741981937716
R-Square: 0.995489062521003
Adjusted R-Square: 0.995397002572452
Determination Coef. (DC): 0.995489062521002
Chi-Square: 15.6369505666224
F-Statistic: 5296.40182594651
Parameter Best Estimate
---------- -------------
a -296.253072488933
b 53.8026426726725
c -2.93929851772126
d 17.5174641892142
h 17.657680608597
c237.jpg

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by