I am batch processing 1000s of data. Sometime the peak positions and magnitudes change drastically, and the program struggles to find these peaks with a single start point value. I have to divide my data into smaller batches to change the start point values, which is time consuming.
Is it possible to try various start point values and select the one with the best rsquare?
ft = fittype('y0 + a*exp(-((x-xa)/(wa))^2), 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [10 10 10 0]; % this is a, wa, xa and y0 - from the equation
[fitresult, gof] = fit(xData, yData, ft, opts);
alpha = gof.rsquare; % extract goodness of fit
if alpha < 0.98 % if rsquare (goodness of fit) is not good enough
for x = 100:10:500; y= 10:1:50 %these numbers are not set in stone - can be any number
opts.StartPoint = [10+x 10 10+y 0]; % tweak the start point values for the fit
[fitresult, gof] = fit(xData, yData, ft, opts); % fit again
Then select the start point with the best rsquare and plot the results.
% plot
f = figure('Name', 'Gauss','Pointer','crosshair');
h = plot(fitresult, xData, yData, '-o');