multistart with matlab function
8 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
i am using lsqcurvefit for fitting some data i have. The code i wrote is the following:
[x, resnorm, residual, exitflag, output] = lsqcurvefit(@(x,xdata)myfun(x,xdata,E_x,Gamma_X,Gamma_C), x0, xdata, ydata, lb, ub, options);
with given x0, E_x, Gamma_X, Gamma_C, lb and ub. The function myfun is in an another file called myfun.m. The fit works fine but I noticed that the result depends on the choice of the initial guess x0. So i tried to implement a multistart. The code i tried to write based on the examples on the help page is the following:
problem = createOptimProblem('lsqcurvefit','x0',x0,'objective',@myfun,'xdata',xdata,'ydata',ydata);
ms = MultiStart('PlotFcns',@gsplotbestf);
[xmulti,errormulti] = run(ms,problem,50)
This code does not work, it gives me the following error:
Error in myfun (line 9)
F(1,:) = real((E_c + E_x - j*(Gamma_X - Gamma_C))/2 + 1/2*sqrt((E_c - E_x - j*(Gamma_X - Gamma_C)).^2 + 4*par(2).^2));
Error in lsqcurvefit (line 202)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in fmultistart
Error in MultiStart/run (line 260)
fmultistart(problem,startPointSets,msoptions);
Error in CHO_matlab (line 91)
[xmulti,errormulti] = run(ms,problem,50)
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
Failure in call to the local solver with supplied problem structure.
My understanding is that the function myfun within problem is missing the inputs parameters. Maybe there is also some other mistake which i do not know. So i replaced @myfun with @myfun(x,xdata,E_x,Gamma_X,Gamma_C) but now i get this error:
Unbalanced or unexpected parenthesis or bracket.
I also tried other similar combinations but noone seems to be acceptable.
What can i do it to make it work? How should i call the function with all the needed parameters?
Thank you all
0 个评论
采纳的回答
Alan Weiss
2020-7-9
You almost had it right. Your objective function is
@(x,xdata)myfun(x,xdata,E_x,Gamma_X,Gamma_C)
where the arguments E_x,Gamma_X,Gamma_C must already be in your workspace when you call createOptimProblem.
Alan Weiss
MATLAB mathematical toolbox documentation
8 个评论
Walter Roberson
2020-7-13
When you pass in an integer number of starting points like you do, then Yes, the starting points will be randomized except for x0
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!