# Angle between two vectors is computed weirdly!

조회 수: 1 (최근 30일)
댓글: Lama Hamadeh 2021년 4월 20일
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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Hrishikesh Borate 2021년 4월 20일
편집: Hrishikesh Borate 2021년 4월 20일
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
Great! Thanks!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by