How to adapt my for loop to get it working with parfor? (ODE solver)
1 回表示 (過去 30 日間)
古いコメントを表示
Hello,
Ive come a long way with programming as a newbie in Matlab, but now Im really stuck. Ive tried to 'slice' the variable dy, but unfortunately it didnt work. I guess the problem lies in the fact that i is dependent on SACn. Ive done this because the ODE solver needs a vector as input, not a matrix. Can someone please help me?
This is a snippet of code:
parfor SACn=0:SACnum-1
i=19+SACn*200:1:66+SACn*200;
dy(i-18+100,:) = alpha.*(1-y(i-18+100,:)).*(1./(1+exp(-((y(i,:) -th1)./k1))))-beta.*y(i-18+100,:);
dy(i-18+150,:) = alpha.*(1-y(i-18+150,:)).*(1./(1+exp(-((y(i-18+100,:)-th2)./k2))))-beta.*y(i-18+150,:);
end
I tried something like this (didnt work though):
parfor SACn=0:SACnum-1
temp1dy=zeros(1,200);
temp2dy=zeros(1,200);
for i=19:1:66
temp1dy(i-18+100,:) = alpha.*(1-y(i-18+100,:)).*(1./(1+exp(-((y(i,:) -th1)./k1))))-beta.*y(i-18+100,:);
temp2dy(i-18+150,:) = alpha.*(1-y(i-18+150,:)).*(1./(1+exp(-((y(i-18+100,:)-th2)./k2))))-beta.*y(i-18+150,:);
end
dy(i-18+100+SACn*200,:)=temp1dy;
dy(i-18+150+SACn*200,:)=temp2dy;
end
Additional info: I have vectorized my code in an ODE solver of Matlab, these are all the :) 's you see at the end of all variables such as y(i+18-100,:). Ive done this so that the ODE solver computes multiple values of dy for the same time point in parallel and then chooses the best option.
Please do not hesitate to ask for more details!
0 件のコメント
採用された回答
Edric Ellis
2015 年 3 月 6 日
For dy to be successfully "sliced", you need to index it in precisely the correct form - where one of the subscripts is the loop variable SACn, and the other subscripts follow the other rules described here in the doc.
I think what you need to do is to make dy have an extra dimension - currently you're trying to assign into all the columns, and a range of rows. I would try making dy three-dimensional, and then you should be able to assign into it in the following form:
dy(:, :, SACn) = <...>;
You'll need to unpick this different form outside the parfor loop.
その他の回答 (1 件)
Jan
2015 年 3 月 6 日
I've done this so that the ODE solver computes multiple values of dy for the same time point in parallel and then chooses the best option.
This sounds like you use the ODE solver for non-smooth values. This collides with the specifications of the step-size control. Are you using a fixed-step solver?
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!