Curve Fitting Tool - Power Fit

49 次查看(过去 30 天)
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')
ans =
General model Power1: ans(x) = a*x^b Coefficients (with 95% confidence bounds): a = 1.491 (1.239, 1.743) b = 0.2802 (0.1807, 0.3797)
a = 1.491;
b = 0.2802;
X=0:0.1:9;
Y=(a.*(X.^b));
plot(X,Y)

采纳的回答

Mathieu NOE
Mathieu NOE 2021-10-13
hello
even without the curve fitting toolbox you can get a good match using fminsearch :
sol = 1.4912 0.2802
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 个评论
Mohamed Rashad
Mohamed Rashad 2021-12-8

Yeah. Forgive me Mathieu. I was kind of busy in studies. Thank you for your answer.

Mathieu NOE
Mathieu NOE 2021-12-8
No problem
my pleasure ! and have a good day

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get 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