필터 지우기
필터 지우기

Error using fsolve inside a parfor loop

조회 수: 3 (최근 30일)
JONAH TAYLOR
JONAH TAYLOR 2022년 12월 4일
댓글: JONAH TAYLOR 2022년 12월 4일
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.
  댓글 수: 1
JONAH TAYLOR
JONAH TAYLOR 2022년 12월 4일
Thank you so much for the help, this worked!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by