lsqnonlin UseParallel not working in 2016a

I use MATLAB 2016a and according to the documentation, parallel computing can be exploited in the optimization function lsqnonlin. I'm trying to use lsqnonlin. It works fine if the option 'UseParallel' is false. However, if I set 'UseParallel' to true in options, I get the following errors:
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
> In lsqncommon (line 56) In lsqnonlin (line 240) In Mariner1 (line 54)
Error using Mariner1>shoot_forward (line 132) In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Mariner1>@(x,y)shoot_forward(x,false) (line 54) [sh_sol,rn] = lsqnonlin(@(x,y)shoot_forward(x,false),inp,[],[],options)
Error in finDiffEvalAndChkErr
Error in parfinitedifferences
Error in parfinitedifferences
Error in computeFinDiffGradAndJac
Error in levenbergMarquardt (line 100) [JAC,~,~,numEvals,evalOK] = computeFinDiffGradAndJac(XOUT,funfcn,confcn,costFun, ... Error in lsqncommon (line 174) levenbergMarquardt(funfcn,xC,flags.verbosity,options,defaultopt,initVals.F,initVals.J, ...
Error in lsqnonlin (line 240) lsqncommon(funfcn,xCurrent,lb,ub,options,defaultopt,caller,...
Error in Mariner1 (line 54) [sh_sol,rn] = lsqnonlin(@(x,y)shoot_forward(x,false),inp,[],[],options)

 Accepted Answer

Stuart Rogers
Stuart Rogers on 2 Apr 2016
Edited: Stuart Rogers on 2 Apr 2016
I was able to fix this error by removing global variables from the input function passed in as the first argument of lsqnonlin. Instead of using global variables, I pass the necessary data into lsqnonlin via the function handle.

1 Comment

I was getting similar errors when using fmincon with parallel computing turned on, and like you, was also able to fix the errors by passing previously global variables as extra parameters into the functions. It seems that PARALLEL COMPUTING IN MATLAB ABSOLUTELY REQUIRES THAT THERE ARE NO GLOBAL VARIABLES IN THE OBJECTIVE AND CONSTRAINT FUNCTIONS.

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!