Curve Fitting Tool - Power Fit

76 visualizaciones (últimos 30 días)
Mohamed Rashad
Mohamed Rashad el 13 de Oct. de 2021
Comentada: Mathieu NOE el 8 de Dic. de 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')
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)

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 13 de Oct. de 2021
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 comentarios
Mohamed Rashad
Mohamed Rashad el 8 de Dic. de 2021

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

Mathieu NOE
Mathieu NOE el 8 de Dic. de 2021
No problem
my pleasure ! and have a good day

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by