Effacer les filtres
Effacer les filtres

parallel computation in matlab

2 vues (au cours des 30 derniers jours)
Ahmed Amin
Ahmed Amin le 1 Juin 2021
Commenté : Walter Roberson le 1 Juin 2021
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
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
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.

Connectez-vous pour commenter.

Réponses (0)

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!

Translated by