how to use fsolve in a parfor loop to reach a correct answer?

4 次查看(过去 30 天)
Hi.
I have problem with parfor loop and fsolve function. when I use fsolve in a for loop it take times a lot ( at last answer correctly ), BUT when I use parfor ( and sliced variable to gather output ) its answer is incorrect. how can I use parfor and get correct answer.
part of the code:
with for :
K0=zeros(1,1,size(X1,3));
for k1=1:size(X1,3)
K0(1,1,k1)=fsolve(@(krho)det(FunChi(krho,Xrho,Xphi,N,X1,X_1,k1)),Bsw0,options);
end
with parfor :
K0=zeros(1,1,size(X1,3));
parfor k1=1:size(X1,3)
K0(1,1,k1)=fsolve(@(krho)det(FunChi(krho,Xrho,Xphi,N,X1,X_1,k1)),Bsw0,options);
end
  1 个评论
S.A abtahi
S.A abtahi 2021-10-31
it's very unaccustomed to me when I use
options = optimoptions('fsolve','Display','off','UseParallel',true);
( with parpool from the begining) in simple for loop
K0=zeros(1,1,size(X1,3));
for k1=1:size(X1,3)
K0(1,1,k1)=fsolve(@(krho)det(FunChi(krho,Xrho,Xphi,N,X1,X_1,k1)),Bsw0,options);
end
the answers become incorrect. What is happening?

请先登录,再进行评论。

回答(1 个)

Raymond Norris
Raymond Norris 2021-10-30
It shouldn't matter, but what is UseParallel option set to? You're using parfor and then is fsolve also using parfor (which resolves to running the "inner" parfor as a for-loop in reverse, negating the need to set UseParallel).
If options has UseParallel set to true, set it to false and then run the parfor again.
  1 个评论
S.A abtahi
S.A abtahi 2021-10-31
hi
thanks but it doesn't help
I had changed my options but the answers are incorrect again.
I used the options below:
options = optimoptions('fsolve','Display','off','UseParallel',false);

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by