File Exchange Pick of the Week

November 27th, 2009

degrees and radians

Bob's pick this week is degrees and radians by Richard Medlock.

These two helper functions are simple but useful. I don't know how many times I have needed to convert between radians and degrees over the years. (Hint: a lot!) For example, sometimes defining a frequency value in Hertz is more convenient for humans, but conversion to radian frequency is generally needed by the computer at some point.

f0 = 60;         %frequency (Hz)
w0 = f0*pi/180;  %natural frequency (rad)

The conversion factor is not difficult, but it can be a workflow distraction nonetheless. Hence, Richard's utility functions to do the conversion for you.

radians(180)
ans =
       3.1416
degrees(pi/3)
ans =
           60

I tend to dislike magic numbers, particularly in complex calculations. So I can see helper functions like these making code more readable. Magic numbers that appear many times throughout a program can also be prone to logical errors if I mistype a value somewhere. Those types of problems can be difficult to track down. Also, having conversion code centralized in one place can pay dividends in reusability and testability.

Comments?


Get the MATLAB code

Published with MATLAB® 7.10

12 Responses to “degrees and radians”

  1. Kunert replied on :

    Hello,

    I prefer (and suggest) the function names
    - deg2rad
    - rad2deg

    In my experience, they tell by name what the functions do (at least more clearly)

    Best regards,

    Gerd

  2. Petter replied on :

    This must be the worst pick of the week ever. Degree to radians conversion? Seriously?

    I agree with Kunert that the functions should have more logical names, too.

  3. Petter replied on :

    One more thing: at least one Mathworks toolbox already has a function called “deg2rad”. It can be found in toolbox/map/map.

  4. Mark Andrews replied on :

    At the risk of sounding heretical, I like the way Mathematica does it, thanks to its implied multiplication. Thus, with Degrees defined as pi / 180, you can write

    x = Sin[ 30 Degrees ]
    

    Petter: I don’t mind revisiting the basics now and again. And who’s to say an absolute beginner won’t stumble upon this post and find it useful?

  5. Peter replied on :

    I personally really like the convension of simply defining the symbol deg=pi/180, that way you can write your code in the “units” and not care about the underlying calculations being in radians. I do this for many different units, and in most scripts go as far as to define m=1 so I can define lengths writing arm=4*m; that way someone reading my code shouldn’t be confuesed as to the units used.

    Mark Andrews: I also think Mathematica has it dead on, expecially because you can write esc-deg-esc, and it will simply show a degrees symbol, I know many hate 2dmath with a passion, but why should we through away milleniums worth of mathematical syntax simply because it’s not unicode

  6. Daniel Armyr replied on :

    Yeah, I think this post and the comments hilight a limitetaion of matlab as a physics modelling tool. In physics, numbers are nothing without their units.

    I am looking forward to the day someone writes a dataclass that handles units correctly and efficiently. Now that will be a worthy pick of the week.

    On the other hand, I am a big fan of small convenient functions. x=flat(x) { x=x(:) } is probably my favourite so far.

  7. Bob replied on :

    Thanks for all the comments, everyone!

  8. Marc replied on :

    This functionality is also in the circular statistics toolbox. And yes, simple conversions can all too easily distract from workflow.

  9. Naor replied on :

    Daniel,
    I always use this:
    http://www.mathworks.com/matlabcentral/fileexchange/13018-physunits-module-from-fortran

    whenever I do physics based calculations in MATLAB. Efficiency is a problem though so there is a switch to “turn off” dimensional awareness and revert to pure numbers.
    (Note: degrees and/or radians are dimensionless units.)

    Hope that helps,
    -n

  10. Cliff Sedlund replied on :

    Are you serious with this example? It’s got several errors and doesn’t illustrate the topic–conversions between radians and degrees.

    Frequency is measured in Hz, which is equivalent to cycles per second, analogous to revolutions per second, or in radians per second (not radians!). Since there are 2*pi radians in a cycle (revolution), the conversion from Hz to rad/sec is 2*pi. There are no degrees involved in this example, although if you wanted to convert a frequency in deg/sec to rad/sec, then you could use the functions described here.

  11. Bob replied on :

    Cliff, you are correct. It was a bad example. Motor speed, for example, is usually in Hz or RPM. Phase angle, on the other hand, is more intuitive in degrees.

    Honestly, that was a late change to my post in an (apparently failed) attempt to motivate Richard’s submission. Our readership is very diverse after all.

    As you probably surmised, the last paragraph (excuse to touch on code hygiene) was more on my mind.

    I will leave the post as is. Thanks for the note!

  12. Richard replied on :

    lol, well that provoked some responses. I actually agree with Kunert that the functions may be better as deg2rad and rad2deg, there was a specific reason why I chose to use the full names (something to do with another language or function I was also using at the time) but I can’t remember it now.

    I disagree that Matlab should use unit symbols I think the variations are too wide to make it really practical and it’s far easier to manipulate numbers unit free, I mean that might work of for degree symbols, but most units are simply letters – Think of weights and measures for example – they could easily be mistaken for variables.

    I would also take issue with the comments about the inclusion of the function with other toolboxes. That’s great if you have those toolboxes, but useless if, like me, you don’t.

    There’s been a bit of bashing about these two functions, but I would still find them useful myself, even now, several years after I first wrote them. I don’t like repetitive code, especially for calculations so for me external functions make things more usable and more maintainable, but then – I am a little biased :)

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.