How to adapt my for loop to get it working with parfor? (ODE solver)
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Edric Ellis
on 6 Mar 2015
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.
More Answers (1)
Jan
on 6 Mar 2015
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?
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!