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
endclear;clc;close allticcylgrid = ones(201,502);init = 100:-1:-100; i=1:201;for j=1:502cylgrid(:,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 endendpsi = 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)); endend%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 endendt=t+dt;enduprofile= 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; endend
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
Best Answer