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

4 次查看(过去 30 天)
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!

回答(1 个)

Uday Pradhan
Uday Pradhan 2020-8-14
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
  3 个评论
Mirjam Studler
Mirjam Studler 2021-11-24
编辑:Mirjam Studler 2021-11-24
Did you manage to find a solution to generate opts.StartPoint automatically in the code? I actually face the same problem.
Matt J
Matt J 2021-12-16
编辑:Matt J 2021-12-16
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by