
Plot the angle between two vectors
    16 views (last 30 days)
  
       Show older comments
    
    Nicholas Snyder
 on 11 Feb 2021
  
    
    
    
    
    Commented: Nicholas Snyder
 on 2 Mar 2021
            Hey MATLAB Answers! 
I am trying to neatly plot the angle between vectors in 3D plots/figures as arcs with their respective angle. Specifically the angles between the x, y, and z-axis and Resultant. The values of which are DirectionAlpha, DirectionBeta, and DirectionGamma respectively. I have attached my code thus far and a picture of the kind of plot I wish to emulate.

%% Resultant Vector Workshop
clc;clearvars;close all %Clear the command window,Clear all workspace variables,Closes all figures
%% Begin Question 2-66
%Magnitude of Force for each Force
Force1 = 300; %Force of F_1 in [N].
Force2 = 500; %Force of F_2 in [N].
%If a Force uses an angle measurement put it here
Alpha1 = 60; %Angle between x-axis and F2 [Degrees].
Beta1 = 45;%Angle between y-axis and F2 [Degrees].
Gamma1 = 120;%Angle between z-axis and F2 [Degrees].
Theta1 = 45; %Angle of Theta1 in [Degrees].
Theta2 = 60;%Angle of Theta2 in [Degrees].  
%Direction Vector for each Force
Vector1 = [-cosd(Theta2)*sind(Theta1), cosd(Theta2)*cosd(Theta1), sind(Theta2)]; %Direction vector of F1.
Vector2 = [cosd(Alpha1), cosd(Beta1), cosd(Gamma1)]; %Direction vector of F2.
%Each Force we are interested in as a vector
F1 = Force1*Vector1; %The magnitude and direction of F1 as a vector.
F2 = Force2*Vector2; %The magnitude and direction of F2 as a vector.
%Solve
Resultant = F1+F2; %Determine the Resultant Vector y adding force vectors
Magnitude = norm(Resultant); %Determine the Magnitude of the Resultant Vector
DirectionAlpha = acosd(Resultant(1)/Magnitude); %Determine the direction of the Resultant Vector [Degrees]
DirectionBeta = acosd(Resultant(2)/Magnitude); %Determine the direction of the Resultant Vector [Degrees]
DirectionGamma = acosd(Resultant(3)/Magnitude); %Determine the direction of the Resultant Vector [Degrees]
%% Extra Stuff
%The next lines of code make the information displayed to the command
%window more user friendly.
fprintf('F1 in Cartesian form is [%.2fi  %.2fj  %.2fk] [N]\n',F1(1),F1(2),F1(3)) %With formating send F1 to the command window
fprintf('F2 in Cartesian form is [%.2fi  %.2fj  %.2fk] [N]\n',F2(1),F2(2),F2(3)) %With formating send F2 to the command window
fprintf('The Resultant Vector in Cartesian form is [%.2fi  %.2fj  %.2fk] [N]\n',Resultant(1),Resultant(2),Resultant(3)) %With formating send the Resultant to the command window
fprintf('With Magnitude %.2f [N] and Direction %.2f [Degrees] from positive "X", %.2f [Degrees] from positive "Y", %.2f [Degrees] from positive "Z" \n',Magnitude,DirectionAlpha,DirectionBeta, DirectionGamma) %With formating send the Magnitude and Direction to the command window
figure()
hold on
plot3([0 0 0; F1(1),F1(2),F1(3)],[0 0 0; F2(1),F2(2),F2(3)],[0 0 0; Resultant(1),Resultant(2),Resultant(3)])
plot3([0 0 0; 0 0 Magnitude], [0 0 0; 0 Magnitude 0], [0 0 0; Magnitude 0 0],'--k')
Any help is greatly appreciated, even if thats a “this isn’t possible” as that would put my mind at ease. 
0 Comments
Accepted Answer
  darova
      
      
 on 11 Feb 2021
        Maybe you will beinterested
clc,clear
ni = 10;
n = 5;
    % some vectors
x = rand(n,1);
y = rand(n,1);
z = rand(n,1);
plot3([0 x(end)],[0 y(end)],[0 z(end)])
    % draw arcs between vectors
hold on
for i = 1:n-1
    x1 = linspace(x(i),x(i+1),ni);
    y1 = linspace(y(i),y(i+1),ni);
    z1 = linspace(z(i),z(i+1),ni);
    L1 = sqrt(x1.^2+y1.^2+z1.^2);
    plot3([0 x(i)],[0 y(i)],[0 z(i)])   % vector
    plot3(x1./L1,y1./L1,z1./L1,'k')     % arc
end
hold off
view(45,45)

More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
