How to make a good fittings
1 view (last 30 days)
Show older comments
x =
1.0e-08 *
0
0.0430
0.0770
0.1061
0.1309
0.1522
0.1705
0.1918
0.2123
0.2302
0.2471
0.2611
0.2743
0.2852
0.2962
0.3064
0.3170
0.3271
0.3386
0.3484
0.3609
0.3715
0.3801
0.3789
y =
1.0e-05 *
0
0.0008
0.0031
0.0063
0.0107
0.0159
0.0214
0.0263
0.0315
0.0372
0.0432
0.0498
0.0564
0.0631
0.0698
0.0769
0.0839
0.0910
0.0979
0.1054
0.1124
0.1199
0.1275
0.1347
g=fittype(@(a,x) 4/3.*a.*(10.^(-9)*10).^0.5.*x.^(3/2),'coefficient',{'a'},'independent',{'x'},'dependent',{'y'});
myfit=fit(x,y,g);
plot(myfit,x,y);
These are my codes for fitting my data with the function of a*x^2/3 finding the coefficient 'a'.
However,

the results are gone wrong. My fitting function (Orange) looks really deviated from the data points (Blue).
'a' coefficient should be about 30*10^10 value to be overlapped with each other like the below image.

Could you give me the advice to improve my fitting codes?
Thanks.
Best regards,
qwertypo.
0 Comments
Accepted Answer
Mathieu NOE
on 16 Feb 2023
hello
sorry I don't have the curve fitting toolbox but such a simple fit can be easily done with fminsearch
the constant you are looking for is
a_sol = 3.5829e+10
clear;
x = 1.0e-08 *[0
0.0430
0.0770
0.1061
0.1309
0.1522
0.1705
0.1918
0.2123
0.2302
0.2471
0.2611
0.2743
0.2852
0.2962
0.3064
0.3170
0.3271
0.3386
0.3484
0.3609
0.3715
0.3801
0.3789];
y = 1.0e-05 *[0
0.0008
0.0031
0.0063
0.0107
0.0159
0.0214
0.0263
0.0315
0.0372
0.0432
0.0498
0.0564
0.0631
0.0698
0.0769
0.0839
0.0910
0.0979
0.1054
0.1124
0.1199
0.1275
0.1347];
const = 4/3.*(10.^(-9)*10).^0.5;
% equation model y = a*const*x^(3/2)
f = @(a,x) (a*const*x.^(3/2));
obj_fun = @(params) norm(f(params(1), x)-y);
% IC guessed
a_ic = (y(end)/(const*x(end).^(3/2)))
sol = fminsearch(obj_fun, a_ic);
a_sol = sol(1)
y_fit = f(a_sol, x);
Rsquared = my_Rsquared_coeff(y,y_fit); % correlation coefficient
figure(1)
plot(x,y,'rd',x,y_fit,'b-');
title(['Power Fit / R² = ' num2str(Rsquared) ], 'FontSize', 15)
ylabel('y', 'FontSize', 14)
xlabel('x', 'FontSize', 14)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Rsquared = my_Rsquared_coeff(data,data_fit)
% R² correlation coefficient computation
% The total sum of squares
sum_of_squares = sum((data-mean(data)).^2);
% The sum of squares of residuals, also called the residual sum of squares:
sum_of_squares_of_residuals = sum((data-data_fit).^2);
% definition of the coefficient of correlation is
Rsquared = 1 - sum_of_squares_of_residuals/sum_of_squares;
end
4 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!