擬合曲線的最常用方法是進行最小二乘擬合,這可以最小化數據與擬合之間的平方差之和。這就是爲什麼當y很大時,你的擬合更緊密:0.18值的偏差爲11%,只是0.000392的平方誤差,而240值的0.1%偏差是0.0576的平方誤差,更重要。
如果您關心的是偏差而不是絕對(平方)誤差,那麼您可以重新擬合擬合算法,或以巧妙的方式轉換您的數據。第二種方法是知道的常用和有用的工具。
在你的情況下做到這一點的一種方法是適合log(y)
而不是y
。這將有可能使小的錯誤更顯著的效果:
data = [0.1 237.98
1 25.836
10 3.785
30 1.740
100 0.804
300 0.431
1000 0.230
2000 0.180];
x = data(:,1);
y = data(:,2);
% Set up fittype and options.
ft = fittype('a/x^b + c/x^d', 'independent', 'x', 'dependent', 'y');
opts = fitoptions('Method', 'NonlinearLeastSquares');
opts.Display = 'Off';
opts.StartPoint = [0.420712466925742 0.585539298834167 0.771799485946335 0.706046088019609];
%% Usual least-squares fit
[fitresult] = fit(x, y, ft, opts);
yhat = fitresult(x);
% Plot fit with data.
figure
semilogy(x, y);
hold on
semilogy(x, yhat);
deviation = abs((y-yhat))./y * 100
%% log-transformed fit
[fitresult] = fit(x, log(y), ft, opts);
yhat = exp(fitresult(x));
% Plot fit with data.
figure
semilogy(x, y);
hold on
semilogy(x, yhat);
deviation = abs((y-yhat))./y * 100