I am having some issues fitting a curve using polyfit to log data. I think I am making a silly mistake during the fit plotting as the fitting is really bad during the beggining but can't seem to figure it out. Any help would be greatly appreciated! I have attached the data as: msd_help
load('msd_help')
figure()
loglog(s,m)
hold on
linearCoefficients = polyfit((s),(m), 1);
yFit = polyval(linearCoefficients, s);
loglog(s, yFit, 'r-', 'LineWidth', 2)

1 件のコメント

Yazan
Yazan 2021 年 8 月 6 日
編集済み: Yazan 2021 年 8 月 6 日
You are not doing anything wrong, and the fitting is working. However, you are plotting the results using a base 10 logarithmic scale. See below the same result plotted using a linear scale.

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2021 年 8 月 6 日

0 投票

Fit it to a power function:
LD = load('msd_help.mat');
m = LD.m;
s = LD.s;
fcn = @(b,x) x.^b(1).*exp(b(2));
B = fminsearch(@(b) norm(m - fcn(b,s)), rand(2,1))
yFit = fcn(B,s);
figure
loglog(s,m,'.')
hold on
plot(s, yFit,'-r')
hold off
grid
producing:
B =
0.867368600071621
-27.933653082691194
and:
.

6 件のコメント

Yazan
Yazan 2021 年 8 月 6 日
A 5th order polynomial also gives reasonable results.
Alex Sha
Alex Sha 2021 年 8 月 7 日
If the fitting function types are free, the follow is good enough:
y = p1*x/(1+p2*x^2)^p3+p4
Root of Mean Square Error (RMSE): 5.64364594544832E-13
Sum of Squared Residual: 1.91104437345451E-21
Correlation Coef. (R): 0.999945510161687
R-Square: 0.999891023292516
Parameter Best Estimate
---------- -------------
p1 3.82443123959562E-13
p2 2.48820059997632E-6
p3 0.334013945130774
p4 -8.79374499358471E-13
Manny Kins
Manny Kins 2021 年 8 月 9 日
編集済み: Manny Kins 2021 年 8 月 9 日
Thanks all for your help. In reference to Star Strider's answer, it is good for me because I wanted to fit to a straight line (apologies for not mentioning this in the initial question). Is there a reason why the straight line fit on the log plot only passes the later stages of the data (after s=100) and misses the rest?
Star Strider
Star Strider 2021 年 8 月 9 日
Is there a reason why the straight line fit on the log plot only passes the later stages of the data (after s=100) and misses the rest?
Yes.
The first-order unweighted power fit may not be the ideal model. (However the actual error is exceedingly small, and close to the limit of IEEE 754 floating-point representation.)
The best model is a mathematical expression of the process that created those data.
.
Manny Kins
Manny Kins 2021 年 8 月 9 日
Thanks for your help and insight
Star Strider
Star Strider 2021 年 8 月 9 日
As always, my pleasure!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

製品

リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by