How to convert radians to degrees?

Hi, I have this code,
clear all, close all, clc,
r = 0.08;
t = 2;
for n = 1:180,
M(1,n) = r*cos(t); %x length from center
M(2,n) = r*sin(t); %y length from center
t = t+2;
end
It is meant to find the x,y distance from the center of a circle with fixed radius (Basically calculating x,y coordinates of circumference).
However the coordinates are coming wrong and it is because Matlab is performing calculations in radians rather than in degrees. (I cross checked the values with calculator) And I don't know how to convert to radians, and none of the forum results made sense to me.
Thanks, Rohan

답변 (3개)

José-Luis
José-Luis 2013년 5월 21일
편집: José-Luis 2013년 5월 21일

2 개 추천

There is a built in function for that:
doc deg2rad
But it is rather trivial to implement it yourself:
myTrans = @(x) x./360.*(2.*pi());
myTrans(180)
ans =
3.1416

댓글 수: 6

To avoid two scalar * vector multiplies, group the constant scalars:
x * (180/pi()); % rad --> deg
x * (pi()/180); % deg --> rad
José-Luis
José-Luis 2013년 5월 21일
Thanks. I was just trying to make the formula explicit though.
Rohan Repale
Rohan Repale 2013년 5월 21일
I figured out, i had to use 'sind' and 'cosd'. Thank you all for your replying.
James Tursa
James Tursa 2013년 5월 21일
No, you did not have to use sind and cosd. They work, but the many other suggestions on this thread using sin and cos correctly also work.
Rohan Repale
Rohan Repale 2013년 5월 21일
@James, mathematically speaking the other suggestions must work as well, but the output was not matching my expected output. But when I used sind and cosd, it worked perfect. Maybe I was not doing it right. Anyways thanks for the help
deg2rad
It does not work without toolbox > geo > private > deg2rad.m http://mooring.ucsd.edu/software/matlab/doc/toolbox/geo/private/deg2rad.html

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

Iain
Iain 2013년 5월 21일

0 개 추천

Degrees = 180 * radians / pi.
Rearrange as needed ;)

댓글 수: 6

To avoid two scalar * vector multiplies, group the constant scalars:
Degrees = radians * (180 / pi);
Rohan Repale
Rohan Repale 2013년 5월 21일
I know the formula, Its not working right. IS there any code to convert the units (radtodeg doesn't seem to be working) ? Or can the units be set through settings/preferences?
Iain
Iain 2013년 5월 21일
Put the conversion in the right place(s)?
Rohan Repale
Rohan Repale 2013년 5월 21일
hi James, I just tried it. It doesnt seem to work either.
Iain
Iain 2013년 5월 21일
Radians = degrees * (pi/180)
"t" is in degrees, and you want it in radians when put into cos and sin.
cos(t * (pi/180)) and sin(t * (pi/180)) is the conversion rearranged as needed, AND put in the right place...
José-Luis
José-Luis 2013년 5월 21일
Doesn't seem to work... How? Please show what you mean.

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

제품

질문:

2013년 5월 21일

댓글:

2014년 11월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by