Export all fit results from my curve fitting toolbox for multiple fits

12 views (last 30 days)
Hi,
I have a fit session using the cftool - there are multiple fits for different sets of data. Some of the data has 2 or 3 different fits.
My ultimate goal is to compare between the different fits for each set of data point (Say for a particular mesaurment I did 3 different fits and I want to get reduced Chi-squared test in order to chose the best fit).
For that I would want to export all of my fits into the workspace as cfit objects. (I don't need any more data, just the cfit files will suffice).
I can manually use the cftool option and extract every one of them wth Fit>Save to workspace and chose only the checkbox with 'Save fit to MATLAB object named:'
However it seems like a redundent manual work, is there a way to do it automatically.
Here's an example of my fits : each row is a unique data set and for some of them I have fit#1,2 and 3 , these are the different versions.

Answers (1)

TED MOSBY
TED MOSBY on 9 Jun 2024
Hi Pavel,
I referred to the MATLAB documentation for the Curve Fitter Tool but couldn’t find a way to automate the export process for every fit result which is generated.
Have a look at the documentation for the Curve Fitter tool here:
I can suggest an alternative way to analyse the fits exported but for that you need to first export the fits manually to the workspace. Post that you can write a script that automatically analyse the fits and calculate the reduced chi-squared. A dummy example is shown below:
% Assuming fitresult_lin and fitresult_quad are in the workspace
% And assuming x, y1, and y2 are your data vectors
% Number of parameters for each model:
% For a linear fit (ax + b), m_lin = 2
% For a quadratic fit (ax^2 + bx + c), m_quad = 3
m_lin = 2;
m_quad = 3;
% Calculate residuals for each fit
residuals_lin = y1 - feval(fitresult_lin, x);
residuals_quad = y2 - feval(fitresult_quad, x);
% Assuming equal variance (sigma = 1) for simplicity
sigma = 1;
% Calculate reduced Chi-squared for each fit
chi_squared_red_lin = sum((residuals_lin / sigma).^2) / (length(y1) - m_lin);
chi_squared_red_quad = sum((residuals_quad / sigma).^2) / (length(y2) - m_quad);
% Display the reduced Chi-squared values
fprintf('Reduced Chi-squared (Linear Fit): %f\n', chi_squared_red_lin);
fprintf('Reduced Chi-squared (Quadratic Fit): %f\n', chi_squared_red_quad);
% Comparing the reduced Chi-squared values
if chi_squared_red_lin < chi_squared_red_quad
fprintf('The linear fit is better according to the reduced Chi-squared criterion.\n');
else
fprintf('The quadratic fit is better according to the reduced Chi-squared criterion.\n');
end
As this is just a dummy example, feel free to adjust it according to your data and fits.
Hope this helps!

Categories

Find more on Interpolation 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!