Finding best fit function for a data set

52 views (last 30 days)
Abdullah Azzam
Abdullah Azzam on 9 Feb 2023
Edited: Arka on 9 Feb 2023
Hi All, I have a set of X,Y,Z Data and I am trying to find the best fit equation between the input variables (X,Y) and the output variable (Z). I have been manually trying different functions that and so far the best function I have came up with is (Z = Log(X*Y) with R2 of 0.59) I am not sure if there is an automated way to find even more best fitting equation for this data. I have attached the excel sheet that contain the data. Thanks in advance for your help.

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 9 Feb 2023
You can try cftool - curve fitting toolbox, or use fit(), e.g.:
D = readmatrix('Samples_Data.xlsx');
X = D(:,2);
Y = D(:,3);
Z = D(:,4);
FModel = fit([X,Y], Z, 'poly23');
plot(FModel, [X, Y], Z)
[FModel, Fit_quality]=fit([X,Y], Z, 'poly23')
Linear model Poly23: FModel(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p21*x^2*y + p12*x*y^2 + p03*y^3 Coefficients (with 95% confidence bounds): p00 = 2595 (-335.2, 5525) p10 = -394.5 (-854.9, 65.83) p01 = -4.988 (-15.39, 5.417) p20 = 14.19 (-2.286, 30.67) p11 = 1.61 (-0.3392, 3.559) p02 = -0.0514 (-0.1291, 0.02633) p21 = -0.0701 (-0.1506, 0.01037) p12 = 0.002326 (-0.001086, 0.005738) p03 = 1.234e-05 (-3.496e-05, 5.963e-05)
Fit_quality = struct with fields:
sse: 592.8486 rsquare: 0.8895 dfe: 3 adjrsquare: 0.5948 rmse: 14.0576

Arka
Arka on 9 Feb 2023
Edited: Arka on 9 Feb 2023
Hello,
From what I understood, you have a 3 variable dataset, and you would like to automate the process of getting a line of best fit for the dataset.
You can use the ‘fit’ function and its supported polynomial solvers to automate the process of finding a polynomial best fit function for the dataset.
The code for the same is given below:
data = readmatrix('Samples_Data.xlsx');
x = data(:, 2);
y = data(:, 3);
z = data(:, 4);
solvers = ["poly11", "poly12", "poly13", "poly21", "poly22", "poly23", "poly31", "poly32", "poly33", "poly41"];
rSq = 0;
bestModel = 0;
for i=1:size(solvers, 2)
[model, goodnessOfFit] = fit([x, y], z, solvers(i));
if goodnessOfFit.rsquare > rSq
rSq = goodnessOfFit.rsquare;
bestModel = model;
end
end
bestModel
plot(bestModel, [x, y], z)
The output gives us the best fit function (and the solver used for it):
output
The plot is given below:
plot
You can refer to the documentation link for the ‘fit’ function below:

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!