KAHIBARO Bookshelves
Discord Login Register

7.3. Trigonometric and Exponential Functions

Overview

In MATLAB you have direct access to a rich set of trigonometric and exponential functions that operate efficiently on scalars, vectors, and matrices. This chapter focuses on what these functions are, how they behave in MATLAB, and how to use them correctly and safely in basic computations. You will see how standard trigonometric functions relate to degrees and radians, how inverse and hyperbolic variants work, and how exponential and logarithmic functions are expressed in MATLAB.

Trigonometric Functions in Radians

MATLAB’s basic trigonometric functions are sin, cos, and tan. By default they all use radians as the angle unit. If you call these functions with numbers that you intend to be in degrees, you must convert them to radians yourself or use the degree-specific variants.

The radian measure of an angle in degrees can be computed with
$$
\text{radians} = \text{degrees} \cdot \frac{\pi}{180}.
$$

In MATLAB you can write:

matlab
x_deg = 30;
x_rad = x_deg * pi / 180;
y = sin(x_rad);

You can also pass vectors or matrices directly. MATLAB automatically applies the function elementwise:

matlab
angles = [0, pi/6, pi/4, pi/3, pi/2];
svals  = sin(angles);

The result svals is a vector of the same size as angles. This broadcasting to each element is a consistent behavior for almost all of MATLAB’s mathematical functions and is especially convenient for plotting and numerical exploration.

Trigonometric Functions in Degrees

If your data is in degrees and you prefer not to convert to radians manually, MATLAB provides degree-based versions of the main trigonometric functions. These are sind, cosd, and tand. They take angles in degrees, not radians.

For example:

matlab
angle_deg = 60;
c1 = cos(angle_deg * pi / 180);  % using radians
c2 = cosd(angle_deg);            % using degrees directly

The values c1 and c2 are equal, but the second form avoids explicit conversion and can make your code clearer when you are working consistently in degrees.

The same applies when you use arrays:

matlab
angles_deg = 0:30:180;
svals_deg = sind(angles_deg);

These degree-specific functions are particularly useful when working with angles from geometry, navigation, and some types of measurement instruments that report directly in degrees.

Inverse Trigonometric Functions

Inverse trigonometric functions return angles from given sine, cosine, or tangent values. In MATLAB, the principal inverse trigonometric functions are asin, acos, and atan. These return results in radians.

For example:

matlab
v = 0.5;
theta = asin(v);      % result in radians
theta_deg = theta * 180 / pi;

If you want the angle directly in degrees, MATLAB also provides degree variants asind, acosd, and atand:

matlab
theta_deg2 = asind(v);

Inverse functions have specific ranges to ensure they are single valued. For example, asin returns values between $-\pi/2$ and $\pi/2$, and acos returns values between 0 and $\pi$. This means that for some applications, especially when you know angles lie outside these principal ranges, you may need additional logic to interpret the result correctly.

There is an additional tangent related function atan2, which takes two arguments. It computes the angle in radians from the positive x-axis to the point $(x,y)$, taking the signs of both arguments into account so that the correct quadrant is returned:

matlab
y = 1;
x = -1;
phi = atan2(y, x);      % angle in radians, between -pi and pi

atan2 is particularly useful for converting Cartesian coordinates to polar form without losing information about the sign of each component.

Hyperbolic Trigonometric Functions

MATLAB includes hyperbolic trigonometric functions and their inverses. The basic hyperbolic functions are sinh, cosh, and tanh. They are defined in terms of the exponential function. For example,
$$
\sinh(x) = \frac{e^{x} - e^{-x}}{2}, \quad
\cosh(x) = \frac{e^{x} + e^{-x}}{2}.
$$

In MATLAB:

matlab
x = 1.5;
sh = sinh(x);
ch = cosh(x);
th = tanh(x);

These functions also act elementwise on vectors and matrices. Hyperbolic functions are common in some areas of physics and engineering, such as describing certain types of curves and systems.

Inverse hyperbolic functions are asinh, acosh, and atanh:

matlab
y = 2;
x1 = asinh(y);
x2 = acosh(3);
x3 = atanh(0.5);

