Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
speed up for loop

Subject: speed up for loop

From: Nicholas

Date: 12 Oct, 2010 12:52:04

Message: 1 of 2

Hello,
I have a code as below for simulating the brownian motion of a set of 'n' particles with radius dr and avoiding it colliding with a large set of spheres at position where the centre of the spheres are stored in a nX3 matrix xyz and with radius r. The code i am currently using is below but because of the large size of xyz this takes a very long time to compute. Can anyone suggest ways to improve the efficiency of the code.
xrv2=22
tsteps=100000
n=10
xyz=6000 by 3 matrix
dt=25
rr=initial positions of particles
dr=50
r=2000


for i=1:tsteps %till time is up
    t=t+dt; %one time step
    for s=1:n %each particle
        flag=0;
        while flag==0
            flag=1;
            dxr=(xrv2)*randn(1,3); %flight in random direction
            rr(s,:)=rr(s,:)+dxr; %random displacement
            rr(s,:)=rr(s,:)-(L*round(rr(s,:)/L)); %adjusting for boundary conditions
            if norm(rr(s,:))<=(R+dr/2)
                k=dsearchn(xyz,rr(s,:)); %finds nearest sphere in opal to the latex particle
                if ((rr(s,1)-xyz(k,1))^2)+((rr(s,2)-xyz(k,2))^2)+((rr(s,3)-xyz(k,3))^2)<=(r+dr/2).^2 %avoid superposition onto spheres
                    flag=0; %this makes sure that the small particles and spheres do not come into contact
                end
            end
        end
   end
end

Subject: speed up for loop

From: Sean

Date: 12 Oct, 2010 14:52:03

Message: 2 of 2

"Nicholas " <n.ballard@warwick.ac.uk> wrote in message <i91llk$n6m$1@fred.mathworks.com>...
> Hello,
> I have a code as below for simulating the brownian motion of a set of 'n' particles with radius dr and avoiding it colliding with a large set of spheres at position where the centre of the spheres are stored in a nX3 matrix xyz and with radius r. The code i am currently using is below but because of the large size of xyz this takes a very long time to compute. Can anyone suggest ways to improve the efficiency of the code.
> xrv2=22
> tsteps=100000
> n=10
> xyz=6000 by 3 matrix
> dt=25
> rr=initial positions of particles
> dr=50
> r=2000
>
>
> for i=1:tsteps %till time is up
> t=t+dt; %one time step
> for s=1:n %each particle
> flag=0;
> while flag==0
> flag=1;
> dxr=(xrv2)*randn(1,3); %flight in random direction
> rr(s,:)=rr(s,:)+dxr; %random displacement
> rr(s,:)=rr(s,:)-(L*round(rr(s,:)/L)); %adjusting for boundary conditions
> if norm(rr(s,:))<=(R+dr/2)
> k=dsearchn(xyz,rr(s,:)); %finds nearest sphere in opal to the latex particle
> if ((rr(s,1)-xyz(k,1))^2)+((rr(s,2)-xyz(k,2))^2)+((rr(s,3)-xyz(k,3))^2)<=(r+dr/2).^2 %avoid superposition onto spheres
> flag=0; %this makes sure that the small particles and spheres do not come into contact
> end
> end
> end
> end
> end

This might help a little, I can't imagine it's going to be much though:

xrv2=22;
tsteps=100000;
n=10;
xyz= ones(6000,3);% by 3 matrix
dt=25;
rr= ones(6000,3); %initial positions of particles
dr=50;
r=2000;
L=17; %Where did you calculate L?

for ii=1:tsteps %till time is up
    for s=1:n %each particle
        flag=true;
        while flag
            flag=false;
            rr(s,:)=rr(s,:)-(L*round(rr(s,:)/L))+(xrv2)*randn(1,3); %adjusting for boundary conditions
            if norm(rr(s,:))<=(r+dr/2) %changed 'R' to 'r'?
                k=dsearchn(xyz,rr(s,:)); %finds nearest sphere in opal to the latex particle
                flag(sum((rr(s,:)-xyz(k,:)).^2)<=(r+dr/2)^2) = true;
            end
        end
    end
end

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us