How to calculate angle between two vectors? (2024)

23 views (last 30 days)

Show older comments

Ors on 7 Aug 2022

  • Link

    Direct link to this question

    https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors

  • Link

    Direct link to this question

    https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors

Commented: Ors on 10 Aug 2022

Accepted Answer: William Rose

  • Screenshot_20220807_013216.png

The attached plot is as you can see an array of points in 3D. The lines link points from the XY plane with points along the Z-axis. They are suppose to be vectors, however when I plot with quiver3 I get a vector facing normal to the XY surface, instead of what I would like it pointing towards the points toward the Z-axis, if that makes sense.

Below you can see a part of the code:

total_p=200;

for i = 1 : nout

for j = 1 : pout

if i == 1

ko = nr1;

r = r1;

elseif i == 2

ko = nr2;

r = r2;

elseif i == 3

ko = nr3;

r = r3;

elseif i == 4

ko = nr4;

r = r4;

end

teta = 2*pi()/ko;

for k = 1 : ko

F{j,i}(k,1) = r*cos(k*teta);

F{j,i}(k,2) = r*sin(k*teta);

F{j,i}(k,3) = 0 - (j-1)*(s+a)*1e3;

for l = 1 : total_p

rout{j,i}(l,1)=sqrt(((F{j,i}(k,1)-calc_area(l,1))^2)+((F{j,i}(k,2)-calc_area(l,2))^2)+((F{j,i}(k,3)-calc_area(l,3))^2));

%teta2{j,i}(l,1)= rad2deg(atan(rout{j,i}(l,1)/0.1));

end

figure(1)

scatter3(F{j,i}(k,1),F{j,i}(k,2),F{j,i}(k,3),'k.')

hold on

daspect([1,1,1])

end

clear r ko teta1

end

end

In the end I wish to calculate the magnitude of vectors between points of F and and calc_area.

where calc_area has points x,y=0 and z=5 to 100; I attempted to calculate the magnitude and saved it in rout cell. Then I have teta2, where I am calculating the angle, however it yields around 90 degrees. For the red line this should be very small... and for the blue line around 30 degrees from visual inspection. What am I doing wrong here?

Thank you for any help.

1 Comment

Show -1 older commentsHide -1 older comments

William Rose on 7 Aug 2022

Direct link to this comment

https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2304830

  • Link

    Direct link to this comment

    https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2304830

@Ors, quiver3() automatically scales the lengths of the vectors it draws it an attempt to prevent vectors from overlapping. Turn off the automatic scalling by adding option 'off':

quiver3(X,Y,Z,U,V,W,'off')

where "The vectors X, Y, and Z represent the location of the base of each arrow, and U, V, and W represent the directional components of each arrow. By default, the quiver3 function shortens the arrows so they do not overlap. Call axis equal to use equal data unit lengths along each axis. This makes the arrows point in the correct direction.". quiver3 help

The angle you appear to seek is θ, the complement of the angle between the vector r and the z-axis. Compute this by calculating the dot product of the vector r with How to calculate angle between two vectors? (3).

Recall that How to calculate angle between two vectors? (4), where ϕ is the angle between r and How to calculate angle between two vectors? (5).

Then How to calculate angle between two vectors? (6) (since How to calculate angle between two vectors? (7)).

Finally, How to calculate angle between two vectors? (8).

Sign in to comment.

Sign in to answer this question.

Accepted Answer

William Rose on 7 Aug 2022

  • Link

    Direct link to this answer

    https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#answer_1022330

  • Link

    Direct link to this answer

    https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#answer_1022330

@Ors, My comment above was intended to be an answer. Please accept this answer if it is satisfactory. Here is some code.

F=[-20,200,0;100,-100,0;-200,20,0]; %points in X-Y plane

calc_area=[0,0,5;0,0,50;0,0,100]; %points on z-axis

r=calc_area-F; %vectors

