MATLAB仿真
我必须模拟蚂蚁在它们的家(黑盒子)和食物(黄盒子)之间移动。这些三色盒子是蚂蚁。我为绘制图而编写的代码如下所示:MATLAB仿真,matlab,plot,Matlab,Plot,我必须模拟蚂蚁在它们的家(黑盒子)和食物(黄盒子)之间移动。这些三色盒子是蚂蚁。我为绘制图而编写的代码如下所示: % background background() % making ants handle = zeros(10,3) handle = makingAnts(10) ; % moving ants movingAnts(hand) 功能背景: function background() figure hold on axis equal ax
% background
background()
% making ants
handle = zeros(10,3)
handle = makingAnts(10) ;
% moving ants
movingAnts(hand)
功能背景:
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 [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
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
功能制造蚂蚁:
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 [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
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
现在我必须移动蚂蚁。虽然我已经写了代码,但它不工作。我需要帮助让蚂蚁移动
我编写的代码:
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 [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
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
由于m,n
是常数,因此在这里,您将在每次迭代中将蚂蚁放置在相同的位置。图
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
轴([0 100 0 100])
矩形('position',[0 0 5 5],'facecolor','k')
矩形('位置',[95 95 100],'facecolor','y')
n=5;
x=零(1,n);
y=零(1,n);
c=一(1,n);
而1
对于i=1:n
h1(i)=矩形('position',[x(i)y(i)22],'facecolor',[0.20.61]);
g1=get(h1(i),“位置”);
h2(i)=矩形('position',[(g1(1)+2)(g1(2)+2)2],'facecolor',[0.410.6]);
h3(i)=矩形('position',[(g1(1)+4)(g1(2)+4)22],'facecolor',[10.81]);
x(i)=x(i)+c(i)*randi(4,1);
y(i)=y(i)+c(i)*randi(4,1);
如果x(i)>100 | | y(i)>100
c(i)=-1*c(i);
x(i)=100;y(i)=100;
结束
如果y(i)<0 | | x(i)<0
c(i)=-1*c(i);
x(i)=0;y(i)=0;
结束
结束
暂停(.1)
删除(h1、h2、h3);
结束
正如@franz1所指出的,问题在于
m
和n
变量在循环之外,因此无法更新。
为了扩展他/她的答案,这里有一个可能的movingAnts.m
函数:
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
此外,您应该将makingAnts.m
中的h
的定义更改为:
h = zeros(n,3);
有人能解释一下为什么这个问题得到了这么多的赞成票吗?@franz1-很可能是因为这是一个很酷的问题要解决,尽管问题陈述并不像这篇文章中的答案那么冗长。