Parfor problems about loopVar = initVal:endVal

1 回表示 (過去 30 日間)
Xin Shen
Xin Shen 2019 年 9 月 24 日
回答済み: Xin Shen 2019 年 10 月 4 日
Greetings,
I got an error in my codes with line "parfor m=1:s", which is very weird. I don't know what happens. Can anyone give me some suggestions? Thank you!
Error using DynSys (line 11)
Unable to perform assignment because the indices on the left side are not compatible with the size of
the right side.
Error in eGram (line 37)
parfor m=1:s
Error in testEGram (line 188)
EmGramian = eGram(@DynSys,[50; numel(index)],X0,0) ;
My function is :
function xhat = eGram(Func,ParaVector,X0,flag)
% xhat = obsv_gram_cov_unscaled(OdeFcn,Tspan,ParaVector,OuptputIndex,uss,xss)
% Func: the ode function of the system
% Tspan: [start time, ending time, sampleLength]
% ParaVector: StateNumber n, OutputNumber k,
% Cm: CmValue, and s = size(Cm,1)
% OutputIndex: the indices of outputs corresponding to states
% uss: the value of input at steady state
% xss: steady state
% Orientation Number r
% flag == 0, use initial state as bench (static); ~=0, use Y0 as bench (dynamically)
Tol = 1E-9;
% gramian parameters
n = ParaVector(1);
k = ParaVector(2);
% initialization for T for observability
T(:,:,1) = eye(n,n);
Cm = logspace(log10(0.01),log10(1),5) .';
e = eye(n,n);
xhat = zeros(n,n);
s = length(Cm);
r = size(T,3);
Ts = 0.1;
nu = 600;
[~,~,Y0] = Func(X0);
for l=1:r
parfor m=1:s % ERROR THROWS HERE!!!!!
chsi = zeros(n,n);
z = zeros(n,nu*k);
for i=1:n
% apply perturbed initial condition
initvalue = X0 + Cm(m)*T(:,:,l)*e(:,i);
[t,X,Y] = Func(initvalue);
if flag==0
for iii = 1:k
z(i,nu*(iii-1)+1:nu*iii) = (Y(:,iii) - Y(1,iii))';
end
else
for iii = 1:k
z(i,nu*(iii-1)+1:nu*iii) = (Y(:,iii) - Y0(:,iii))';
end
end
end
chsi = z*z';
xhat = xhat + 1/(r*s*Cm(m)^2)*Ts*T(:,:,l)*chsi*T(:,:,l)';
end
end
  3 件のコメント
Matt J
Matt J 2019 年 10 月 3 日
Do you get any errors if you replace your parfor loop with a regular for loop?
Xin Shen
Xin Shen 2019 年 10 月 3 日
I don't get any error if I use regular for loop

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

採用された回答

Xin Shen
Xin Shen 2019 年 10 月 4 日
I found that "Error using DynSys (line 11)" is caused by global variables. For parallel computation, I cannot use global variables.

その他の回答 (1 件)

meghannmarie
meghannmarie 2019 年 10 月 3 日
編集済み: meghannmarie 2019 年 10 月 3 日
You are setting the variable xhat in the parfor and using the prevous itereation of xhat. You need to just access and set a slice of xhat, you cannot access and change xhat as a whole while other workers in parfor are accessing it. For example:
xhat(m,:) = xhat(m,:) + ...
  4 件のコメント
meghannmarie
meghannmarie 2019 年 10 月 3 日
編集済み: meghannmarie 2019 年 10 月 3 日
Can you give some example inputs for Func, ParaVector, X0? Also what did you set xhat to in last line?
Xin Shen
Xin Shen 2019 年 10 月 3 日
I solved the problem of the Func because I use some global variables in Func.
Currently, the error is what you said about xhat. It seems that I cannot slice output xhat.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by