editing the random waypoint mobility matlab for a constant node - matlab

Please help me out with this. This plot gives the mobile users that are moving. Now, I am trying to add a few constant points in the plot. Like the base stations for the mobile users '*'. I want to put say a '#' for every base station and put like around 10 of them.
function test_Animate(s_mobility,s_input,time_step)
v_t = 0:time_step:s_input.SIMULATION_TIME;
for nodeIndex = 1:s_mobility.NB_NODES
%Simple interpolation (linear) to get the position, anytime.
%Remember that "interp1" is the matlab function to use in order to
%get nodes' position at any continuous time.
vs_node(nodeIndex).v_x = interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,s_mobility.VS_NODE(nodeIndex).V_POSITION_X,v_t);
vs_node(nodeIndex).v_y = interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,s_mobility.VS_NODE(nodeIndex).V_POSITION_Y,v_t);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;
hold on;
for nodeIndex = 1:s_mobility.NB_NODES
vh_node_pos(nodeIndex) = plot(vs_node(nodeIndex).v_x(1),vs_node(nodeIndex).v_y(1),'*','color',[0.3 0.3 1]);
end
title(cat(2,'Simulation time (sec): ',num2str(s_mobility.SIMULATION_TIME)));
xlabel('X (meters)');
ylabel('Y (meters)');
title('Random Waypoint mobility');
ht = text(min(vs_node(1).v_x),max(vs_node(1).v_y),cat(2,'Time (sec) = 0'));
axis([min(vs_node(1).v_x) max(vs_node(1).v_x) min(vs_node(1).v_y) max(vs_node(1).v_y)]);
hold off;
for timeIndex = 1:length(v_t);
t = v_t(timeIndex);
set(ht,'String',cat(2,'Time (sec) = ',num2str(t,4)));
for nodeIndex = 1:s_mobility.NB_NODES
set(vh_node_pos(nodeIndex),'XData',vs_node(nodeIndex).v_x(timeIndex),'YData',vs_node(nodeIndex).v_y(timeIndex));
end
drawnow;
end
end
The complete submission of this can be got in the MATHworks site.
http://www.mathworks.in/matlabcentral/fileexchange/30939-random-waypoint-mobility-model
Please help me out.

Here's the basic idea. In the following, fixed_x and fixed_y are vectors of equal length giving the location of your fixed points.
moving_x and moving_y are matrices of size t x n where t is the number of time steps, and n the number of points.
%plot the fixed points
plot(fixed_x,fixed_y, 'p');
hold on
%plot the first set of moving points
h = plot(moving_x(1,:),moving_y(1,:),'r*');
Then, to move the points, we use the handle to the moving points, h to set XData and YData values. This would be within a loop where n is the time step:
set(h, 'XData',moving_x(n,:),'YData',moving_y(n,:));
%may need drawnow;
If you want to be able to see the points moving, you may need to add some pause command - otherwise it might cycle through the update loop so quickly you won't see anything except the final set of positions. On the other hand, you could just save out the figure as an image file after you've moved the points with set, or add it as a frame to a movie, etc.

Related

Creating a point moving along a circle in MATLAB with speed and radius defined by the user

I am looking to create a simple circle graph within MATLAB in which the model shows the point moving along the circle with radius and angular velocity defined by the user.
Angular velocity in RADIANS/SEC
I am relatively new at MATLAB coding so any help would be very useful!
I tried this code:
r=1;
t = 0:.01:2*pi;
x = r*cos(t);
y = r*sin(t);
comet(x,y);
But when I change the 0.01 value the point doesn't move faster, it just skips more of the curve, also i'm unsure if the increments are in radians.
Thanks for your time
Edited version: See edit history for previous version.
Radius = 10;
AngularVelocity = 5; % in deg / s
AngleStep = 0.1
Angles = AngleStep : AngleStep : 2*pi;
CircleX = [Radius]; % empty array
CircleY = [0]; % empty array
%Initial zero-angle plot whose data we'll keep updating in the for loop:
a = plot([CircleX,CircleX], [CircleY,CircleY], 'r:');
hold on;
b = plot(CircleX, CircleY, 'o', 'markeredgecolor', 'k', 'markerfacecolor','g');
axis([-Radius, +Radius, -Radius, +Radius]); % make sure the axis is fixed
axis equal; % make x and y pixels of equal size so it "looks" a circle!
hold off;
for t = Angles
CircleX(end+1) = Radius * cos (t); % append point at end of CircleX array
CircleY(end+1) = Radius * sin (t); % append point at end of Circley array
set(a,'xdata',CircleX,'ydata',CircleY); % update plot 'a' data
set(b,'xdata', CircleX(end), 'ydata', CircleY(end)); % update plot 'b' data
drawnow; % ensure intermediate frames are shown!
pause(AngleStep/AngularVelocity) % pause the right amount of time!
end
This edit has made two changes compared to the previous version:
Instead of redrawing, now we're updating the data of an existing plot. This is generally faster as matlab doesn't have to redraw axes objects (i.e. the containers that hold the plot)
I increased AngleStep from 0.01 to 0.1. This means there's 10 times less angles to draw, so you can afford to draw then 10 times slower, therefore it becomes less likely that matlab will be unable to draw because of overhead. Having said that, this is at the cost of a less perfect circle. Try with AngleStep=1 to see what I mean.

