Angle between a vector and the positive X axis

조회 수: 44 (최근 30일)
Neena
Neena 2012년 6월 4일
편집: Walter Roberson 2020년 8월 14일
Hi all,
I wish to find the angle between a vector that has the equation y=m*x+c and the positive X axis. Is there a way in MATLAB that I can do it. I get the values for m and c using polyfit. The slope m doesnt always give the angle with reference to the positive X axis. I was wondering if there is an easier way in MATLAB to do this.
Your help is appreciated.

답변 (4개)

Steve Eddins
Steve Eddins 2012년 6월 4일
Try this:
atan(m)
If you need the answer in degrees, then:
atan(m) * 180 / pi
  댓글 수: 1
Neena
Neena 2012년 6월 4일
편집: Walter Roberson 2020년 8월 14일
Hi Steve,
The reason why I want to know this is that I want to rotate the vector by the angle so that its direction is along the positive X axis. I use atan(m) for the analysis. But sometimes it gives me errorenous results. Take for instance this example. (I have given my code as well)
X=[0 -0.1705 -0.1630 -0.0060 -0.0308];
Y=[0 -1.0382 -2.2907 -3.2725 -3.6321];
x1=[X;Y];
poly=polyfit(x1(1,1:4),x1(2,1:4),1); %the vector is the line that fits the first four points
theta=atan(poly(1));
%this is the way I find the correct angle of rotation.
sumx=sum(x1(1,:));
if (sumx>0)
theta=-theta;
else
theta=-(pi+theta);
end
%computation of rotation angle
rot=[cos(theta) -sin(theta); sin(theta) cos(theta)];
x2=rot*x1;
plot(x1(1,:),x1(2,:))
hold on
plot(x2(1,:),x2(2,:),'*r')
even though the vector points along the negative X direction, atan gives an angle estimate of about 20 degrees. Can you please tell me what I am doing wrong. I want x1 to point along the positive X axis after rotation.

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


oblivious
oblivious 2012년 6월 4일
you might want to consider all possible combinations i.e. 1st and 2nd quadrant.
b=atan(m);
if b<0
ang=pi+b;
else ang=b;
end
  댓글 수: 2
oblivious
oblivious 2012년 6월 4일
answer is in radian
Neena
Neena 2012년 6월 4일
See my comment to the above answer. Thanks for the help

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


Walter Roberson
Walter Roberson 2012년 7월 8일

Ryan seader
Ryan seader 2020년 8월 14일
You decide to find a resultant of more than just two forces by resolving each individual force into its rectangular components and then combining them to find the rectangular components of the resultant force. You do not want to use the triangle rule with the law of sines and cosines, because you would need to do it first for the first two vectors and then again to combine the third vector with the resultant of the first two. The following steps can be arranged to allow for the determination of the magnitude and direction of the resultant force of multiple forces using their rectangular components. Give the correct order in which to implement each step, assuming that you are going to calculate the magnitude of the resultant first before you calculate its direction.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by