Searching for math expert! angle of vectors in a loop using law of cosines
1 次查看(过去 30 天)
显示 更早的评论
Hello,
I created a loop to get each angle in between two vektors as you can see in the picture. My aim is to get the angle on each sixth point as shwon in the diagramm.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/157832/image.png)
Using the follwing scipt, my resulting angle is always 90°! I can not see the mistake! Probably you do!?
alphaans=int16.empty(1,length(x1),0);
for i = 1:(length(x1)-2)
ax(i)=x1(1,i+1)-x1(1,i);
ay(i)=y1(1,i+1)-y1(1,i);
bx(i)=x1(1,i+2)-x1(1,i+1); %second (b) vector
by(i)=y1(1,i+2)-y1(1,i+1);
cx(i)=x1(1,i)+ax(i)+bx(i); %resulting, leading to third vector
cy(i)=y1(1,i)+ay(i)+by(i);
lcx(i)=x1(1,i+2)-x1(1,i); %third (c) vector
lcy(i)=x1(1,i+2)-x1(1,i);
Ba(i)=sqrt((ax(1,i)^2+ay(1,i)^2)); % norm (a)
Bb(i)=sqrt((bx(1,i)^2+by(1,i)^2)); % norm (b)
Bc(i)=sqrt((lcx(1,i)^2+lcy(1,i)^2)); % norm (c)
alpha(i)=acosd(((Bb(1,i))^2+(Ba(1,i))^2-(Bc(1,i))^2)/(2.*Ba(1,i).*Bb(1,i))); %law of cosines
alphaans(1,i,1)=alpha(i); %put each angle in the array alphaans
end
Thanks!
5 个评论
Mostafa
2016-11-9
I think you only need to replace each instance of i+6 with i+1 and i+12 with i+2
Cheers.
采纳的回答
Thorsten
2016-11-9
编辑:Thorsten
2016-11-9
% define sample values
x = [1234.77 936.40 681.39 516.59 355.26 82.90];
y = [241.90 155.16 118.73 193.32 408.43 458.74];
% plot values
y = 500 - y; % subtract to make y axis pointing upwards, just for display
% purposes
plot(x, y, 'ko-')
axis equal
grid on
box off
% get angle between successive line elements
vec = [diff(x)' diff(y)'];
for i = 1:size(vec, 1) - 1
u = vec(i,:);
v = vec(i+1,:);
theta(i) = acos( (u * v')/(norm(u)*norm(v)) );
% u * v' is the dot product between u and v
end
rad2deg(theta)
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!