フィルターのクリア

Curve Fitting Tool - Power Fit

49 ビュー (過去 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 Center および File ExchangeGet 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