Fitting a model to my data using non linear least square fit method

6 次查看(过去 30 天)
I have x values (first column in the excel file) and P values (second column in the excel file). I want to obtain the parameters 'q' and 'm' using the expression: P = [ 1 - (1-q) * m * x ] ^ ( 1 / (1 - q) ) in a log log plot and plot the same using non linear least square fit method.
  1 个评论
Diwakar Diwakar
Diwakar Diwakar 2023-7-21
% Step 1: Load data from the Excel file
data = xlsread('data.xlsx');
x = data(:, 1);
P = data(:, 2);
% Step 2: Define the equation and the error function for nonlinear fit
eqn = @(params, x) (1 - (1 - params(1)) * params(2) * x) .^ (1 / (1 - params(1)));
errorFunc = @(params) eqn(params, x) - P;
% Step 3: Use lsqcurvefit to obtain parameter estimates
initialGuess = [0.5, 1]; % Initial guess for q and m
paramsFit = lsqcurvefit(eqn, initialGuess, x, P);
% Extract the fitted parameters
q = paramsFit(1);
m = paramsFit(2);
% Step 4: Plot the data and the fitted curve on a log-log plot
x_vals_for_plot = logspace(log10(min(x)), log10(max(x)), 100); % Generating log-spaced x values for the plot
P_fit = eqn(paramsFit, x_vals_for_plot);
loglog(x, P, 'o', 'MarkerFaceColor', 'b'); % Plot the original data points with markers
hold on;
loglog(x_vals_for_plot, P_fit, 'r', 'LineWidth', 2); % Plot the fitted curve in red
hold off;
xlabel('x');
ylabel('P');
legend('Data', 'Fitted Curve');
title('Nonlinear Least Squares Fit');
grid on;

请先登录,再进行评论。

回答(2 个)

Mathieu NOE
Mathieu NOE 2023-7-21
hello
try this , hope it helps
results :
m = 24.5179
q = 1.2078
data = readmatrix('LVD_AE.xlsx');
x = data(:,1);
y = data(:,2);
% sort / unique x
[x,ia,ic] = unique(x);
y = y(ia);
% remove x = 0 data (just in case)
ind = x>eps;
x = x(ind);
y = y(ind);
% remove y = 0 data (just in case)
ind = y>eps;
x = x(ind);
y = y(ind);
% fit using log spaced values
xx = logspace(log10(min(x)),log10(max(x)),100);
yy = interp1(x,y,xx,'linear');
loglog(x,y,'*-',xx,yy,'*-');
% curve fit using fminsearch
% We would like to fit the function :
% P = [ 1 - (1-q) * m * x ] ^ ( 1 / (1 - q) )
f = @(m,q,x) (1-(1-q)*m*x).^(1/(1-q));
obj_fun = @(params) norm(f(params(1), params(2), x)-yy);
C1_guess = [10 1 ];
sol = fminsearch(obj_fun, C1_guess); %
m_sol = sol(1)
q_sol = sol(2)
yfit = f(m_sol, q_sol, xx);
Rsquared = my_Rsquared_coeff(yy,yfit); % correlation coefficient
loglog(xx, yfit, '-',x,y, 'r .', 'MarkerSize', 25)
title(['Fit equation to data : R² = ' num2str(Rsquared) ], 'FontSize', 20)
xlabel('x data', 'FontSize', 20)
ylabel('y data', 'FontSize', 20)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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

Alex Sha
Alex Sha 2023-9-27
The best solution:
Root of Mean Square Error (RMSE): 0.0143800358786989
Correlation Coef. (R): 0.998885623657614
R-Square: 0.997772489149861
Parameter Best Estimate
--------- -------------
q 1.42974692753178
m 46.7110560131104

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by