8 Wrapping values to a range
In many engineering applications we want to measure a direction. For example, Google Maps on your smart phone will show you what direction your phone is pointing in.
A direction can be represented as an angle between \(-360\) (exclusive) and \(360\) degrees (exclusive). Angular values greater than or equal to \(360\) degrees are wrapped around; for example, \(361\) degrees gets wrapped around to \(1\) degree. Angular values less than \(-360\) degrees are also wrapped around; for example, \(-365\) degrees gets wrapped around to \(-5\) degrees. Angular values of \(+360\) or \(-360\) degrees are wrapped to \(0\) degrees. The sign of the wrapped angle is always the same as the sign of the input angle: The wrapped value of a positive angle is also a positive angle and the wrapped value of a negative angle is also a negative angle.
In MATLAB, open the file wrapAngle.m
. This file contains a user-defined function that when completed returns an angle wrapped to the range \(-360\) degrees and \(360\) degrees given an input angle.
To implement this function, you should compute a remainder after division. For example, given the angle \(725\) degrees, you would compute the wrapped angle as the remainder of dividing \(725\) by \(360\) (\(\frac{725}{360} = 2\) with a remainder of \(5\)).
It turns out that many engineering problems require you to compute a remainder. MATLAB has two functions that perform such a calculation: mod
and rem
. Read the help
documentation for these functions and decide which one is appropriate for this problem. Complete the wrapAngle
function using the appropriate method.
8.1 Using wrapAngle.m
Test your function by typing the following in the MATLAB Command Window:
ang = -365;
y = wrapAngle(ang)
A correct function would cause the value -5
to be output. If the correct value is not output, review your function and correct any errors that you find.
Test your function using different values for the price; for example, type following statements in the MATLAB Command Window and verify that the outputs are correct:
wrapAngle(0)
wrapAngle(-361)
wrapAngle(361)