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.
- Category:
- Picks
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.