Calculate angles correctly between two vectors using the dot product.

46 Ansichten (letzte 30 Tage)
LH
LH am 4 Jan. 2024
Bearbeitet: Torsten am 7 Sep. 2025 um 12:05
Hi all,
As shown below in the figure, I have two vectors, and , and I want to calculate the angle with the norm, and, between each of these vectors with respect to vector L using the dot product.
Here is a simple code to calculate these angles:
close all;
clear all;
%define the reference vector
L = [-0.5 -0.5];
%v1
v1 = [-0.6788 0.3214];
%theta1 with the x axis
theta1 = acos((v1(1)*L(1)+v1(2)*L(2))/(sqrt(v1(1)^2+v1(2)^2)*sqrt(L(1)^2+L(2)^2)));
%theta1 with the norm
theta1norm = pi/2 - theta1;
%v2
v2 = [0.3214 -0.6788];
%theta2 with the x axis
theta2 = acos((v2(1)*L(1)+v2(2)*L(2))/(sqrt(v2(1)^2+v2(2)^2)*sqrt(L(1)^2+L(2)^2)));
%theta1 with the norm
theta2norm = pi/2 - theta2;
My question here is that the product produces both angles to be equal, i.e., . However, and as shwon in the figure, it is clear that , i.e., and . How can I caulcate these angles correctly using the dot product?
Thanks.
  1 Kommentar
Dyuman Joshi
Dyuman Joshi am 4 Jan. 2024
"However, and as shwon in the figure, it is clear that Theta2n = Theta1N + pi/2"
How exactly is that clear or shown?
"How can I caulcate these angles correctly using the dot product?"
Utilize these functions - dot, norm

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Hassaan
Hassaan am 4 Jan. 2024
% Close all figures and clear variables
close all;
clear all;
% Define the reference vector
L = [-0.5 -0.5];
% Define the first vector v1 and calculate the angle with L
v1 = [-0.6788 0.3214];
% Dot product of v1 and L
dot_v1_L = dot(v1, L);
% Norms of v1 and L
norm_v1 = norm(v1);
norm_L = norm(L);
% Angle theta1 with the x axis
theta1 = acos(dot_v1_L / (norm_v1 * norm_L));
% Angle theta1 with the norm
theta1norm = pi/2 - theta1;
% Define the second vector v2 and calculate the angle with L
v2 = [0.3214 -0.6788];
% Dot product of v2 and L
dot_v2_L = dot(v2, L);
% Norms of v2
norm_v2 = norm(v2);
% Angle theta2 with the x axis
theta2 = acos(dot_v2_L / (norm_v2 * norm_L));
% Angle theta2 with the norm
theta2norm = pi/2 - theta2;
% Convert angles to degrees
theta1_degree = radtodeg(theta1);
theta2_degree = radtodeg(theta2);
theta1norm_degree = radtodeg(theta1norm);
theta2norm_degree = radtodeg(theta2norm);
% Display the results
disp(['Theta1: ', num2str(theta1_degree), ' degrees']);
Theta1: 70.3367 degrees
disp(['Theta2: ', num2str(theta2_degree), ' degrees']);
Theta2: 70.3367 degrees
disp(['Theta1 from the norm: ', num2str(theta1norm_degree), ' degrees']);
Theta1 from the norm: 19.6633 degrees
disp(['Theta2 from the norm: ', num2str(theta2norm_degree), ' degrees']);
Theta2 from the norm: 19.6633 degrees
The functions dot and norm are used to calculate the dot product and the magnitude of the vectors, respectively, and acos computes the arccosine of the given value to obtain the angle in radians. The function radtodeg converts the angle from radians to degrees.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering

James Tursa
James Tursa am 4 Jan. 2024
Bearbeitet: James Tursa am 4 Jan. 2024
Also see this related link for a robust method using atan2 that can recover small angles:

Torsten
Torsten am 4 Jan. 2024
I think an intuitive way is to compute the angles between the positive x-axis and the respective vector counterclockwise first (the result will be between 0 and 360) and then make the necessary subtractions.
theta1 = cart2pol(L(1)/norm(L),L(2)/norm(L))*180/pi
if theta1 <=0
theta1 = 360 + theta1;
end
theta2 = cart2pol(v1(1)/norm(v1),v1(2)/norm(v1))*180/pi
if theta2 <=0
theta2 = 360 + theta2;
end
theta3 = cart2pol(v2(1)/norm(v2),v2(2)/norm(v2))*180/pi
if theta3 <=0
theta3 = 360 + theta3;
end

Sinazo
Sinazo am 6 Sep. 2025 um 15:54
Diese(r) Antwort wurde durch Dyuman Joshi markiert
To evaluate the scalar product and angle between two vectors using MATLAB. Let vector A = 3ûₓ + 4ûᵧ and vector B = 12ûₓ + 5ûᵧ. Use these vectors to perform the scalar product and angle calculations.
  1 Kommentar
Torsten
Torsten am 7 Sep. 2025 um 12:00
Bearbeitet: Torsten am 7 Sep. 2025 um 12:05
A = [3;4];
B = [12;5];
sp = A.'*B;
nA = norm(A);
nB = norm(B);
ang = acos(sp/(nA*nB))*180/pi
ang = 30.5102
A1 = [1;0];
sp1 = A1.'*B;
ang_lower = acos(sp1/(1*nB))*180/pi
ang_lower = 22.6199
B2 = [0;1];
sp2 = A.'*B2;
ang_upper = acos(sp2/(nA*1))*180/pi
ang_upper = 36.8699
ang_lower+ang+ang_upper
ans = 90.0000

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Stress and Strain finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by