Finding best fit function for a data set
51 次查看(过去 30 天)
显示 更早的评论
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.
回答(2 个)
Sulaymon Eshkabilov
2023-2-9
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')
0 个评论
Arka
2023-2-9
编辑:Arka
2023-2-9
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):
The plot is given below:
You can refer to the documentation link for the ‘fit’ function below:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!