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

6 ビュー (過去 30 日間)
S.A abtahi 2021 年 10 月 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 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 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 2021 年 10 月 31 日
hi
thanks but it doesn't help
I used the options below:
options = optimoptions('fsolve','Display','off','UseParallel',false);

サインインしてコメントする。

### カテゴリ

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by