Academic Block | Stay Coded

Academic Block logo

Basic Trigonometry Operations in MATLAB

MATLAB provides a variety of built-in trigonometric functions to perform operations such as calculating sine, cosine, tangent, and their inverses. These functions work with both degrees and radians, making them versatile for different types of problems.

Basic Concepts

In MATLAB, trigonometric functions operate on angles provided in radians by default. To work with degrees, you can use the degree-specific functions or convert degrees to radians using deg2rad.

% Basic trigonometric functions
theta = pi/4; % Angle in radians
sin_val = sin(theta);
cos_val = cos(theta);
tan_val = tan(theta);

disp(sin_val); % Output: 0.7071
disp(cos_val); % Output: 0.7071
disp(tan_val); % Output: 1.0000

You can use deg2rad to convert angles in degrees to radians:

% Conversion from degrees to radians
theta_deg = 45;
theta_rad = deg2rad(theta_deg);
sin_val = sin(theta_rad);
disp(sin_val); % Output: 0.7071

Alternatively, you can also use sind, and cosd to accept input angles directly in degrees. However, users should note that there are reported cases of these functions not working as desired.

% Using input angles directly as degrees
theta_deg = 45;
sin_val = sind(theta_deg);
cos_val = cosd(theta_deg);
tan_val = tand(theta_deg);
disp(sin_val);
disp(cos_val);
disp(tan_val);

Inverse Trigonometric Functions

MATLAB also provides functions to compute the inverse of trigonometric values. The output is in radians:

% Inverse trigonometric functions
x = 0.7071;
angle_rad = asin(x);
angle_deg = rad2deg(angle_rad);
disp(angle_rad); % Output: 0.7854 (radians)
disp(angle_deg); % Output: 45 (degrees)

Handling Complex Results

Trigonometric functions in MATLAB can handle complex numbers, providing results in the complex plane:

% Trigonometric functions with complex numbers
z = 1 + 1i; % Complex number
sin_z = sin(z);
disp(sin_z); % Output: 1.2985 + 0.6349i

Common Trigonometric Operations

Hyperbolic Trigonometry

MATLAB supports hyperbolic trigonometric functions:

% Hyperbolic sine and cosine
x = 1;
sinh_val = sinh(x);
cosh_val = cosh(x);
disp(sinh_val); % Output: 1.1752
disp(cosh_val); % Output: 1.5431

Finding Angles

Using atan2 to find angles in the range [-π, π]:

% Finding angles with atan2
y = 1;
x = 1;
angle = atan2(y, x);
disp(angle); % Output: 0.7854 (radians)

Useful MATLAB Functions for Trigonometry

Function
Explanation
sin
A = sin(B) will output the sine value of ‘B’. If the input B is an array, then sin(B) will output the element wise sine values of the array.
cos
A = cos(B) will output the cosine value of ‘B’. If the input B is an array, then cos(B) will output the element wise cosine values of the array.
tan
A = tan(B) will output the tangent of ‘B’. If the input B is an array, then tan(B) will output the element wise tangent values of the array.
asin
A = asin(B) Computes the inverse sine (arcsin) of a value.
acos
A = acos(B) Computes the inverse cosine (arccos) of a value.
atan
A = atan(B) Computes the inverse tangent (arctan) of a value.
atan2
A = atan2(B,C) Computes the four-quadrant inverse tangent.
deg2rad
A = deg2rad(B) Converts degrees to radians.
rad2deg
A = rad2deg(B) Converts radians to degrees.

Practice Questions

Test Yourself

1. Find the sine, cosine, and tangent of a 30-degree angle.

2. Compute the angle (in degrees) whose tangent is 1.

3. Use atan2 to find the angle for y = -1 and x = -1.