22visualizaciones (últimos 30días)
Mostrar comentarios más antiguos
Ors el 7 de Ag. de 2022
Comentada: Ors el 10 de Ag. de 2022
Respuesta aceptada: William Rose
- Screenshot_20220807_013216.png
Abrir en MATLAB Online
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 comentario Mostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Mostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
William Rose el 7 de Ag. de 2022
Enlace directo a este comentario
https://la.mathworks.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 .
Recall that , where ϕ is the angle between r and .
Then (since ).
Finally, .
Iniciar sesión para comentar.
Iniciar sesión para responder a esta pregunta.
Respuesta aceptada
William Rose el 7 de Ag. de 2022
Abrir en MATLAB Online
@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
thetaDeg=90-phiDeg
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 comentarios Mostrar 3 comentarios más antiguosOcultar 3 comentarios más antiguos
Mostrar 3 comentarios más antiguosOcultar 3 comentarios más antiguos
Ors el 7 de Ag. de 2022
Enlace directo a este comentario
https://la.mathworks.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305055
Editada: Ors el 7 de Ag. de 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 el 7 de Ag. de 2022
Enlace directo a este comentario
https://la.mathworks.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305300
Abrir en MATLAB Online
@Ors,
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 in the X-Y plane.
Ors el 7 de Ag. de 2022
Enlace directo a este comentario
https://la.mathworks.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 el 7 de Ag. de 2022
Enlace directo a este comentario
https://la.mathworks.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2305470
@Ors,
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 el 10 de Ag. de 2022
Enlace directo a este comentario
https://la.mathworks.com/matlabcentral/answers/1775230-how-to-calculate-angle-between-two-vectors#comment_2309910
I see, thank you William :D
Iniciar sesión para comentar.
Más respuestas (0)
Iniciar sesión para responder a esta pregunta.
Ver también
Categorías
MATLABGraphics2-D and 3-D PlotsGeographic Plots
Más información sobre Geographic Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Se ha producido un error
No se puede completar la acción debido a los cambios realizados en la página. Vuelva a cargar la página para ver el estado actualizado.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia-Pacífico
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Comuníquese con su oficina local