parallel computation in matlab
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Matt J
le 1 Juin 2021
Modifié(e) : Matt J
le 1 Juin 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
le 1 Juin 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.
Réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!