parallel computation in matlab
1 view (last 30 days)
Show older comments
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 Comments
Matt J
on 1 Jun 2021
Edited: Matt J
on 1 Jun 2021
@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
on 1 Jun 2021
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.
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!