How to Calculate Inverse Cosine in MATLAB

Ammar Ali Feb 12, 2024
  1. Unveiling the MATLAB acos() Function
  2. Find the Inverse Cosine Using MATLAB acos() and acosd() Functions
  3. Practical Application of MATLAB acos() Function
  4. Handling Special Cases
  5. Visualization and Plotting
  6. Conclusion
How to Calculate Inverse Cosine in MATLAB

MATLAB, a robust numerical computing environment, houses an array of mathematical functions catering to various needs. Among these, the acos() function stands out as a crucial tool for trigonometric calculations, specifically addressing the inverse cosine operation.

In this article, we will delve into the intricacies of the acos() function, exploring its syntax, applications, and insights into the world of inverse trigonometry. We will also discuss finding the inverse cosine of value using the acos() and acosd() functions in MATLAB.

Unveiling the MATLAB acos() Function

The acos() function in MATLAB is designed to calculate the inverse cosine, providing a way to determine an angle whose cosine equals a given value. This function is particularly useful in scenarios where one needs to find angles in a right-angled triangle when the lengths of its sides are known.

The basic syntax of the acos() function is as follows:

y = acos(x)
  • x: The cosine value for which the angle is calculated.
  • y: The result, which is the angle in radians whose cosine is equal to x.

Find the Inverse Cosine Using MATLAB acos() and acosd() Functions

We can use the acos() and acosd() functions of MATLAB to find the inverse cosine of a value. The acos() function returns the inverse cosine of a value in radians, and the acosd() function returns the inverse cosine of a value in degrees.

The acos() and acosd() function accepts inputs in real and complex forms. If the input value is real and falls within a -1 to 1 interval, the function will return values within the interval of 0 to pi.

If the input value is real and falls outside the interval of -1 to 1 and for complex values, the function will return complex values. For example, let’s find the inverse cosine of a value in radians and degrees.

See the code below:

clc
clear

radian_out = acos(0.5)
degree_out = acosd(0.5)

Output:

radian_out = 1.0472
degree_out = 60.000

In the above code, we used a 0.5 value, and we can see in the output that the result is returned both in radians and degrees. We can also find the inverse cosine of a vector containing multiple values, and the values can be real or complex.

The acos() and acosd() will return a vector containing the inverse cosine of each value present in the input vector. For example, let’s create a vector with multiple values and find the inverse cosine of each value.

See the code below:

clc
clear

values = [0 0.5 0.7 0.9]
radian_out = acos(values)
degree_out = acosd(values)

Output:

values =

   0.00000   0.50000   0.70000   0.90000

radian_out =

   1.57080   1.04720   0.79540   0.45103

degree_out =

   90.000   60.000   45.573   25.842

Each value is returned in radians and degrees in the above output. The function’s input can be a scalar, vector, matrix, or multidimensional array of real or complex values.

Practical Application of MATLAB acos() Function

Let’s explore a few practical examples to illustrate the application of the acos() function.

Example 1: Calculating Inverse Cosine

cosine_value = 0.5;
angle_radians = acos(cosine_value);

In this example, the acos() function calculates the angle in radians whose cosine is 0.5. The result is stored in the variable angle_radians.

Note that the result is in radians, as MATLAB’s trigonometric functions inherently work with radians.

Example 2: Vectorized Operation

cosine_values = [1, 0, -0.5];
angles_radians = acos(cosine_values);

Here, the acos() function operates on a vector of cosine values. The resulting angles_radians vector contains the corresponding angles in radians.

Conversion to Degrees

While the acos() function returns angles in radians, MATLAB provides the rad2deg() function for converting radians to degrees.

angle_degrees = rad2deg(angle_radians);

This line converts the angle from radians to degrees, providing a more intuitive representation for users accustomed to degrees.

Handling Special Cases

The acos() function returns real values for cosine values within the range [-1, 1]. However, values outside this range return complex results.

To handle such cases, users should check for the validity of the input or use the real() function to extract the real part of the result.

cosine_value = 1.5;
angle_radians = acos(cosine_value);

if isreal(angle_radians)
    disp(['Angle in radians: ', num2str(angle_radians)]);
else
    disp('Invalid input: Cosine value must be within [-1, 1].');
end

Visualization and Plotting

Visualizing the output of the acos() function can aid in understanding the relationship between cosine values and angles. MATLAB’s plotting capabilities can be leveraged to create informative visualizations.

cosine_values = linspace(-1, 1, 1000);
angles_radians = acos(cosine_values);

plot(cosine_values, rad2deg(angles_radians));
xlabel('Cosine Value');
ylabel('Angle (Degrees)');
title('Inverse Cosine Function: Cosine Value vs. Angle');

Output:

matlab acos function

This example generates a plot that shows the relationship between cosine values and angles obtained from the acos() function.

Conclusion

The acos() function in MATLAB serves as a versatile tool for inverse trigonometric calculations, providing a straightforward way to find angles corresponding to given cosine values. Whether used for single values or vectorized operations, understanding the nuances of the acos() function enhances its effective application in mathematical computations and scientific analyses.

As users navigate MATLAB’s trigonometric functions, the acos() function stands as a valuable asset, contributing to the flexibility and power of MATLAB in handling a diverse range of mathematical problems.

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