Each inverse hyperbolic function has its own domain and range that you need to respect, particularly if you are working with real values.

The Exponential Function

MATLAB represents the natural exponential function $e^x$ with exp. This is one of the most widely used mathematical functions in numerical computing. It appears in growth and decay models, probability distributions, signal processing, and differential equations.

To compute $e^x$:

matlab
x = 2;
y = exp(x);

Since exp is fully vectorized, it operates on arrays naturally:

matlab
x = -2:0.5:2;
y = exp(x);

The same behavior applies to matrices. Each element is transformed independently:

matlab
A = [0, 1; 2, 3];
B = exp(A);

Note that exp(A) is not the same as the matrix exponential in linear algebra. MATLAB has a separate function expm for the matrix exponential that performs a very different calculation. As a beginner, use exp when you simply want to apply $e^x$ to each value in an array.

Logarithms and Their Bases

The natural logarithm, the inverse of the exponential function, is represented by log in MATLAB. It computes $\ln(x)$ where the base is $e$. For a number $x>0$:

matlab
x = 10;
lnx = log(x);

To compute logarithms in base 10 or base 2, MATLAB provides log10 and log2:

matlab
x = 100;
lg = log10(x);    % log base 10
lb = log2(x);     % log base 2

If you need a logarithm in an arbitrary base $b$, you can use the relationship
$$
\log_b x = \frac{\ln x}{\ln b}.
$$
In MATLAB:

matlab
b = 3;
x = 27;
logb_x = log(x) / log(b);

Keep in mind that the logarithm of zero or negative numbers is not defined in the real domain. MATLAB will return complex results or special values such as -Inf or NaN depending on the input and context.

Exponentials and Logs with Arrays

A common use of exponentials and logarithms in MATLAB is to transform data elementwise. Because these functions accept arrays directly, there is no need for explicit loops in typical use.

For example, to apply a logarithm to every element in a vector:

matlab
data = [1, 10, 100, 1000];
logdata = log10(data);

Or to convert a matrix of logarithmic values back to linear scale:

matlab
logvals = [-1, 0; 1, 2];
linvals = 10 .^ logvals;

Here .^ is the elementwise power operator, which raises 10 to each element of logvals. Using .^ is important when working with arrays, since ^ is reserved for matrix power, not elementwise exponentiation.

The combination of log and exp is often used to improve numerical stability. Sometimes expressions that involve large or small numbers are computed more safely by taking logs, performing algebra in the log domain, and then converting back with exp.

Interplay Between Trigonometric and Exponential Functions

Trigonometric and exponential functions are linked through complex numbers, even though you may not need this connection for basic use. One classic identity that MATLAB respects is Euler’s formula
$$
e^{i\theta} = \cos(\theta) + i\sin(\theta),
$$
where $i$ is the imaginary unit and $\theta$ is an angle in radians.

In MATLAB you can verify this numerically:

matlab
theta = pi/4;
lhs = exp(1i * theta);
rhs = cos(theta) + 1i * sin(theta);

The values lhs and rhs are numerically equal subject to floating point precision. This connection is the basis of many transform methods and signal processing tools within MATLAB.

As you work with real data, you may not use complex exponentials directly at first, but understanding that MATLAB treats these identities consistently can help you interpret outputs from more advanced functions later.

Handling Large and Small Values

Exponential and logarithmic functions can produce extremely large or extremely small numbers. Since MATLAB uses floating point arithmetic, there are limits on the range of representable values.

Very large positive arguments to exp can overflow to Inf. Very negative arguments can underflow to zero. Likewise, logarithms near zero can tend to -Inf. You can see this behavior:

matlab
exp(1000)    % likely Inf
log(0)       % -Inf

When working with real data you may want to check ranges before applying these functions, especially if your inputs can vary widely. Sometimes simple transformations of the form log(x + c) for some constant c > 0 are used to avoid exact zeros while keeping the original scale approximately intact.

Practical Use with Trigonometry

A frequent beginner task is to compute values of sine and cosine for plotting periodic signals. Using trigonometric functions together with vectors allows you to create smooth curves with few lines of code:

