multistart with matlab function

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

 Accepted Answer

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 Comments

Thank you very much, it worked very fine. i was sure to have tried that one. Another related question: is there a way to output the starting points used by multistart? I am just putting some lower and upper bound, but i leave the algorithm to choose the points.
Use the syntax
[x,fval,exitflag,output,solutions] = run(ms,problem,k)
The solutions output is a vector of GlobalOptimSolution objects. Those objects have an X0 property. See Visualize the Basins of Attraction for an example using this property.
Alan Weiss
MATLAB mathematical toolbox documentation
thank you again, i managed to make it work for my case. In the same file you suggested there is also the possibility of visualizing the basin by contour plot which is interesting. The problem is that my function depends on 3 variables, while typically the countour is applied to functions which depends on 2 variables. I was thinking to fix the third variable but i did not find the way to do it properly. shall i post this question in an another post with more details or can we discuss it here?
Please ask the detailed question in a new post. Meanwhile, you might get some inspiration from Plot 3-D Solutions and Their Gradients, particularly Contour Slices Through 3-D Solution, which is in the PDE Toolbox documentation, and I think is somewhat relevant to what you are thinking about. I am not sure that it is directly relevant, it is mainly a way to think about this kind of plot.
Alan Weiss
MATLAB mathematical toolbox documentation
Sure i will do that thank you. Meanwhile, is there a way to make multistart generate a different set of initial points within the same bounds each time i run it? This will help me in sampling better the volume and the basins. I could increase the parameter k within run but it will take much more time to complete the calculation. While running several times the entire code it is something that i need to do anyway. Maybe randomizing the initial guess point?
Ok thanks, so multistart already generates a set of different starting points each time it runs. Correct?
When you pass in an integer number of starting points like you do, then Yes, the starting points will be randomized except for x0

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!