How to animate points on an image in MATLAB?

I have the pixel locations of P points on a -constant- image, for T iterations of an algorithm, so locations = [T x 2*P] double.
Now I want to create an animation where it plots the image, then plots the points, pauses for N seconds and updates their location to the next step. I don't know if there is a standard way to follow. I think I need something like:
figure;
imshow(img);
hold on;
for t=1:T
anim = updatePlot(locations(t,:), anim); % ?
end
How can I implement this updatePlot function?
Thanks for any help!
You can do this a couple of different ways. The first way would be to give the plotted points a handle so that you can delete them before the next iteration:
figure
imshow(img);
hold on;
for t = 1:T
% delete the previous points plotted (skip t = 1 since they won't exist)
if t > 1
delete(hPoints);
end
hPoints = plot(xLocations(t,:),yLocations(t,:),'.');
getframe;
pause(N);
end
(I am not exactly sure how you parse your locations along each row to separate the x and y components, so I've just used xLocations and yLocations to represent those values.)
The second way would be to re-draw the entire image at each iteration:
figure
for t = 1:T
clf;
imshow(img);
hold on;
plot(xLocations(t,:),yLocations(t,:),'.');
getframe;
pause(N);
end
Note that imshow might have its own getframe effect so that you'll see the image flicker before plotting the points -- if that happens just switch from imshow to image.

plotting line between points in a loop goes wrong

I am currently trying to simulate a random walk. The idea is to choose a random number between 0 and 2*pi and let the random walker go in that direction. Here is what I tried to do to simulate such a random walk:
before=[0 0]; %start in (0,0)
while 1
x=rand;
x=x*2*pi; %// choose random angle
increment=[cos(x),sin(x)] %// increments using the sine and cosine function
now=before+increment;
plot(before, now)
hold on
before=now;
pause(1);
end
I expect this program to plot lines and each new line starts at the ending point of the previous line, but this does not happen. I have no clue why it is not working.
You got the syntax for plot wrong, which is plot(X,Y). Change the call to
plot([before(1), now(1)], [before(2), now(2)])
and your program should work as expected.
Here is an improved version that does all the calculation vectorized and gives you two choices of output. The first one displays all at once and is very fast. The second one takes a lot of time depending on the amount of samples.
pts = [0,0]; % starting point
N = 10000; % sample count
x = rand(N,1) * 2*pi; % random angle
% calculate increments and points
inc = [cos(x),sin(x)];
pts = [pts;cumsum(inc,1)];
% plot result at once
figure;
plot(pts(:,1),pts(:,2));
% plot results in time steps
figure; hold on;
for i = 1:size(pts,1)
plot(pts(i:i+1,1),pts(i:i+1,2))
pause(1)
end
Here is an example of the output:

dynamic Polar plot on normal plot

Request a big help on this. I am trying to use a dynamic polar plot on normal plot. I have a random waypoint model. Now, I would like to put a dynamically changing polar plot to reach a few selected users in the random waypoint.
function test_Animate(s_mobility,s_input,time_step)
v_t = 0:time_step:s_input.SIMULATION_TIME;
for nodeIndex = 1:s_mobility.NB_NODES
%Simple interpolation (linear) to get the position, anytime.
%Remember that "interp1" is the matlab function to use in order to
%get nodes' position at any continuous time.
vs_node(nodeIndex).v_x = interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,s_mobility.VS_NODE(nodeIndex).V_POSITION_X,v_t);
vs_node(nodeIndex).v_y = interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,s_mobility.VS_NODE(nodeIndex).V_POSITION_Y,v_t);
end
fig = figure;
hold on;
for nodeIndex = 1:s_mobility.NB_NODES
vh_node_pos(nodeIndex) = plot(vs_node(nodeIndex).v_x(1),vs_node(nodeIndex).v_y(1),'*','color',[0.3 0.3 1]);
axis off;
end
title(cat(2,'Simulation time (sec): ',num2str(s_mobility.SIMULATION_TIME)));
xlabel('X (meters)');
ylabel('Y (meters)');
title('Random Waypoint mobility');
ht = text(min(vs_node(1).v_x),max(vs_node(1).v_y),cat(2,'Time (sec) = 0'));
axis([min(vs_node(1).v_x) max(vs_node(1).v_x) min(vs_node(1).v_y) max(vs_node(1).v_y)]);
disp(min(vs_node(1).v_x));
disp(max(vs_node(1).v_x));
disp(min(vs_node(1).v_y));
disp(max(vs_node(1).v_y));
hold off;
for timeIndex = 1:length(v_t);
t = v_t(timeIndex);
set(ht,'String',cat(2,'Time (sec) = ',num2str(t,4)));
for nodeIndex = 1:s_mobility.NB_NODES
set(vh_node_pos(nodeIndex),'XData',vs_node(nodeIndex).v_x(timeIndex),'YData',vs_node(nodeInd ex).v_y(timeIndex));
curr_x_pos=vs_node(nodeIndex).v_x(timeIndex);
% curr_y_pos=vs_node(nodeIndex).v_y(timeIndex);
%disp(curr_x_pos);
%curr_y_pos=vs_node(nodeIndex).v_y(timeIndex);
end
drawnow;
end
end
Now, in this above code, I need to add a polar plot which has directional antenna beam. Please help me out as to how to change the beam position according to user position which we can see on the plot. As in, the beam should encircle the user position dynamically. The other files regarding this are present code are in the following link. RANDOM WAYPOINT MOBILITY MODEL. Request a big help.
Thanks in advance.