matlab
t = 0:0.01:2*pi;   % time vector in radians
y = sin(t);
plot(t, y);

If your angle is in degrees instead, always ensure that you call sind, cosd, or convert to radians first. A common mistake is to use sin on degree values, which produces completely different numerical results than expected.

When you compute inverse trigonometric functions, remember that the returned angles are in radians for asin, acos, and atan. If you need degrees, explicitly convert or call the asind, acosd, or atand variants. This small detail avoids subtle bugs when you mix trigonometric calculations with externally provided angles.

Summary

In everyday MATLAB use, trigonometric and exponential functions are essential tools for modeling, analysis, and visualization. They work naturally with arrays, support both radians and degrees, and include inverse and hyperbolic variants for more specialized problems. Being aware of angle units, function domains, and how large or small numbers behave with exponentials and logarithms will help you write more correct and reliable code.

Important points to remember:

  1. sin, cos, tan, asin, acos, atan use radians. Use sind, cosd, tand, asind, acosd, atand for degrees.
  2. exp computes $e^x$ elementwise. Use log for natural log, log10 for base 10, log2 for base 2.
  3. Many functions are vectorized. They apply to every element of a vector or matrix without loops.
  4. Use .^ for elementwise powers like 10 .^ x. Do not confuse this with ^, which is matrix power.
  5. Extremely large or small arguments to exp and log can produce Inf, -Inf, or NaN. Check input ranges when necessary.

Views: 0

Comments

Please login to add a comment.

Don't have an account? Register now!

Where to Move

