MATLAB: Fiiting curve column by column for given data matrix in MATLAB R2014a

curve fittingMATLAB

I have a 2D matrix dataset. The 2nd dimension (Y-axis) needs to be fit with an expeonetial decay function. And each column should be fit separately. I would like a return of 2D matrix as well with fit results shown in the Y-axis. Any possible solution?

Best Answer

Exponential decay function is expressed as follows:
y = F(x|u) = 1 - exp(x/u)
Assuming that this is the correct form of the function here is the function that will give you the correct fit:
 
function [fitresult, gof] = createFit(x, y)
%CREATEFIT(X,Y)
% Create a fit.
%
% Input:
% X Input : x
% Y Output: y
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( '1-exp(x/u)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = 0.795199901137063;
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
%figure( 'Name', 'Cumulative exponential decay fit' );
%h = plot( fitresult, xData, yData );
%legend( h, 'y vs. x', ''Cumulative exponential decay fit', 'Location', 'NorthEast' );
% Label axes
%xlabel( 'x' );
%ylabel( 'y' );
%grid on
Once you save the above function as M file, you can use it as follows:
>> testData = rand(20,1);
>> [rows,cols] = size(testData);
>> x = transpose(1:rows);
>> coefficients = cell(1,cols);
>> for cid=1:cols
>> [fitresults,gof] = createFit(x,testData(:,1));
>> coefficients(cid) = {fitresults};
>> end
>> coefficients{1}.u % Parameters of fit for first column of testData matrix.
If the form of the function is different then you can substitute the correct one in the function at following line:
ft = fittype( '<place_correct_form_of_the_function_here>', 'independent', 'x', 'dependent', 'y' );
Rest of the code should remain the same and you can access the parameters as illustrated in the last line of the script.
There is another way for fitting custom functions using GUI. In order to accomplish that please follow these steps:
1. Click on APPS.
2. Click on Curve Fitting.
3. Select X data variable from your workspace. This will be the x-variable for the fit.
4. Select Y data variable from your workspace. This will be the y-variable for the fit.
5. Select Custom Equation from the drop down menu above "Method".
6. Input correct form of the function in the text area.
7. Click on "Fit" if "Auto Fit" is unchecked.
8. To save the work as a function, go to "File" and click on "Generate Code". This will create a MATLAB function for you which will have code to fit the custom function using parameters you have mentioned.
9. When you "Fit" the function using GUI you can see values of the parameters and their confidence intervals in the "Results" panel.
10. If you fit the results programmatically using the MATLAB function generated, you will find the parameter values stored in "fitresult" variable.
Note that the function generated takes two argument vectors of same size namely, "x" and "y". In this case, "x" corresponds to row numbers and "y" variable contains individual columns.