# Fit meta data to a custom equation

32 Ansichten (letzte 30 Tage)
Yang Hu am 23 Jul. 2024 um 17:55
Kommentiert: Yang Hu am 24 Jul. 2024 um 3:22
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 = 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.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Milan Bansal am 23 Jul. 2024 um 19:17
Bearbeitet: Milan Bansal am 23 Jul. 2024 um 19:19
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:
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 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Yang Hu am 24 Jul. 2024 um 3:22
I found the fitted results are depend on the intial guess, do you know if there is another way to get rid of it?

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by