Plot a line between two points with given angle which points its direction.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
I need your guidance that how I can compute angle between two points and draw a direction vector to get the direction of these two colored lines(Pink,Blue) at each instant of time.
Secondly is it possible that I can extract the speed as I am given (x,Y) position and time (as can be seen from the figure which is plotted on X,Y plane in a given time)?
Thanks in advance for your kind reply.

Accepted Answer
Star Strider
on 21 Dec 2015
8 Comments
Arsal15
on 20 Jan 2016
Using quiver I have got this graph, using quiver [x,y,u,v] and i know the x and y position of start and end of direction vector for a node but how I can get the value of node position in the middle of a vector. It will be (x2-x1/u,y2-y1/v) or something different?

Star Strider
on 20 Jan 2016
I don’t know what you mean by ‘the value of node position in the middle of a vector’. You would have to know one value (either x or y) to get the other. If you have two specific (x,y) points and you want an equation for a line connecting them, the easiest way would be to use the polyfit and polyval functions and the known x-value between them:
b = polyfit([x1, x2], [y1, y2], 1);
y3 = polyval(b, x3);
such that ‘x3’ here is between ‘x1’ and ‘x2’ (that is, x1 < x3 < x2).
Star Strider, Thanks for your kind reply.
I have attached my code file so you will better get idea about my code. I want to find the position of a node at any instant of time when i just run this simulation it give me minimum distance from other node and its own position at that time instant.

Star Strider
on 21 Jan 2016
You have four variables each (1x501) vectors. Your time variable is a (7x1) double, so doing anything with respect to that time vector is not going to work. You could get time from the position differences and the velocities, but I am lost as to what you want to do. Plotting ‘v_x’ and ‘v_y’ is straightforward and produces 5 straight lines.
I have no idea what you want to do, but I took a wild guess and came up with this:
D = load('Arsal15 node_property.mat');
np = D.node_property;
x = np.v_x;
y = np.v_y;
vx = np.vel_x;
vy = np.vel_y;
xp = gradient(x); % X-Position Change
yp = gradient(y); % Y-Position Change
vt = hypot(vx(:), vy(:)); % Net Velocity
pt = hypot(xp, yp); % Net Position Change
tv = pt(:)./vt(:); % Differential Time
tc = cumsum(tv); % Cumulative Time
figure(1)
plot3(x, y, tc, '-r')
hold on
quiver3(x(:), y(:), tc(:), xp(:), yp(:), tv(:), 1.5, 'LineWidth',1)
hold off
grid on
xlabel('X')
ylabel('Y')
zlabel('Time')
figure(2)
plot3(x, y, tc, '.r')
hold on
quiver3(x(:), y(:), tc(:), xp(:), yp(:), tv(:), 2.5, 'LineWidth',1)
hold off
grid on
xlabel('X')
ylabel('Y')
zlabel('Time')
Arsal15
on 21 Jan 2016
Thanks for your time and kind reply Star Strider.
Actually I want to do three things
Regarding time I can use interpolation like this so all index will be (1x51).
if true
node_property(nodeIndex).time = interp1(s_mobility.VS_NODE(nodeIndex).V_TIME,v_t);
end
1) .Getting data from the structure in a efficient way so that I use one loop index and I get data of all mobile nodes in their variable like x_position, y_position x_velocity, y_velocity using single for loop.
2). I want the position of mobile node at any instant of time like if I call a function
if true
[x,y] = node_position(node_number,time_instant,direction);
end
it gives times position of that node at that time instant but also direction vector value.
3). I want to calculated distance of node1(x,y) with node2(x,y) I am using function
if true
distance = findSqDistance(node1_X,node1_Y,node2_X,node2_Y);
end
but I am fail to pass array of (1x51) of x and y of node1 and node 2 to this function and get a result back in distance matrix which also a (1x51) so I m stuck. Actually i get puzzled in indexing.
Star Strider
on 21 Jan 2016
My pleasure.
I have no idea what you’re doing. I gave my best guess, and that’s all I can do. Your ‘nodes’ simply look like points on the trajectory of something, and beyond plotting them as I did, I really cannot help further.
Arsal15
on 24 Jan 2016
Thanks for your guidance.
Star Strider
on 24 Jan 2016
My pleasure.
More Answers (0)
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)