can I compare the previous state with current state in " While loop " ?
Mostrar comentarios más antiguos
While loop used to move the ponts from position to position in one shot (each one second). now my question can I can iterate the while loop and
compare the past step with currenct step, for example If we want to calculate the X inside the while loop, we should compare X(i) with X(i-1) ?
Thanks in advance
npts=1;center=[0 0];radius=1000;
npts2=1;center2=[0 0];radius2=1000;
% Initial direction/velocity of the points
velocity = 28.8/3.6;
velocity2 = 28.8/3.6;
theta = rand(npts, 1) * 2*pi;
g = 0.5 * radius + 0.5 * radius * rand(npts,1);
X_x=center(1)+g.*cos(theta);
Y_y=center(2)+g.*sin(theta);
XY = [X_x ,Y_y];
theta2 = rand(npts2, 1) * 2*pi;
g2 = 0.5 * radius2 + 0.5 * radius2 * rand(npts2,1);
X_x2=center2(1)+g2.*cos(theta2);
Y_y2=center2(2)+g.*sin(theta2);
XY2 = [X_x2 ,Y_y2];
hfig = figure('Color', 'w');
hax = axes('Parent', hfig);
hdots(1) = plot(XY(1,1),XY(1,2),'Parent', hax,'Marker', '.','Color', 'k','LineStyle', 'none','MarkerSize', 10);
hold(hax, 'on')
axis(hax, 'equal')
hdots(2) = plot(XY2(1,1),XY2(1,2),'Parent', hax,'Marker', '.','Color', 'r','LineStyle', 'none','MarkerSize', 10);
hold(hax, 'on')
axis(hax, 'equal')
% Plot the circl e as a reference
t = linspace(0, 2*pi, 100);
plot(radius * cos(t) + center(1),radius * sin(t) + center(2))
while all(ishghandle(hdots)) %Timestep=1:1:Totaltime
direction2 = rand(npts, 1) * 2 *pi;
direction = rand(npts, 1) * 2 *pi;
[XY2, direction2] = step(XY2, direction2, velocity2, radius2, center2);
% Plot the dots as black markers
[XY, direction] = step(XY, direction, velocity, radius, center);
% Update the dot plot to reflect n ew locations
set(hdots(2), 'XData', XY2(1,1), 'YData', XY2(1,2))
set(hdots(1), 'XData', XY(1,1), 'YData', XY(1,2))
% Force a r edraw
drawnow
%pause (1)
end
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!