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

4 visualizzazioni (ultimi 30 giorni)
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 Commento
S.A abtahi
S.A abtahi il 31 Ott 2021
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?

Accedi per commentare.

Risposte (1)

Raymond Norris
Raymond Norris il 30 Ott 2021
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 Commento
S.A abtahi
S.A abtahi il 31 Ott 2021
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);

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by