plot3(calc_area(:,1),calc_area(:,2),calc_area(:,3),'r.') %plot red points on z-axis

hold on; grid on; axis equal; %plot details

xlabel('X'),ylabel('Y');zlabel('Z') %axis labels

plot3(F(:,1),F(:,2),F(:,3),'k.'); %plot black points in the X-Y plane

xlim([-250,250]); ylim([-250,250]); zlim([0 150]) %set axis limits

X=F(:,1); Y=F(:,2); Z=F(:,3); %starting points for arrows

U=r(:,1); V=r(:,2); W=r(:,3); %arrow lengths in each dimension

quiver3(X,Y,Z,U,V,W,'off') %draw arrows on the plot

How to calculate angle between two vectors? (10)

When you run the code, you will be able to rotate the 3D plot, by clicking onthe 3-D rotation icon above the plot, then clicking and dragging within the plot area.

Compute the angles of the vectors above the X-Y plane:

for i=1:3

phiDeg(i)=acosd(dot(r(i,:),[0,0,1])/norm(r(i,:)));

end

thetaDeg=90-phiDeg

thetaDeg = 1×3

1.4250 19.4712 26.4512

The code for the angles implements the equations I gave in my comment above. I used acosd() to get the angle phi, in degrees.

Good luck.

5 Comments

Show 3 older commentsHide 3 older comments

Ors on 7 Aug 2022

Direct link to this comment

https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305055

  • Link

    Direct link to this comment

    https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305055

Edited: Ors on 7 Aug 2022

Thank you so much William! :D

Although both, your comment and answer are more than satisfactory I am curious

why my method did not work. This is not urgent though, however I would really appreciate it.

William Rose on 7 Aug 2022

Direct link to this comment

https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305300

  • Link

    Direct link to this comment

    https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305300

In your code fragment, I do not see where you try to calculate the angles between the vectors from F to calc_area and the normal to the X-Y plane. Therefore I cannot comment on why your method did not work.

In my script, the angles between the vectors from F to calc_area is called "theta()". Your "teta" is not analagous to my "theta". Your teta is an angle in the x-y plane, which is used as a multiplier to compute the x-y coordinates of the points F, as seen belwow in this fragment from the code you posted:

teta = 2*pi()/ko;

for k = 1 : ko

F{j,i}(k,1) = r*cos(k*teta);

F{j,i}(k,2) = r*sin(k*teta);

F{j,i}(k,3) = 0 - (j-1)*(s+a)*1e3;

%...

end

The code above creates a set of ko points F{}() which are spaced at angle How to calculate angle between two vectors? (13) in the X-Y plane.

Ors on 7 Aug 2022

Direct link to this comment

https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305455

  • Link

    Direct link to this comment

    https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305455

yes, sorry... its commented out and is teta2

should have mentioned that :)

William Rose on 7 Aug 2022

Direct link to this comment

https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305470

  • Link

    Direct link to this comment

    https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305470

YOu have

teta2{j,i}(l,1)= rad2deg(atan(rout{j,i}(l,1)/0.1));

It apears that rout is the length of a 3D vector from a point F to a point calc_area.

You could simplify the equation to

teta2=rad2deg(atan(num/den));

This will work IF num=length of the projection of the vector onto the x-y plane, and if den=length of the projection of the vector onto the z axis. Neither of those conditions appear to be true. That is why this method does not give the correct answer.

Ors on 10 Aug 2022

Direct link to this comment

https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2309910

  • Link

    Direct link to this comment

    https://www.matlab.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2309910

I see, thank you William :D

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsGeographic Plots

Find more on Geographic Plots in Help Center and File Exchange

Tags

  • vectors
  • angle
  • magnitude
  • 3d

Products

  • MATLAB

Release

R2020b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to calculate angle between two vectors? (17)

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

Americas

Europe

Asia Pacific

Contact your local office

How to calculate angle between two vectors? (2024)
Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6060

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.