How to calculate angle between two vectors?
Afficher commentaires plus anciens
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 commentaire
William Rose
le 7 Août 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
).
(since Finally,
.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Surface and Mesh Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
