parallel computation in matlab

2 次查看(过去 30 天)
Ahmed Amin
Ahmed Amin 2021-6-1
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
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
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by