parallel computation in matlab
1 回表示 (過去 30 日間)
古いコメントを表示
How can i use parallel computation in this Function ?
function [x,y]=euler_backward(f,xinit,yinit,xfinal,n)
% calculate h
h=(xfinal-xinit)/n;
% Initialize x and y as column vectors
x=[xinit zeros(1,n)];
y=[yinit zeros(1,n)];
% Calculate of x and y
for i=1:n
x(i+1)=x(i)+h;
ynew=y(i)+h*(f(x(i),y(i)));
y(i+1)=y(i)+h*f(x(i+1),ynew);
end
end
4 件のコメント
Matt J
2021 年 6 月 1 日
編集済み: Matt J
2021 年 6 月 1 日
@Walter Roberson I'm not sure I follow. Parallelization of the computations within f() could allow each individual call to f() to go faster. If so, then the total time for the loop should decrease as well.
It might also be worth pointing out that the x(i) can all be pre-computed and the loop reduced as follows
x=linspace(xinit,xfinal,n+1);
h=x(2)-x(1);
for i=1:n
ynew=y(i)+h*(f(x(i),y(i)));
y(i+1)=y(i)+h*f(x(i+1),ynew);
end
Therefore, if for example f() looks something like f(a,b)=p(a)+q(a,b) where p() is an expensive function but q() is simple, then the loop can be accelerated with the following strategy:
x=linspace(xinit,xfinal,n+1);
h=x(2)-x(1);
parfor i=1:n+1
px=p(x(i));
end
for i=1:n
ynew=y(i)+h*( px(i) + q(x(i),y(i)) );
y(i+1)=y(i)+h*( px(i+1) + q(x(i),ynew) );
end
Walter Roberson
2021 年 6 月 1 日
The large majority of the ode functions I see people posting have code that ignore the first parameter (such as "time") and depend only on the second parameter (current boundary conditions). I do see the occasional toy example that ignores the boundary conditions... usually in the context of people being asked to program Euler method.
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!