MATLAB Deg2rad Function

Ammar Ali Feb 15, 2024
  1. Understanding Degrees and Radians
  2. Syntax of the deg2rad() Function in MATLAB
  3. Conclusion
MATLAB Deg2rad Function

MATLAB, short for Matrix Laboratory, is a numerical computing environment and programming language widely used in various fields such as engineering, mathematics, and scientific research.

Among its many built-in functions, deg2rad() stands out as a handy tool for converting angles from degrees to radians. This function simplifies mathematical computations involving trigonometric functions where angles are often expressed in radians.

In this article, we’ll delve into the details of how to effectively use the deg2rad() function in MATLAB.

Understanding Degrees and Radians

Before diving into the usage of deg2rad(), it’s crucial to understand the fundamental difference between degrees and radians.

Degrees and radians are two units for measuring angles. While degrees are more commonly used in everyday contexts, radians are preferred in mathematical calculations due to their simplicity and their close relationship with the unit circle.

In the radian system, an angle of one radian subtends an arc on the unit circle equal in length to the radius of the circle. Hence, a complete revolution around a circle equals radians, which is approximately 360 degrees.

Syntax of the deg2rad() Function in MATLAB

The deg2rad() function in MATLAB follows a simple syntax:

rad = deg2rad(deg)

Here, deg represents the angle in degrees that you wish to convert to radians, and rad is the variable where the converted angle in radians will be stored. The input angle deg can be a scalar, vector, matrix, or array of any dimension, providing versatility in its usage.

When you invoke the deg2rad() function with an angle in degrees as its argument, MATLAB internally applies the conversion factor (π/180) to transform the input angle into radians. The resulting value is then assigned to the specified variable rad.

This process ensures consistency in units, enabling seamless integration with MATLAB’s mathematical functions and plotting capabilities.

Let’s explore some practical examples of using the deg2rad() function in various scenarios.

Example 1: Converting a Single Angle From Degrees to Radians Using the deg2rad() Function

Let’s start with a simple example where we have a single angle in degrees, and we want to convert it to radians.

angle_deg = 45;
angle_rad = deg2rad(angle_deg)

In this example, we start by defining a single angle in degrees as angle_deg = 45. Then, we utilize the deg2rad() function to convert this angle from degrees to radians.

This conversion is achieved by simply passing the angle in degrees (angle_deg) as an argument to the deg2rad() function. The resulting angle in radians is stored in the variable angle_rad.

Finally, we use the disp() function to display the converted angle in radians.

Output:

angle_rad =  0.7854

Example 2: Converting Multiple Angles From Degrees to Radians in a Vector Using the deg2rad() Function

Often, we may need to convert multiple angles from degrees to radians simultaneously. MATLAB allows us to do this efficiently using vectorization. Here’s an example:

angles_deg = [30, 60, 90];
angles_rad = deg2rad(angles_deg)

Here, we create a vector angles_deg containing three angles in degrees: [30, 60, 90]. By invoking the deg2rad() function with this vector as its argument, MATLAB automatically converts all the angles in the vector from degrees to radians simultaneously.

The resulting radians are stored in the variable angles_rad. Then, we use disp() to display the converted angles in radians.

Output:

angles_rad =

   0.52360   1.04720   1.57080

Example 3: Plotting Trigonometric Functions With Angles in Radians

Visualizing trigonometric functions requires angles to be represented in radians. Let’s plot the sine function using angles in radians:

theta_deg = 0:10:360;
theta_rad = deg2rad(theta_deg);
y = sin(theta_rad);
plot(theta_rad, y);
xlabel('Angle (radians)');
ylabel('sin(theta)');
title('Plot of sin(theta) with Angles in Radians');

This example involves plotting the sine function with angles specified in radians.

We first generate a range of angles from 0 to 360 degrees with a step size of 10, using theta_deg = 0:10:360. Then, we convert these angles to radians using deg2rad(theta_deg).

The resulting radian angles are stored in theta_rad. Next, we compute the sine values (y = sin(theta_rad)) corresponding to these angles.

Finally, we plot the sine function against the radian angles (theta_rad) using plot(). The plot is then labeled appropriately using xlabel(), ylabel(), and title() functions.

Output:

Plotting Trigonometric Functions With Angles in Radians

Example 4: Applying Trigonometric Functions to Angle Matrices

MATLAB also supports matrix operations, making it easy to work with matrices of angles. Consider the following example:

angle_matrix_deg = [30, 45, 60; 90, 120, 150];
angle_matrix_rad = deg2rad(angle_matrix_deg);
sine_matrix = sin(angle_matrix_rad)

In this example, we define a 2x3 matrix angle_matrix_deg containing angles in degrees. We then apply the deg2rad() function to convert all the angles in the matrix from degrees to radians, resulting in angle_matrix_rad.

After the conversion, we compute the sine values for each angle in the matrix using the sin() function. The resulting sine values are stored in the sine_matrix.

Finally, we display the computed sine values using disp().

Output:

sine_matrix =

   0.50000   0.70711   0.86603
   1.00000   0.86603   0.50000

These examples illustrate the versatility and utility of the deg2rad() function in MATLAB. Whether you’re dealing with single angles, vectors, matrices, or plotting trigonometric functions, deg2rad() simplifies the conversion process, ensuring consistency and accuracy in your calculations.

Recommendations and Considerations When Using the deg2rad() Function in MATLAB

It’s worth noting the historical context and recommendations regarding angle conversion functions in MATLAB.

The deg2rad() function was introduced in MATLAB version R2015b to replace the older degtorad() function, which had been available since MATLAB version R2009b.

While both functions serve the same purpose, MATLAB now recommends using deg2rad() over degtorad() for versions R2015b or later. However, users with versions before R2015b can still utilize degtorad().

One crucial distinction between the two functions lies in the data types they accept. degtorad() accepts inputs in integer data types, while deg2rad() accepts inputs in single and double data types.

To convert integer data types such as uint8 to single or double, MATLAB provides the single() and double() functions.

In scenarios where the input angle is in an integer data type like uint8, it’s necessary to convert the input to a single or double data type before utilizing the deg2rad() function. This ensures compatibility with the function’s input requirements.

If degtorad() were used in such cases, the output would remain the same, but it’s important to consider the recommended approach for version compatibility and future-proofing of code.

Conclusion

In MATLAB, the deg2rad() function serves as a convenient tool for converting angles from degrees to radians. Whether you’re working on trigonometric computations, geometry, or any other field involving angular measurements, this function simplifies the conversion process, enabling you to seamlessly integrate degrees and radians in your calculations.

By understanding its syntax and examples, you can leverage the deg2rad() function effectively in your MATLAB programs, enhancing the efficiency and accuracy of your mathematical operations.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook