# How to calculate angle between two vectors?

14 views (last 30 days)
Ors on 7 Aug 2022
Commented: Ors on 10 Aug 2022
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));
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.
William Rose on 7 Aug 2022
@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 .
Recall that , where ϕ is the angle between r and .
Then (since ).
Finally, .

William Rose on 7 Aug 2022
@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 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
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.
Ors on 10 Aug 2022
I see, thank you William :D

### Categories

Find more on Lighting, Transparency, and Shading in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!