Time updated plot has a triangle instead of square
1 回表示 (過去 30 日間)
古いコメントを表示
I wrote a program to produce a time updated plot of two particles. The next position for B is a triangle instead of a square. Does anyone have a fix?
ntrail = 5000;
%tic used to time the code
for trail = 1:1:1000
nsteps = 5000;collision_flag = 0; k = 0;
%Initial Position of A and B
xa = -5; xak = xa; xb = 5; xbk = xb; %For Part D, replace xa and xb with (-4,0) or (-3,0) to simulate them closer together.
ya = 0; yak = ya; yb = 0; ybk = yb;
BC = [5,-5,-5,5];%Set a boundary position
while collision_flag == 0 && k < 1000
[xakp1, yakp1] = RandWalk_2D(xak,yak,BC);
[xbkp1, ybkp1] = RandWalk_2D(xbk,ybk,BC);
%Create Particle A on Grid for Step(k)
xakval = [xak-.5, xak+.5, xak+.5, xak-.5];
yakval = [yak-.5, yak-.5, yak+.5, yak+.5];
%Create Particle A on Grid for Step(k+1)
xakvalp1 = [xakp1-.5, xakp1+.5, xakp1+.5, xakp1-.5];
yakvalp1 = [yakp1-.5, yakp1-.5, yakp1+.5, yakp1+.5];
%Create Particle B on Grid for Step(k)
xbkval = [xbk-.5, xbk+.5, xbk+.5, xbk-.5];
ybkval = [ybk-.5, ybk-.5, ybk+.5, ybk+.5];
%Create Particle B on Grid for Step(k+1)
xbkvalp1 = [xbkp1-.5, xbkp1+.5, xbkp1+.5, xbkp1-.5];
ybkvalp1 = [ybkp1-.5, ybkp1-.5, xbkp1+.5, ybkp1+.5];
figure(1)
hold on
xlim([-5.5,5.5,])
ylim([-5.5,5.5,])
fill(xa,ya,'r')
fill(xakval,yakval,'r') %current step A
fill(xakvalp1, yakvalp1, 'b')%next step for A
fill(xbkval,ybkval,'y') %current step B
fill(xbkvalp1, ybkvalp1, 'p')%next step for B %For problem C comment
%out the next step so B stays in one place.
title('Initial Position of A and B')
title('Final Position of A and B')
hold off
%Update the New Position for Step(k+1)
xak = xakp1; yak = yakp1;
xbk = xbkp1; ybk = ybkp1; %For problem C comment out the update value for B.
k = k+1;
if xak == xbk && yak == ybk
collision_flag = 1;
niter = k;
end
end
end
medval = median(niter);
fprintf("Median = %d\n", medval);
%Time = toc
function [x,y] = RandWalk_2D(x0,y0,BC)
r = rand;
if r<0.2
x = x0;
y = y0+1;
if y >= BC(1); y = BC(1);
end
else if 0.2 < r && r <= 0.4;
x = x0;
y = y0-1;
if y <= BC(2);
y = BC(2);
end
elseif 0.4< r && r<=0.6
x = x0 - 1;
y = y0;
if x<= BC(3); x = BC(3);
end
elseif 0.6 < r && r <= 0.8
x = x0+1; y = y0;
if x >= BC(4)
x = BC(4);
end
elseif 0.8<r
x = x0; y = y0;
end
end
end
0 件のコメント
回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で MATLAB Mobile Fundamentals についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!