Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ANSWER WITH MATLAB CODE Consider an experiment where ants are placed on a rectan

ID: 3881236 • Letter: A

Question

ANSWER WITH MATLAB CODE

Consider an experiment where ants are placed on a rectangular board of height R inches and width C inches and are allowed to move on the board. Let us represent the number of ants on one inch-square segments of the board by the matrix m. The figure below shows an example of a 3-by-4 board. After one minute, for any given segment, 1/4th of the ants on that segment move up, 1/4th move down, 1/4th move left, and 1/4th move right. For the ants on the segments along the edges of the board, some of the ants move off the board.



Write a function diffuseants2d(m) that takes a matrix m and returns the number of ants on the segments of the board after one minute. For simplicity, assume that the number of ants in a segment does not have to be a whole integer.

Explanation / Answer

%Function simulateants2dallway()
function ants = simulateants2dallway( m,T )
  
   %Initialize the variables
   counter = 0;
   matrixRow = size(m,1);
   matrixCol = size(m,2);
  
   %Loop
   while(counter<T)
      
       %Initialize
       f = zeros(size(m,1), size(m,2));
       index1=1;
      
       %Loop
       while(index1<=matrixRow)
          
           %Initialize
           index2=1;
          
           %Loop
           while(index2<=matrixCol)
              
               %Initialize
               iter = m(index1,index2)/9;
              
               %Check condition
               if m(index1,index2) == 0
                  
                   %Update
                   index2 = index2 + 1;
                   continue;
              
               %End condition check
               end
              
               %Update
               f(index1,index2) = iter;

               %Check condition - left
               if index2 > 1
                  
                   %Update
                   f(index1,index2-1) = f(index1,index2-1) + iter;
              
               %End condition check
               end

               %Check condition - right
               if index2 < matrixCol
                  
                   %Update
                   f(index1,index2+1) = f(index1,index2+1) + iter;
              
               %End condition check
               end

               %Check condition - top
               if index1 > 1
                  
                   %Update
                   f(index1-1,index2) = f(index1-1,index2) + iter;
              
               %End condition check
               end

               %Check condition - bottom
               if index1 < matrixRow
                  
                   %Update
                   f(index1+1,index2) = f(index1+1,index2) + iter;
              
               %End condition check
               end

               %Check condition - top left
               if index1 > 1 && index2 > 1
                  
                   %Update
                   f(index1-1,index2-1) = f(index1-1,index2-1) + iter;
              
               %End condition check
               end

               %Check condition - top right
               if index1 > 1 && index2 < matrixCol
                  
                   %Update
                   f(index1-1,index2+1) = f(index1-1,index2+1) + iter;
              
               %End condition check
               end

               %Check condition - bottom left
               if index1 < matrixRow && index2 > 1
                  
                   %Update
                   f(index1+1,index2-1) = f(index1+1,index2-1) + iter;
              
               %End condition check
               end

               %Check condition - bottom right
               if index1 < matrixRow && index2 < matrixCol
                  
                   %Update
                   f(index1+1,index2+1) = f(index1+1,index2+1) + iter;
              
               %End condition check
               end
              
               %Update
               index2 = index2 + 1;
          
           %End loop
           end
          
           %Update
           index1 = index1 + 1;
      
       %End loop
       end
      
       %Update
       counter = counter+1;
       m = f;
  
   %End loop
   end
  
   %Function call
   ants = antsCounter(m);

%End function
end

%Function antsCounter()
function a = antsCounter(m)
  
   %Initialize
   index1=1;
   a = 0;
  
   %Loop
   while(index1<=size(m,1))
      
       %Initialize
       index2 = 1;
      
       %Loop
       while(index2<=size(m,2))
          
           %Update
           a = a + m(index1,index2);
           index2= index2+1;
      
       %End loop
       end
      
       %Update
       index1 = index1+1;
  
   %End loop
   end

%End function  
end

%Function call
simulateants2dallway([8 0 0 4; 0 0 10 0; 0 0 0 0],2)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote