Fit meta data to a custom equation

조회 수: 2 (최근 30일)
Yang Hu
Yang Hu 2024년 7월 23일
댓글: Yang Hu 2024년 7월 24일
Hello everyone,
I have been having trouble fitting a set of meta data (attached called IndiMSD) to this custom equation, doing fit to a custom equation and output parameters of the equation.
The first row of data points of x and y is zero, it will output Inf of NaN when do the fit. So I disgard all in x and y matrix.
now the x and y are below:
x_input = readmatrix('IndiMSD.xlsx');
x = x_input(2:end,:); % first zero rows are disgarded, now x is 96 by 112, instead of 97 by 112
y = 15:15:1440;% first zero data point is disgarded, now y is 96 by 1
I want to fit each paif of column of x with y as above. For example, x(:,1) will be fitted with y, output its own S, P value with goodness of fit; , x(:,2) fits with y, output its own S, P value with goodness of fit, and so on until the last column. All fitted results of S, P and goodness of fit will be stored in separate matrice.
The fitted equation is
y = 2*((S^2)*(P^2)) * ((x./P) - 1 + exp(-(x./P)));
I really appreciate it if anyone has some ideas of how to do it.

채택된 답변

Milan Bansal
Milan Bansal 2024년 7월 23일
편집: Milan Bansal 2024년 7월 23일
Hi Yang Hu,
As per my understanding you wish to fit a each column of x with given y and determine the S and P parameters along with goodness of fit for each column in the attached table. Here, I am assuming the metrics for goodness of fit is R^2 value.
You can use lsqcurvefit function for this task. It is based on least square method. It adjusts the parameters of a given model to best fit a set of data points by minimizing the sum of the squares of the residuals. Please refer to the documentation to learn more about lsqcurvefit function : https://www.mathworks.com/help/optim/ug/lsqcurvefit.html
Please refer to the steps in following code snippet to learn how lsqcurvefit can be used:
% Load the data
x_input = readmatrix('IndiMSD.xlsx');
x = x_input(2:end, :); % Discard the first row
y = (15:15:1440)'; % Make sure y is a column vector
% Preallocate
numCols = size(x, 2);
S_values = zeros(1, numCols); % S parameter
P_values = zeros(1, numCols); % P parameter
gof_values = zeros(1, numCols); % goodness of fit
% Custom equation as a function handle
customEquation = @(params, x) 2 * ((params(1)^2) * (params(2)^2)) * ((x./params(2)) - 1 + exp(-(x./params(2))));
% Define the options for lsqcurvefit
options = optimset('Display', 'off');
% Loop through each column of x
for col = 1:numCols
x_col = x(:, col);
% Initial guess for parameters [S, P], please modify this as per your
% requirement
initialGuess = [1, 1];
% Perform the curve fitting
[fittedParams, ~, ~, exitflag, output] = lsqcurvefit(customEquation, initialGuess, x_col, y, [], [], options);
% Store the fitted parameters
S_values(col) = fittedParams(1);
P_values(col) = fittedParams(2);
% Calculate goodness of fit (e.g., R-squared)
y_fit = customEquation(fittedParams, x_col);
SS_res = sum((y - y_fit).^2);
SS_tot = sum((y - mean(y)).^2);
gof_values(col) = 1 - (SS_res / SS_tot);
end
% Display Results
% disp('Fitted S:'); disp(S_values);
% disp('Fitted P:'); disp(P_values);
% disp('Goodness of fit (R-squared):'); disp(gof_values);
Hope this helps!
  댓글 수: 1
Yang Hu
Yang Hu 2024년 7월 24일
I found the fitted results are depend on the intial guess, do you know if there is another way to get rid of it?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by