0

I have a for loop in a curve fitting problem that needs to be parallelized using the parfor loop. Below is the for loop that works in single core, is there a way I can convert this to parfor?

for initial_p = [1:1:1000]
    % initial condition for fitting
    x0 = [50 5 10 50 5];
    % run the solver
    options = optimset('MaxFunEvals',10000,'MaxIter',10000,'Display','iter');
    [x,fval,exitflag,output] = fminsearch(@(x) Obj(x,initial_p,c1,c2,c3,c4,c5,c6,t,y),x0,options);
    % exit for loop
    if x(1) < 100
        break
    end
end
ShadowWarrior
  • 180
  • 1
  • 12
  • 2
    Note that you cannot convert your `for` loop to be parallel, since there is a serial condition on `x` to break out of the loop. Making `fminsearch` run in parralel is a duplicate of [parallel computing toolbox fminsearch](https://stackoverflow.com/questions/14484269/parallel-computing-toolbox-fminsearch). Your range of `initial_p` is enormous, I feel like the root of your problem is not understanding the behaviour of the optimisation problem enough before throwing `fminsearch` at it... – Wolfie Oct 23 '18 at 10:58
  • It can't run in parallel under these conditions. Perhaps think about lowering your tolerances on your search range as well as your options in fminsearch. Those things will make it run much faster. Alternatively, what I've done in the past is vectorize fminsearch to solve thousands of problems at once which gives a significant speedup. Overall though, with the number of iterations you wish to do, theres almost nothing that can speed up this process enough for your purposes. Consider optimizing for initial_p as well. – Durkee Oct 23 '18 at 11:21
  • I have updated the `initial_p` range, based on educated guess the range can be narrowed down. – ShadowWarrior Oct 23 '18 at 11:27

0 Answers0