![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/766221/image.png)
Curve Fitting Tool - Power Fit
48 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mohamed Rashad
le 13 Oct 2021
Commenté : Mathieu NOE
le 8 Déc 2021
So I have been trying to match the Power fit Curve obtained from my Program and the one Obtained using curve fitting tool. In the curve fitting tool i get a result saying "Cannot fit Power functions to data where X has nonpositive values". So here is my code and I have attached the screenshots of both Plots. Suggest me how to make this right. Thank You.
clc
clear;
x=[0.75; 2; 3; 4; 6; 8; 8.5];
y=[1.2; 1.95; 2; 2.4; 2.4; 2.7; 2.6];
fit(x,y,'power1')
a = 1.491;
b = 0.2802;
X=0:0.1:9;
Y=(a.*(X.^b));
plot(X,Y)
0 commentaires
Réponse acceptée
Mathieu NOE
le 13 Oct 2021
hello
even without the curve fitting toolbox you can get a good match using fminsearch :
sol = 1.4912 0.2802
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/766221/image.png)
clc
clearvars
% data
x=[0.75; 2; 3; 4; 6; 8; 8.5];
y=[1.2; 1.95; 2; 2.4; 2.4; 2.7; 2.6];
%power fit
f = @(a,b,x) a.*x.^b;
obj_fun = @(params) norm(f(params(1), params(2),x)-y);
sol = fminsearch(obj_fun, [1,1]);
a_sol = sol(1);
b_sol = sol(2);
x_fit = linspace(0,10,300);
y_fit = f(a_sol, b_sol, x_fit);
Rsquared = my_Rsquared_coeff(y,f(a_sol, b_sol, x)); % correlation coefficient
figure(3);plot(x,y,'o',x_fit,y_fit,'-')
title(['Data fit - R squared = ' num2str(Rsquared)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Rsquared = my_Rsquared_coeff(data,data_fit)
% R2 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with Curve Fitting Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!