Move chapter:

  • ☰ 1. Getting Started with MATLAB
    • ☰ 1.1. What Is MATLAB and What Is It Used For
      • ☰ 1.2. Installing MATLAB and Setting Up the Environment
        • ☰ 1.3. Understanding the MATLAB Desktop Interface
          • ☰ 1.4. Command Window, Workspace, and Current Folder
            • ☰ 1.5. Basic Workflow and Running Simple Commands
              • ☰ 1.6. Getting Help and Documentation
              • ☰ 2. MATLAB Language Basics
                • ☰ 2.1. MATLAB Syntax and Expressions
                  • ☰ 2.2. Variables and Assignment
                    • ☰ 2.3. Data Types and Type Conversion
                      • ☰ 2.4. Vectors and Matrices Fundamentals
                        • ☰ 2.5. Creating and Indexing Arrays
                          • ☰ 2.6. Basic Arithmetic and Elementwise Operations
                            • ☰ 2.7. Using Built-In Constants and Functions
                            • ☰ 3. Working with Variables and the Workspace
                              • ☰ 3.1. Creating, Viewing, and Editing Variables
                                • ☰ 3.2. Workspace Browser and Variable Inspector
                                  • ☰ 3.3. Saving and Loading Data Files
                                    • ☰ 3.4. MAT-Files and Importing External Data
                                      • ☰ 3.5. Clearing Variables and Managing the Workspace
                                      • ☰ 4. Vectors, Matrices, and Arrays
                                        • ☰ 4.1. Row and Column Vectors
                                          • ☰ 4.2. Matrix Creation and Concatenation
                                            • ☰ 4.3. Indexing, Slicing, and Logical Indexing
                                              • ☰ 4.4. Matrix Operations vs Elementwise Operations
                                                • ☰ 4.5. Reshaping and Rearranging Arrays
                                                  • ☰ 4.6. Special Matrices and Functions
                                                  • ☰ 5. Basic Programming Constructs
                                                    • ☰ 5.1. Scripts vs Functions
                                                      • ☰ 5.2. Control Flow with if, elseif, else
                                                        • ☰ 5.3. Loops with for and while
                                                          • ☰ 5.4. break, continue, and return
                                                            • ☰ 5.5. Logical Expressions and Relational Operators
                                                              • ☰ 5.6. Best Practices for Clear and Readable Code
                                                              • ☰ 6. Functions in MATLAB
                                                                • ☰ 6.1. Creating Simple Functions
                                                                  • ☰ 6.2. Function Inputs and Outputs
                                                                    • ☰ 6.3. Function Files and the Path
                                                                      • ☰ 6.4. Local vs Global Variables
                                                                        • ☰ 6.5. Anonymous Functions
                                                                          • ☰ 6.6. Organizing Code into Multiple Functions
                                                                          • ☰ 7. Working with Numbers and Math
                                                                            • ☰ 7.1. Scalars and Vectorized Computations
                                                                              • ☰ 7.2. Basic Mathematical Functions
                                                                                • ☰ 7.3. Trigonometric and Exponential Functions
                                                                                  • ☰ 7.4. Complex Numbers in MATLAB
                                                                                    • ☰ 7.5. Rounding, Modulo, and Special Values
                                                                                      • ☰ 7.6. Numerical Precision and Floating Point Considerations
                                                                                      • ☰ 8. Strings, Characters, and Categorical Data
                                                                                        • ☰ 8.1. Character Arrays and String Arrays
                                                                                          • ☰ 8.2. Creating and Manipulating Strings
                                                                                            • ☰ 8.3. Converting Between Data Types and Strings
                                                                                              • ☰ 8.4. Comparing and Searching Strings
                                                                                                • ☰ 8.5. Categorical Arrays Basics
                                                                                                • ☰ 9. Tables, Structures, and Cell Arrays
                                                                                                  • ☰ 9.1. Introduction to Tables
                                                                                                    • ☰ 9.2. Creating Tables and Accessing Data
                                                                                                      • ☰ 9.3. Modifying and Joining Tables
                                                                                                        • ☰ 9.4. Structures and Nested Structures
                                                                                                          • ☰ 9.5. Cell Arrays and Cell Indexing
                                                                                                            • ☰ 9.6. Choosing the Right Container Type
                                                                                                            • ☰ 10. Importing and Exporting Data
                                                                                                              • ☰ 10.1. Reading Text and CSV Files
                                                                                                                • ☰ 10.2. Importing Excel Spreadsheets
                                                                                                                  • ☰ 10.3. Importing Data with the Import Tool
                                                                                                                    • ☰ 10.4. Exporting Data to Files
                                                                                                                      • ☰ 10.5. Working with Image and Audio Files
                                                                                                                        • ☰ 10.6. Basic File Management from MATLAB
                                                                                                                        • ☰ 11. Plotting and Visualization
                                                                                                                          • ☰ 11.1. Introduction to 2D Plotting
                                                                                                                            • ☰ 11.2. Customizing Plots and Axes
                                                                                                                              • ☰ 11.3. Multiple Plots in One Figure
                                                                                                                                • ☰ 11.4. Labels, Titles, Legends, and Annotations
                                                                                                                                  • ☰ 11.5. Plotting with Different Line and Marker Styles
                                                                                                                                    • ☰ 11.6. Saving and Exporting Figures
                                                                                                                                    • ☰ 12. Advanced Plotting for Beginners
                                                                                                                                      • ☰ 12.1. Subplots and Tiled Layouts
                                                                                                                                        • ☰ 12.2. Bar, Scatter, and Stem Plots
                                                                                                                                          • ☰ 12.3. Histograms and Density Plots
                                                                                                                                            • ☰ 12.4. 3D Plots and Surface Plots
                                                                                                                                              • ☰ 12.5. Using plottools and Interactive Editing
                                                                                                                                                • ☰ 12.6. Basics of Handle Graphics
                                                                                                                                                • ☰ 13. Working with Scripts and Live Scripts
                                                                                                                                                  • ☰ 13.1. Creating and Running Script Files
                                                                                                                                                    • ☰ 13.2. Sectioning Code and Running Sections
                                                                                                                                                      • ☰ 13.3. Live Scripts and Rich Text
                                                                                                                                                        • ☰ 13.4. Adding Equations, Images, and Outputs
                                                                                                                                                          • ☰ 13.5. Sharing and Exporting Live Scripts
                                                                                                                                                          • ☰ 14. Debugging and Code Quality
                                                                                                                                                            • ☰ 14.1. Finding and Fixing Syntax Errors
                                                                                                                                                              • ☰ 14.2. Setting Breakpoints and Stepping Through Code
                                                                                                                                                                • ☰ 14.3. Inspecting Variable Values During Execution
                                                                                                                                                                  • ☰ 14.4. Using the Editor Debugging Tools
                                                                                                                                                                    • ☰ 14.5. Using Code Analyzer and Warnings
                                                                                                                                                                      • ☰ 14.6. Writing Clean and Maintainable MATLAB Code
                                                                                                                                                                      • ☰ 15. Basic Linear Algebra with MATLAB
                                                                                                                                                                        • ☰ 15.1. Vectors and Norms
                                                                                                                                                                          • ☰ 15.2. Matrix Addition and Multiplication
                                                                                                                                                                            • ☰ 15.3. Solving Linear Systems with Backslash
                                                                                                                                                                              • ☰ 15.4. Inverses, Determinants, and Rank
                                                                                                                                                                                • ☰ 15.5. Eigenvalues and Eigenvectors
                                                                                                                                                                                  • ☰ 15.6. Using Linear Algebra Functions Safely
                                                                                                                                                                                  • ☰ 16. Data Analysis and Statistics Fundamentals
                                                                                                                                                                                    • ☰ 16.1. Descriptive Statistics and Summary Measures
                                                                                                                                                                                      • ☰ 16.2. Sorting, Filtering, and Grouping Data
                                                                                                                                                                                        • ☰ 16.3. Basic Correlation and Regression
                                                                                                                                                                                          • ☰ 16.4. Simple Data Cleaning Techniques
                                                                                                                                                                                            • ☰ 16.5. Basic Time Series Handling
                                                                                                                                                                                            • ☰ 17. Working with Time and Dates
                                                                                                                                                                                              • ☰ 17.1. Representing Dates and Times
                                                                                                                                                                                                • ☰ 17.2. Creating and Formatting datetime Arrays
                                                                                                                                                                                                  • ☰ 17.3. Date Calculations and Differences
                                                                                                                                                                                                    • ☰ 17.4. Plotting Data over Time
                                                                                                                                                                                                    • ☰ 18. Introduction to Signal and Image Handling
                                                                                                                                                                                                      • ☰ 18.1. Basic Signal Representation in MATLAB
                                                                                                                                                                                                        • ☰ 18.2. Simple Filtering and Transformations
                                                                                                                                                                                                          • ☰ 18.3. Reading, Displaying, and Writing Images
                                                                                                                                                                                                            • ☰ 18.4. Basic Image Transformations
                                                                                                                                                                                                            • ☰ 19. Automation and Scripting Tasks
                                                                                                                                                                                                              • ☰ 19.1. Batch Processing of Files
                                                                                                                                                                                                                • ☰ 19.2. Writing Reusable Utility Scripts
                                                                                                                                                                                                                  • ☰ 19.3. Using Relative Paths and the MATLAB Path
                                                                                                                                                                                                                    • ☰ 19.4. Simple User Interaction with input and disp
                                                                                                                                                                                                                    • ☰ 20. Introduction to Toolboxes
                                                                                                                                                                                                                      • ☰ 20.1. What Are Toolboxes and How to Use Them
                                                                                                                                                                                                                        • ☰ 20.2. Exploring Installed Toolboxes
                                                                                                                                                                                                                          • ☰ 20.3. Finding Functions in Toolboxes
                                                                                                                                                                                                                            • ☰ 20.4. Add-Ons and MATLAB File Exchange
                                                                                                                                                                                                                            • ☰ 21. Best Practices and Next Steps
                                                                                                                                                                                                                              • ☰ 21.1. Organizing Projects and Folders
                                                                                                                                                                                                                                • ☰ 21.2. Version Control Basics with MATLAB
                                                                                                                                                                                                                                  • ☰ 21.3. Performance Tips for Beginners
                                                                                                                                                                                                                                    • ☰ 21.4. Learning Resources and Documentation Path
                                                                                                                                                                                                                                      • ☰ 21.5. Suggested Practice Projects for Beginners