Table of Contents
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:
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:
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:
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:
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:
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:
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:
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:
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:
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$:
x = 2;
y = exp(x);
Since exp is fully vectorized, it operates on arrays naturally:
x = -2:0.5:2;
y = exp(x);The same behavior applies to matrices. Each element is transformed independently:
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$:
x = 10;
lnx = log(x);
To compute logarithms in base 10 or base 2, MATLAB provides log10 and log2:
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:
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:
data = [1, 10, 100, 1000];
logdata = log10(data);Or to convert a matrix of logarithmic values back to linear scale:
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:
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:
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:
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:
sin,cos,tan,asin,acos,atanuse radians. Usesind,cosd,tand,asind,acosd,atandfor degrees.expcomputes $e^x$ elementwise. Uselogfor natural log,log10for base 10,log2for base 2.- Many functions are vectorized. They apply to every element of a vector or matrix without loops.
- Use
.^for elementwise powers like10 .^ x. Do not confuse this with^, which is matrix power. - Extremely large or small arguments to
expandlogcan produceInf,-Inf, orNaN. Check input ranges when necessary.