MATLAB:提高逻辑语句中的代码效率(Metropolis方法)

时间:2014-04-19 03:51:10

标签: algorithm matlab optimization logic logical-operators

我有一个代码,用于模拟盒子中理想气体的运动。它基于蒙特卡罗模拟中的大都市方法。但是,我使用一系列逻辑语句(主要是ifs)来定义相邻粒子到随机选择的粒子时的边界条件。该算法表示1x4矩阵内的任何相邻粒子为1,而没有粒子的相邻点为0。我需要算法自动将框外的任何相邻点设置为0,以适应框边缘上的任何粒子。有没有办法减少这些逻辑语句并仍然得到相同的结果?代码如下。

% define constants
beta = .01; %Inverse temperature
N=2000; %Duration of simulation
eps = -7;
mu=9;


for j=1:N
    a=randi(L);
    b=randi(L);
    c=randi(L);
    d=randi(L);
    % Calculate energy at positions
    if lattice(a,b)==1 && lattice(c,d)==0
        %If distribution (according to energy) suggests move,
              lattice(a,b)=0;
            lattice(c,d)=1;

         %Energy at random site/position
       E = n*(mu-eps)
            if (a~=1&& a~=L &&b~=1 && b~=L)
               adjacent= [lattice(a-1,b) lattice(a+1,b) lattice(a,b+1) lattice(a,b-1) 1];
            else if (a==1 && b==1)
                    adjacent= [0 lattice(a+1,b) lattice(a,b+1) 0 1];
                else if (a==L && b==L)
                        adjacent= [lattice(a-1,b) 0 0 lattice(a,b-1) 1];
                    else if(a==1&&b==L)
                            adjacent= [0 lattice(a+1,b) 0 lattice(a,b-1) 1];
                        else if (a==L &&b==1)
                                adjacent= [lattice(a-1,b) 0 lattice(a,b+1) 0 1];
                            else if (a==1 && b~=L && b~=1)
                                    adjacent= [0 lattice(a+1,b) lattice(a,b+1) lattice(a,b-1) 1];
                                 else if (a==L && b~=L && b~=1)
                                        adjacent= [lattice(a-1,b) 0 lattice(a,b+1) lattice(a,b-1) 1];
                                     else if (b==1&&a~=L&&a~=1)
                                             adjacent= [lattice(a-1,b) lattice(a+1,b) lattice(a,b+1) 0 1];
                                         else if (b==L&&a~=L&&a~=1)
                                              adjacent= [lattice(a-1,b) lattice(a+1,b) 0 lattice(a,b-1) 1];  
                                             end
                                         end
                                     end
                                end
                            end
                        end
                    end
                end
            end


            E1 = mu*sum(adjacent) + eps*sum(sum(adjacent.*adjacent)); 
            %This calculates the energy of the particle at its current
            %position

            if (c~=1&& c~=L &&d~=1 && d~=L)
               adjacent1= [lattice(c-1,d) lattice(c+1,d) lattice(c,d+1) lattice(c,d-1) 1];
            else if (c==1 && d==1)
                    adjacent1= [0 lattice(c+1,d) lattice(c,d+1) 0 1];
                else if (c==L && d==L)
                        adjacent1= [lattice(c-1,d) 0 0 lattice(c,d-1) 1];
                    else if(c==1&&d==L)
                            adjacent1= [0 lattice(c+1,d) 0 lattice(c,d-1) 1];
                        else if (c==L &&d==1)
                                adjacent1= [lattice(c-1,d) 0 lattice(c,d+1) 0 1];
                            else if (c==1 && d~=L && d~=1)
                                    adjacent1= [0 lattice(c+1,d) lattice(c,d+1) lattice(c,d-1) 1];
                                 else if (c==L && d~=L && d~=1)
                                        adjacent1= [lattice(c-1,d) 0 lattice(c,d+1) lattice(c,d-1) 1];
                                     else if (d==1&&c~=L&&c~=1)
                                             adjacent1= [lattice(c-1,d) lattice(c+1,d) lattice(c,d+1) 0 1];
                                         else if (d==L&&c~=L&&c~=1)
                                              adjacent1= [lattice(c-1,d) lattice(c+1,d) 0 lattice(c,d-1) 1];  
                                             end
                                         end
                                     end
                                end
                            end
                        end
                    end
                end
            end


            E2 = mu*sum(adjacent) + eps*sum(sum(adjacent1.*adjacent1)); 
            %Calculates the energy at randomly chosen position.

            dE = E2-E1; %Change in energy of the particle as it goes from the two locations.

            if rand<exp(-beta*dE)
                lattice(a,b)=0;
                lattice(c,d)=1;
            end



    end
 time(:,:,j)=lattice;


end

1 个答案:

答案 0 :(得分:2)

您可以取消所有这些if else语句。实际上,您只评估四个逻辑表达式并根据它选择索引。您可以使用以下

ab = [a-1, b; a+1, b; a, b+1; a, b-1];
abIn = [a ~= 1; a ~= L; b ~= L; b ~= 1];
adjacent = zeros(1, 5);
adjacent(abIn) = lattice(sub2ind([L, L], ab(abIn, 1), ab(abIn, 2)));
adjacent(5) = 1;

代替第一组if else语句,并对第二组进行名称更改。这里的想法是只获取它们在边界内的lattice值。这四个逻辑表达式在第四行用于logical indexing