How can I plot two vectors starting from origin?

15 views (last 30 days)
I wrote the following code to set the appearance of the axis and the figure:
search_width = 5;
figure2 = figure;
% Figure erstellen
axes2 = axes('Parent',figure2);
hold(axes2,'on');
% Axen erstellen
figure2.Name = sprintf('Vektoren in der Suchmaske - alpha = %g', alpha(1, 1));
figure2.NumberTitle = 'off';
% Titel des Fensters bearbeiten
axes2.XLim = [ (-(round((search_width/2)+1))) (round((search_width/2)+1))];
axes2.YLim = [ (-(round((search_width/2)+1))) (round((search_width/2)+1))];
axes2.XAxisLocation = 'origin';
axes2.YAxisLocation = 'origin';
% Achsen in den Mittelpunkt verschieben
% Vektoren
box(axes2,'on');
% Einstellungen zum Aussehen des Schaubildes
Now I want to plot two vectors.
a = [-3; -3]
b = [-3; 0]
a and b determines the end of the vectors. Starting-point shall be for both vectors the point [0, 0]. It would be nice if the vectors have the appearance of arrows. How can I manage this?
I use MATLAB R2015b.

Accepted Answer

Star Strider
Star Strider on 21 Oct 2015
Use the quiver function:
o = [0; 0];
a = [-3; -3];
ac = [a o];
auv = [o a];
b = [-3; 0];
bc = [b o];
buv = [o b];
figure(1)
quiver(ac(1,:), ac(2,:), auv(1,:), auv(2,:), 0)
hold on
quiver(bc(1,:), bc(2,:), buv(1,:), buv(2,:), 0)
hold off
axis([-5 1 -5 1])
  2 Comments
Chewy
Chewy on 22 Oct 2015
Thanks a lot, that works. For everybody else who has the same problem, here´s the complete code:
edge_search_width = 5;
% Determination of search width
c_v_y = -1;
c_v_x = 3;
% Current-Vector
t_v = [0; -(edge_search_width-2)];
% Top-Vector, shows always to the top. x = 0, y = -edge_search_width.
% Calculations for quiver-command in the visualisation.
o = [0; 0];
c_v = [c_v_x; c_v_y];
ac = [c_v o];
auv = [o c_v];
bc = [t_v o];
buv = [o t_v];
%%Visualisation
figure2 = figure;
% create figure
axes2 = axes('Parent',figure2);
hold(axes2,'on');
% create axes
figure2.Name = 'vectors in searchmask';
figure2.NumberTitle = 'off';
% create title of the window
axes2.XLim = [ (-(round((edge_search_width/2)+1))) (round((edge_search_width/2)+1))];
axes2.YLim = [ (-(round((edge_search_width/2)+1))) (round((edge_search_width/2)+1))];
axes2.XAxisLocation = 'origin';
axes2.YAxisLocation = 'origin';
axes2.YDir = 'reverse';
% set axes to the origion
quiver(ac(1,:), ac(2,:), auv(1,:), auv(2,:), 0)
hold on
quiver(bc(1,:), bc(2,:), buv(1,:), buv(2,:), 0)
hold off
% plot vectors
box(axes2,'on');
% appearance of the figure

Sign in to comment.

More Answers (1)

TastyPastry
TastyPastry on 21 Oct 2015
Edited: TastyPastry on 21 Oct 2015
Try the Da Vinci Draw Toolbox. The old arrow.m file doesn't work anymore.
There is a method using annotation() and another using quiver(). They quiver() method doesn't draw exact vectors to the endpoint.

Categories

Find more on Vector Fields in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!