Plot initial and final position only on plot

6 ビュー (過去 30 日間)
Marios Christofides
Marios Christofides 2020 年 7 月 28 日
回答済み: Alan Moses 2020 年 8 月 12 日
I'm making a time updated plot for a random walk simulation and I'm trying to do a plot showing the initial position only and another for the final position only. Does anyone have any ideas on how to do this I haven't been successful with my attempt of plotting (xa,ya)?
%Initial Position of A
xa = -5; xak = xa;
ya = 0; yak = ya;
%Initial Position of B
xb = 5; xbk = xb;
yb = 0; ybk = yb;
%Set a boundary position
BC = [5,-5,-5,5];
%Number of steps
nsteps = 5000;
%Set Collision Flag and Initial Step to Zero
collision_flag = 0; k = 0;
tic
while collision_flag == 0 && k < nsteps
[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, 'g')%next step for B
title('Initial Position of A and B')
%title('Final Position of A and B')
hold off
% if mod(nsteps,1000) == 0
% if collision_flag == 0
% break
% end
% end
%Update the New Position for Step(k+1)
xak = xakp1; yak = yakp1;
xbk = xbkp1; ybk = ybkp1;
k = k+1;
if xak == xbk && yak == ybk
collision_flag = 1;
niter = k;
end
end

回答 (1 件)

Alan Moses
Alan Moses 2020 年 8 月 12 日
It is my understanding that you are trying to solve a collision problem in 2D plane. You can check out the scatter function to plot the initial and the final position. You can use the scatter function to plot the initial position before entering the loop, and by using hold on you can retain the current axes and use scatter once again to plot the final position on detecting collision.

カテゴリ

Help Center および File ExchangeGraphics についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by