我正在尝试for
在 Matlab 中的 parfor 循环内使用循环。
该for
循环等效于此处的 ballode 示例。
在for
循环内部ballBouncing
调用一个函数,它是一个由 6 个微分方程组成的系统。
所以,我想要做的是为 ODE 系统使用 500 组不同的参数值并运行它,但是对于每个参数集,都会添加一个突然的脉冲,这是通过“for”循环中的代码处理的。
但是,我不明白如何使用 aparfor
和一个for
循环来实现这一点,如下所示。
我可以通过使用两个for
循环来运行这段代码,但是当外循环被设置为 a 时,parfor
它会给出错误
the PARFOR loop cannot run due to the way variable results is used
,
the PARFOR loop cannot run due to the way variable y0 is used
和
Valid indices for results are restricted in PARFOR loops
results=NaN(500,100);
x=rand(500,10);
parfor j=1:500
bouncingTimes=[10,50];%at time 10 a sudden impulse is added
refine=2;
tout=0;
yout=y0;%initial conditions of ODE system
paras=x(j,:);%parameter values for the ODE
for i=1:2
tfinal=bouncingTimes(i);
[t,y]=ode45(@(t,y)ballBouncing(t,y,paras),tstart:1:tfinal,y0,options);
nt=length(t);
tout=[tout;t(2:nt)];
yout=[yout;y(2:nt,:)];
y0(1:5)=y(nt,1:5);%updating initial conditions with the impulse
y0(6)=y(nt,6)+paras(j,10);
options = odeset(options,'InitialStep',t(nt)-t(nt-refine),...
'MaxStep',t(nt)-t(1));
tstart =t(nt);
end
numRows=length(yout(:,1));
results(1:numRows,j)=yout(:,1);
end
results;
有人可以帮助我使用parfor
外循环来实现这一点。
将分配固定results
为相对简单 - 您需要做的是确保始终分配一整列。这是我将如何做到的:
% We will always need the full size of results in dimension 1
numRows = size(results, 1);
parfor j = ...
yout = ...; % variable size
yout(end:numRows, :) = NaN; % Expand if necessary
results(:, j) = yout(1:numRows, 1); % Shrink 'yout' if necessary
end
但是,y0
更难处理 -parfor
循环的迭代不是顺序无关的,因为您将信息从一个迭代传递到下一个迭代的方式。parfor
只能处理迭代顺序无关的循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句