Vectorizing/Profiling Code with double for loops etc
显示 更早的评论
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 个)
Jan
2014-5-4
A general hint: SQRT() is expensive, so try to avoid it:
sqrt( (ii ).^2 +(jj - j./5).^2) < 25
Faster:
ii.^2 + (jj - j ./ 5).^2 < 625
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!