MATLAB plot moving data points in separate subplots simultaneously

I wish to visualize the movement of a data point throughout space across a period of time within MATLAB. However, the way I want my figure to display is such that only a single instant is plotted at any given time. That was easy, I simply created a for loop to update my 3D plot display for every set of coordinates (x,y,z) in my data. However, I wish to display 4 different viewing angles of this plot at all times. I am well aware of how to setup subplots within MATLAB, that is not the issue. My issue is getting all 4 of these subplots to execute simultaneously so that all 4 subplots are always displaying the same point in time.
How to handle this issue?
As requested, my code for a figure with a single plot is shown below:
datan = DATA; %data in form of x,y,z,a,b,c by column for row# of time points
tib=zeros(size(datan,1),12);
tib(:,1:3) = datan(:,1:3);
tib_ref=tib(1,1:3);
for i=1:size(datan,1)
tib(i,1:3)=tib(i,1:3)-tib_ref;
end
angle_to_dircos
close all
figure('Name','Directions (Individual Cycles)','NumberTitle','off')
for cc=1:2
hold off
for bb=1:10:size(tib,1);
scatter3(tib(bb,1),tib(bb,2),tib(bb,3),'green','filled'); %z and y axes are flipped in polhemus system
hold on
p0 = [tib(bb,1),tib(bb,2),tib(bb,3)];
p1 = [tib(bb,1)+10*tib(bb,4),tib(bb,2)+10*tib(bb,5),tib(bb,3)+10*tib(bb,6)];
p2 = [tib(bb,1)+10*tib(bb,7),tib(bb,2)+10*tib(bb,8),tib(bb,3)+10*tib(bb,9)];
p3 = [-(tib(bb,1)+100*tib(bb,10)),-(tib(bb,2)+100*tib(bb,11)),-(tib(bb,3)+100*tib(bb,12))];
vectarrow(p0,p1,1,0,0)
hold on
vectarrow(p0,p2,0,1,0)
hold on
vectarrow(p0,p3,0,0,1)
hold on
az = 90;
el = 0;
view(az, el);
xlim([-50,50]);
ylim([-50,50]);
zlim([-50,50]);
xlabel('distance from center in X');
ylabel('distance from center in Y');
zlabel('distance from center in Z');
title('XYZ Scatter Plots of Tracker Position');
hold on
plot3(0,0,0,'sk','markerfacecolor',[0,0,0]);
p0 = [0,0,0];
p1 = [10,0,0];
p2 = [0,10,0];
p3 = [0,0,100];
vectarrow(p0,p1,1,0,0)
hold on
vectarrow(p0,p2,0,1,0)
hold on
vectarrow(p0,p3,1,0,1)
drawnow;
end
end
If you use set to update the x and y-data of your points, rather than recreating the plot entirely every time, the update will be simultaneous thanks to Matlab waiting for drawnow.
Here's an example
figure,
subplot(1,2,1),plot(rand(10,1),rand(10,1),'.'),hold on,p1=plot(rand(1),rand(1),'.r')
subplot(1,2,2),plot(rand(10,1),rand(10,1),'.'),hold on,p2=plot(rand(1),rand(1),'.r')
%# read the red coordinates - I should have stored them before plotting :)
x(1) = get(p1,'xdata');y(1)=get(p1,'ydata');x(2)=get(p2,'xdata');y(2)=get(p2,'ydata');
%# animate
for i=1:100,
delta = randn(1,2)*0.01;
x=x+delta(1);
y=y+delta(2);
set(p1,'xdata',x(1),'ydata',y(1));
set(p2,'xdata',x(2),'ydata',y(2));
pause(0.1),
drawnow, %# I put this in case you take out the pause
end