Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulation on MATLAB

Tags:

plot

matlab

enter image description here

I have to make a simulation of ants moving between their home (black box) and food (yellow box). These triple colored boxes are ants. The code that I wrote to draw the figure shown is following :

 % background
 background()   

 % making ants 
 handle = zeros(10,3) 
 handle = makingAnts(10) ;

 % moving ants 
 movingAnts(hand)

Function background :

 function background()

figure
hold on 
axis equal
axis([0 100 0 100])
pos = rand(1,2).*75
rectangle('position',[0 0 10 10],'facecolor','k')
rectangle('position',[pos 25 25],'facecolor','y')
 end

function making ants :

  function [h] = makingAnts(n)
  h = zeros(10,3)
  dia = [2 2]
   for i = 1:n
  pos = rand(1,2).* 95 ; 
  h(i,1) = rectangle('position',[pos dia],'facecolor',[0.2 0.6 1])
  g1 = get(h(i,1),'position')
  h(i,2) = rectangle('position',[(g1(1)+2) (g1(2)+2) 2 2],'facecolor',[0.4 1 0.6])   
  h(i,3) = rectangle('position',[(g1(1)+4) (g1(2)+4) 2 2],'facecolor',[1 0.8 1])
  end
   end

Now I have to move the ants. Though I have written the code but it is not working. I need help in making the ants move.

The code that I wrote :

 function  movingAnts(h)
 % moving 1 ant 
 pos = get(h(1),'position') 
 m = pos(1)
 n = pos(2) 
  for i = 1:50 
 set(h(1),'position',[(m+0.2) (n+0.2) 2 2])
 pause(0.05) 
 end
   end
like image 806
Manahil Avatar asked Nov 15 '14 13:11

Manahil


People also ask

Can MATLAB be used for simulation?

Model and simulate dynamic system behavior with MATLAB, Simulink, Stateflow, and Simscape. Modeling is a way to create a virtual representation of a real-world system that includes software and hardware.

What is meant by simulation in MATLAB?

Simulation software helps you predict the behavior of a system. You can use simulation software to evaluate a new design, diagnose problems with an existing design, and test a system under conditions that are hard to reproduce, such as a satellite in outer space.

How do you run a simulation in MATLAB?

MATLAB Command to Run SimulationCreate a MATLAB engine object and start a MATLAB session. Load the Simulink model in MATLAB ( load_system (Simulink)). Run the simulation with specific simulation parameters ( sim (Simulink)). Access the results of the simulation using methods of the returned Simulink.

What is the objective of the simulation in MATLAB?

The main goal of model preparation is to ensure that your model is real-time capable. Your model is real-time capable if it is both: Accurate enough to generate simulation results that match your expectations, as based on theoretical models and empirical data.


3 Answers

As @franz1 pointed out the problem is that m and n variables are outside the loop and therefore not updating. To extend on his/her answer, here is a possible movingAnts.m function:

function  movingAnts(h)
    h = reshape(h,numel(h),[]);
    for i = 1:50
        pos = get(h,'position');
        % move all ants
        pos = cellfun(@(x) x+[1,1,0,0], pos, 'UniformOutput', false);
        set(h,{'position'},pos);        
        drawnow update %display updates
        pause(0.1);
    end
end

enter image description here

Additionally you should change the definition of h in makingAnts.m to:

h = zeros(n,3);
like image 93
rozsasarpi Avatar answered Oct 19 '22 00:10

rozsasarpi


figure
axis([0 100 0 100])
rectangle('position',[0 0 5 5],'facecolor','k')
rectangle('position',[95 95 100 100],'facecolor','y')
n = 5;
x = zeros(1,n);
y = zeros(1,n);
c = ones(1,n);
while 1
  for i = 1 : n
      h1(i) = rectangle('position',[x(i) y(i) 2 2],'facecolor',[0.2 0.6 1]);
      g1 = get(h1(i),'position');
      h2(i) = rectangle('position',[(g1(1)+2) (g1(2)+2) 2 2],'facecolor',[0.4 1 0.6]);
      h3(i) = rectangle('position',[(g1(1)+4) (g1(2)+4) 2 2],'facecolor',[1 0.8 1]);
      x(i) = x(i) + c(i) * randi(4,1);
      y(i) = y(i) + c(i) * randi(4,1);
      if x(i) > 100 || y(i) > 100
          c(i) = -1 * c(i);
          x(i) = 100; y(i) = 100;
      end
      if y(i) < 0 || x(i) < 0
          c(i) = -1 * c(i);
          x(i) = 0; y(i) = 0;
      end
  end
  pause(.1)
  delete(h1,h2,h3);
end

enter image description here

like image 42
Rashid Avatar answered Oct 18 '22 22:10

Rashid


for i = 1:50 
    set(h(1),'position',[(m+0.2) (n+0.2) 2 2])
    pause(0.05) 
end

Since m, n are constants, here you are placing the ant to the same position in each iteration.

like image 2
robert Avatar answered Oct 19 '22 00:10

robert