MATLAB Answers

Angle between two vectors is computed weirdly!

1 view (last 30 days)
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
mesh(X,Y,Z,'Marker','o','EdgeColor',"k") %or surf
axis equal tight
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
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
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))/...
%show results on the screen
fprintf('The angle between the first line and the vertical norm: theta1=%g\n',...
Any comments on that would be appreicted!

Accepted Answer

Hrishikesh Borate
Hrishikesh Borate on 20 Apr 2021
Edited: Hrishikesh Borate on 20 Apr 2021
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!