Error using fsolve inside a parfor loop

3 次查看(过去 30 天)
I have a system of 5 non-linear equations I am trying to solve using fsolve. I am trying to solve this system for a bunch of different combinations of initial guesses for each of the 5 variables and then see which one produces the most accurate result. IG is a 1x11^4 cell array, where each entry is a 1x5 vector containing a set of initial guesses. I am trying to execute the following parfor loop:
s = size(IG);
Accuracies = zeros(s);
parfor (i = 1:s(2), 4)
[x,fval] = fsolve(@(x) max_Jemit(x, n0, kTe_eV, Vc), IG{i}, opts);
Accuracies(i) = fval(1)^2 + fval(2)^2 + fval(3)^2 + fval(4)^2 + fval(5)^2;
end
I obtain the following error:
Error using JemitSolver>max_Jemit (line 553)
Matrix dimensions must agree.
Error in parallel.internal.pool.deserialize>@(x)max_Jemit(x,n0,kTe_eV,Vc)
Error in fsolve (line 242)
fuser = feval(funfcn{3},x,varargin{:});
Error in JemitSolver (line 155)
parfor (i = 1:s(2), 4)
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Line 553 that it refers to is inside the function max_Jemit where I define my system of equations:
function F = max_Jemit(x, n0, kTe_eV, Vc)
global qe me epsilon0
kTe_J = kTe_eV*qe;
eta_s = (qe*x(5))/(kTe_J);
eta_0 = (qe*x(2))/(kTe_J);
jc = x(4)*qe*sqrt(kTe_J/me)*(2*eta_s)^(3/2);
nu_i = x(1)/x(4);
Jb = x(3)/jc; This is the line it does not like
lambda_D = sqrt((epsilon0*kTe_J)/(x(4)*qe^2));
Enorm = (qe*lambda_D)/(kTe_J);
F(1) = nu_i - 1 - 2*eta_s*Jb;
F(2) = eta_0 - (1/2)*(nu_i/(1-Jb));
F(3) = (2/Enorm^2)*(2*nu_i*eta_0*(sqrt(1 + eta_s/eta_0)-1) - Jb*(2*eta_s)^2 + exp(-eta_s) - 1);
F(4) = x(4) - n0*exp(-eta_0);
F(5) = Vc - x(5) - x(2);
end
I have no issues when doing this exact code in a normal for-loop. These errors only arise when I use parfor. Am I not able to use cell arrays with parfor loops?

采纳的回答

Walter Roberson
Walter Roberson 2022-12-4
global variables are not copied to workers in parfor.
You will need to use parfevalOnAll to set the global variables to their desired values.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by