Vectorizing/Profiling Code with double for loops etc
Afficher commentaires plus anciens
Hi,
Need help increasing the efficiency of the code below (getting rid of double for-loops etc.) I saw some topics relating to bsxfun, but im not too sure if thats applicable here. I appreciate all help in advance. Thank you.
EDIT: Tried changing some other things to vectorize this, but could not make it happen. END Edit
Here's the code:
if true
% code
end
clear;clc;close all
tic
cylgrid = ones(201,502);
init = 100:-1:-100;
i=1:201;
for j=1:502
cylgrid(:,j)=init;
end
%initialize circle
j=500;
for ii = -100:100 %fluctuate i
for jj= 1:502 %fluctuate j
if sqrt( (ii ).^2 +(jj - j./5).^2) < 25
cylgrid(ii+101,jj) = 0;%psi = 0 at surface and inside cylinder
end
end
end
psi = cylgrid;%just for notation
psinew= cylgrid;
error = 0.001;
t=0;dt=1;
while error < max(max(psinew-psi)) || t==0
psi=psinew;
for ii = 2:200
for jj = 2:500
psinew(ii,jj) = psi(ii,jj) +(1/4).*(psi(ii-1,jj)+psi(ii+1,jj)+psi(ii,jj-1)+psi(ii,jj+1)-4.*psi(ii,jj));
end
end
%making sure we begin with our circle again...
for ii = -100:100 %fluctuate i
for jj= 1:502 %fluctuate j
if sqrt( (ii ).^2 +(jj - j./5).^2) < 25
psinew(ii+101,jj) = 0;%psi = 0 at surface and inside cylinder
end
end
end
t=t+dt;
end
uprofile= abs(psinew);
for iii = 100:-1:1
for jjj=1:502
uprofile(101-iii,jjj) = uprofile(101-iii,jjj)./iii;
uprofile(iii+101,jjj) = uprofile(iii+101,jjj)./iii;
end
end
figure(1); title('Streamline Plot');
contourf(psinew,100);set(gca,'DataAspectRatio',[1 1 1]);
figure(2);title('Velocity Profile');
contourf(uprofile,10);set(gca,'DataAspectRatio',[1 1 1]);
toc
1 commentaire
E
le 3 Mai 2014
Réponse acceptée
Plus de réponses (1)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!