Generate Code from Curve Fitting App
Show older comments
Hello! so I have multiple sets of data, each of 2 independent (coordinates in space) and 1 dependent (energy flux) variables for which I am trying to find a suitable surface fit. I have used the curve fitting app to generate a polyinomial surface fit for one of the data sets (I did it manually at first and then genrated its function code); however, the code generated gives a total of 25 plots when run, which is unnecessary.
In short, what I am trying to do is generate a surface fitting function (creatFit) and then edit so that it finds the best fit possible (highest R-square value with no center and scale warrning) and then plot the this best fit only. I need this in a code so that I do not have to do it manually in the app for every data set. Furthermore, I want to have a matrix or table that shows the degree of each curve, its R-square, and weather any warning (center and scale) were ditected for that fit. The code segment I currently have is as below, I have deleted the plotting commands:
function [fitresult, gof] = createFits(z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy)
%CREATEFITS(Z_LEFT_BOTTOM,X_LEFT_BOTTOM,EB_LEFT_BOTTOM_STRIP_ARRAY_ENERGY)
% Create fits.
%
% Data for fit:
% X Input : z_Left_Bottom
% Y Input : x_Left_Bottom
% Z Output: Eb_Left_Bottom_strip_array_energy
% Output:
% fitresult : a cell-array of fit objects representing the fits.
% gof : structure array with goodness-of fit info.
%% Initialization.
% Initialize arrays to store fits and goodness-of-fit.
fitresult = cell( 25, 1 );
gof = struct( 'sse', cell( 25, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
%% Fit: '1x1'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly11' );
% Fit model to data.
[fitresult{1}, gof(1)] = fit( [xData, yData], zData, ft );
%% Fit: '2x1'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly21' );
% Fit model to data.
[fitresult{2}, gof(2)] = fit( [xData, yData], zData, ft );
%% Fit: '3x1'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly31' );
% Fit model to data.
[fitresult{3}, gof(3)] = fit( [xData, yData], zData, ft );
%% Fit: '4x1'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly41' );
% Fit model to data.
[fitresult{4}, gof(4)] = fit( [xData, yData], zData, ft );
%% Fit: '5x1'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly51' );
% Fit model to data.
[fitresult{5}, gof(5)] = fit( [xData, yData], zData, ft );
%% Fit: '1x2'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly12' );
% Fit model to data.
[fitresult{6}, gof(6)] = fit( [xData, yData], zData, ft );
%% Fit: '2x2'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly22' );
% Fit model to data.
[fitresult{7}, gof(7)] = fit( [xData, yData], zData, ft );
%% Fit: '3x2'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly32' );
% Fit model to data.
[fitresult{8}, gof(8)] = fit( [xData, yData], zData, ft );
%% Fit: '4x2'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly42' );
% Fit model to data.
[fitresult{9}, gof(9)] = fit( [xData, yData], zData, ft );
%% Fit: '5x2'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly52' );
% Fit model to data.
[fitresult{10}, gof(10)] = fit( [xData, yData], zData, ft );
%% Fit: '1x3'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly13' );
% Fit model to data.
[fitresult{11}, gof(11)] = fit( [xData, yData], zData, ft );
%% Fit: '2x3'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly23' );
% Fit model to data.
[fitresult{12}, gof(12)] = fit( [xData, yData], zData, ft );
%% Fit: '3x3'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly33' );
% Fit model to data.
[fitresult{13}, gof(13)] = fit( [xData, yData], zData, ft );
%% Fit: '4x3'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly43' );
% Fit model to data.
[fitresult{14}, gof(14)] = fit( [xData, yData], zData, ft );
%% Fit: '5x3'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly53' );
% Fit model to data.
[fitresult{21}, gof(21)] = fit( [xData, yData], zData, ft );
%% Fit: '1x4'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly14' );
% Fit model to data.
[fitresult{22}, gof(22)] = fit( [xData, yData], zData, ft );
%% Fit: '2x4'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly24' );
% Fit model to data.
[fitresult{23}, gof(23)] = fit( [xData, yData], zData, ft );
%% Fit: '3x4'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly34' );
% Fit model to data.
[fitresult{24}, gof(24)] = fit( [xData, yData], zData, ft );
%% Fit: '4x4'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly44' );
% Fit model to data.
[fitresult{25}, gof(25)] = fit( [xData, yData], zData, ft );
%% Fit: '5x4'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly54' );
% Fit model to data.
[fitresult{15}, gof(15)] = fit( [xData, yData], zData, ft );
%% Fit: '1x5'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly15' );
% Fit model to data.
[fitresult{16}, gof(16)] = fit( [xData, yData], zData, ft );
%% Fit: '2x5'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly25' );
% Fit model to data.
[fitresult{17}, gof(17)] = fit( [xData, yData], zData, ft );
%% Fit: '3x5'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly35' );
% Fit model to data.
[fitresult{18}, gof(18)] = fit( [xData, yData], zData, ft );
%% Fit: '4x5'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly45' );
% Fit model to data.
[fitresult{19}, gof(19)] = fit( [xData, yData], zData, ft );
%% Fit: '5x5'.
[xData, yData, zData] = prepareSurfaceData( z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy );
% Set up fittype and options.
ft = fittype( 'poly55' );
% Fit model to data.
[fitresult{20}, gof(20)] = fit( [xData, yData], zData, ft );
2 Comments
ABDALLA AL KHALEDI
on 10 Nov 2023
Edited: ABDALLA AL KHALEDI
on 10 Nov 2023
ABDALLA AL KHALEDI
on 10 Nov 2023
Accepted Answer
More Answers (0)
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!