# Angle between two vectors is computed weirdly!

1 vue (au cours des 30 derniers jours)
Lama Hamadeh le 9 Avr 2021
Commenté : Lama Hamadeh le 20 Avr 2021
Hi all,
I am trying to compute the angle between line L1v and the verticle norm Nv via the dot product using the follwoing code. However, I can see that the resulting angle is comouted between the xaxis (the horizontal norm) rather than the verticle and I can't see why. If you can run the follwoing piece of code you can see wha tI mean.
close all;
clear all;
%boundary length
L = 1;
%number of points on S axis
ns = 25;
%boundary variable
s = linspace(0,L,ns);
%construct coordinates meshgrid
[X,Y] = meshgrid(s,s);
% mesh needs X,Y and Z so create z
Z = zeros(size(X));
%Visualise the grid
figure;
mesh(X,Y,Z,'Marker','o','EdgeColor',"k") %or surf
axis equal tight
view(2)
set(gca,'ytick',[])
xlabel('\$S\$','Interpreter','latex')
set(gca,'TickLabelInterpreter','latex')
set(gca,'FontSize',16)
hold on
% %-----------------------------
%Compute first line equation | L1: Ax + By + C = 0
%first line coordinates
x1L1 = 0.5; y1L1 = 0;
x2L1 = 1 ; y2L1 = 0.1;
%Setting the determinant to be zero to get the line's standrad formula
A = y1L1 - y2L1;
B = x2L1 - x1L1;
C = x1L1*y2L1 - x2L1*y1L1; %or C=-A*x1-B*y1
%L1 stamdard line equation
L1 = (-C/B)+(-A/B)*s; % -A/B is the gradient and -C/B is the intercept
%plotting on the grid
plot(s,L1,'b','LineWidth',2)
axis([min(s) max(s) min(s) max(s)])
%find the angle between L1 and the norm N
L1v = [x2L1,y2L1] - [x1L1,y1L1]; %compute the x and y projections of L1
%defining the vertical norm
%pos1 = randi(length(s)); vnorm = s(pos1);
vnorm = x1L1;
Nvx = [vnorm 0]; %the verticle normal vector
%plot the verticle norm line
y=get(gca,'ylim');
hold on
plot([vnorm vnorm],y,'g','LineWidth',2)
%dot product between L1v and Nvx to get the angle
theta1 = acos((L1v(1,1) * Nvx(1,1)+L1v(1,2) * Nvx(1,2))/...
(sqrt((L1v(1,1))^2+(L1v(1,2))^2)*sqrt((Nvx(1,1))^2+(Nvx(1,2))^2)));
%show results on the screen
fprintf('The angle between the first line and the vertical norm: theta1=%g\n',...
theta1*(180/pi)');
Any comments on that would be appreicted!
Thanks.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Hrishikesh Borate le 20 Avr 2021
Modifié(e) : Hrishikesh Borate le 20 Avr 2021
Hi,
It’s my understanding that you are trying to find angle between line L1v and vertical normal Nv. This can be achieved by modifying the assignment of Nvx as follows :-
Nvx = [0 vnorm]; %the vertical normal vector
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Lama Hamadeh le 20 Avr 2021
Great! Thanks!

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by