Curve Fitting Tool - Power Fit

조회 수: 89 (최근 30일)
Mohamed Rashad
Mohamed Rashad 2021년 10월 13일
댓글: Mathieu NOE 2021년 12월 8일
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