Little
Little

Reputation: 3477

how to plot the projection of a vector?

I have made the following program for calculating the vector projection:

a=[6 7]
b=[1 4]
p=(dot(a,b)/(b*b'))*b

the result of p is [2 8] that is the projection of a on b.

I read that for plotting a vector in Matlab I should choose some origin points, so I have added those to the vectors and form a set of matrices with them like this:

x=[0 0; 6 7]
y=[0 0; 1 4]
z=[0 0; 2 8]
plot3(x,y,z)
grid;

but I cannot get to visualize the projection, what am I missing?

Thanks

Upvotes: 1

Views: 3555

Answers (1)

MinaKamel
MinaKamel

Reputation: 253

You can use quiver for 2D vector plotting or quiver3 for 3D plotting.

a = [6 7];
b = [1 4];
p = (dot(a,b)/dot(b,b))*b;

figure;
quiver(0,0,a(1), a(2));
hold on;
quiver(0,0,b(1), b(2));
quiver(0,0,p(1), p(2));

Upvotes: 1

Related Questions