Effacer les filtres
Effacer les filtres

Generate Code from Curve Fitting App

22 vues (au cours des 30 derniers jours)
ABDALLA AL KHALEDI
ABDALLA AL KHALEDI le 10 Nov 2023
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 commentaires
ABDALLA AL KHALEDI
ABDALLA AL KHALEDI le 10 Nov 2023
Modifié(e) : ABDALLA AL KHALEDI le 10 Nov 2023
I am getting a bunch of the following warning, I don't want this warning to be shown but I want it to be recorded (at which fit did it happen)
Warning: Equation is badly conditioned. Remove repeated data points or try centering and scaling.
> In curvefit.attention/Warning/throw (line 30)
In fit>iLinearFit (line 680)
In fit>iFit (line 391)
In fit (line 116)
In createFits (line 109)
ABDALLA AL KHALEDI
ABDALLA AL KHALEDI le 10 Nov 2023
I added the following section to the function to obtain a table of the results of intrest
%% % Initialize a table to store the results
resultTable = table('Size', [25, 3], 'VariableTypes', {'double', 'double', 'double'}, 'VariableNames', {'Degree_in_Z', 'Degree_in_X', 'R_Square'});
% Loop through the fits
for i = 1:25
[xData, yData, zData] = prepareSurfaceData(z_Left_Bottom, x_Left_Bottom, Eb_Left_Bottom_strip_array_energy);
% Set up fittype and options
degreeX = floor((i-1)/5) + 1;
degreeY = mod(i-1, 5) + 1;
% Fit model to data
ft = fittype(['poly', num2str(degreeX), num2str(degreeY)]);
[fitresult{i}, gof(i)] = fit([xData, yData], zData, ft);
% Store results in the table
resultTable.Degree_in_Z(i) = degreeX;
resultTable.Degree_in_X(i) = degreeY;
resultTable.R_Square(i) = gof(i).rsquare;
end
% Display the table
disp(resultTable);
and now I am trying to prevent the center and scale warning from showing but I need to know where it happens, I tried the following but with no luck so far.
warning ('off','curvefit:fit:equationBadlyConditioned')
[fitresult, gof] = createFits(z_Right_Bottom, x_Right_Bottom, Eb_Right_Bottom_strip_array_energy);
warning ('on','curvefit:fit:equationBadlyConditioned')
warning('query','curvefit:fit:equationBadlyConditioned')
stack = dbstack;
disp(dbstack)

Connectez-vous pour commenter.

Réponse acceptée

Drew
Drew le 17 Nov 2023
I copy-pasted your last two comments as a query to the MATLAB AI Chatbot https://www.mathworks.com/matlabcentral/playground/new (see announcement at https://blogs.mathworks.com/community/2023/11/07/the-matlab-ai-chat-playground-has-launched/) and the AI gave this answer in image form (see further below for copy-able code), and note that the AI suggests some possible follow-up questions to ask.
Here is the code snippet provided by the AI:
warning('off', 'curvefit:fit:equationBadlyConditioned')
[fitresult, gof] = createFits(z_Right_Bottom, x_Right_Bottom, Eb_Right_Bottom_strip_array_energy);
[~, lastWarning] = lastwarn();
warning('on', 'curvefit:fit:equationBadlyConditioned')
Here is the doc link for the lastwarn function:
If this answer helps you, please remember to accept the answer, and keep in mind to try the MATLAB AI Chat Playground (https://www.mathworks.com/matlabcentral/playground/new) for your MATLAB questions in the future.
  1 commentaire
ABDALLA AL KHALEDI
ABDALLA AL KHALEDI le 20 Nov 2023
Thank you for the extensive explanation!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by