# How can I change opts.StartPoint for a custom equation at every loop iteration?

6 views (last 30 days)
pinar on 11 Aug 2020
Edited: Matt J on 16 Dec 2021
Hello,
I have generated a code to fit a Gaussian CDF to the data points of each participant. Since I generated the code by using only one participant's data as a sample, opts.StartPoint was based on this one participant's data points. But the fits were not very good when I did it like this.
So what I want to do is to change opts.StartPoint at each iteration so that the fit on that iteration is based on the start points selected for that participant.
Below is how I have written the start points initially.
```
% Set up fittype and options.
ft = fittype( 'a*(.5*(1+erf((x-m)/(s*sqrt(2)))))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.71149061180612 0.231594386708524 0.488897743920167];
opts.MaxFunEvals = 1000;
```
Thank you!

Uday Pradhan on 14 Aug 2020
Hi Pinar,
It is my understanding that you would like to define custom starting points for your curve - fitting model for different participants. To do this, we can create an n by 3 matrix startPts (n being the number of participants) containing the parameters a, m and s in that order (i.e. the i - th row of startPts is the start point for the i - th participant). Then, we can use a loop to create n different fit objects which have different starting points based on the vectors extracted from startPts. I have a code snippet below that you may find useful.
%startPts is the n - by - 3 matrix containing the start points for each of the 'n' participants;
ft = fittype( 'a*(.5*(1+erf((x-m)/(s*sqrt(2)))))', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.MaxFunEvals = 1000;
s = cell(n,1); %store the n fitobjects for plotting
for i = 1:n
opts.StartPoint = startPts(i,:);
s{i} = fit(x,y,ft,opts);
end
Matt J on 16 Dec 2021
Angelavtc's comment moved here:
Dear @Uday Pradhan your answer is very useful, but your code makes the matrix "startPts" a predefined matrix by the user. Is there a way to make this matrix contain the starting point used by the curve fitting tool?