What are mathematical functions in MATLAB?

MATLAB has various built-in mathematical functions for various purposes. We can use them as shortcuts without creating a separate function for each case.

The following are some of the common categories of mathematical functions available in MATLAB:

Elementary math functions

These include basic arithmetic operations, trigonometric, exponential, and logarithmic functions.

  • Arithmetic functions: +, -, *, /, and \
  • Trigonometric functions: sin(), cos(), tan(), asin(), acos(), atan(), and atan2()
  • Exponential and logarithmic functions: exp(), log(), and log10()
  • Others: abs(), sqrt(), round(), ceil(), and floor()

Let’s get an overview of how to use each function in MATLAB in the code widget below:

% Elementary math functions
a = 5;
b = 3;
% Arithmetic operations
addition_result = a + b;
subtraction_result = a - b;
multiplication_result = a * b;
division_result = a / b;
% Trigonometric functions
angle_rad = pi/4;
sin_result = sin(angle_rad);
cos_result = cos(angle_rad);
tan_result = tan(angle_rad);
% Exponential and logarithmic functions
exp_result = exp(a);
log_result = log(a);
log10_result = log10(a);
% Display results
disp('Elementary Math Functions Results:');
disp(['Addition: ', num2str(addition_result)]);
disp(['Subtraction: ', num2str(subtraction_result)]);
disp(['Multiplication: ', num2str(multiplication_result)]);
disp(['Division: ', num2str(division_result)]);
disp(['Sine: ', num2str(sin_result)]);
disp(['Cosine: ', num2str(cos_result)]);
disp(['Tangent: ', num2str(tan_result)]);
disp(['Exponential: ', num2str(exp_result)]);
disp(['Natural Logarithm: ', num2str(log_result)]);
disp(['Base 10 Logarithm: ', num2str(log10_result)]);

In the code above:

  • + adds two variables.

  • - subtracts one variable from another.

  • * multiplies two variables.

  • / calculates the fraction of two variables.

  • sin() calculates the sin of an angle.

  • cos() calculates the cosine of an angle.

  • tan() calculates the tangent of an angle.

  • exp() calculates the exponential of a variable.

  • log() calculates the natural logarithm of a variable.

  • log10() calculates the base 10 logarithm of a variable.

Matrix and array operations

MATLAB is well-known for its matrix computation capabilities, and it offers numerous functions for manipulating matrices and arrays.

  • Matrix operations: transpose(), inv(), det(), eigenvalues(), and eigenvectors()
  • Array operations: sum(), mean(), median(), max(), min(), std(), var(), and sort()

Let’s get an overview of how to use each function in MATLAB in the code widget below:

% Matrix and array operations
A = [1, 2, 2; 4, 5, 6; 7, 8, 9];
% Matrix operations
transpose_A = transpose(A);
determinant_A = det(A);
inverse_A = inv(A);
% Array operations
sum_A = sum(A);
mean_A = mean(A);
max_A = max(A);
% Display results
disp('Matrix and Array Operations Results:');
disp('Transpose of A:');
disp(transpose_A);
disp(['Determinant of A: ', num2str(determinant_A)]);
disp('Inverse of A:');
disp(inverse_A);
disp(['Sum of elements in A: ', num2str(sum_A)]);
disp(['Mean of elements in A: ', num2str(mean_A)]);
disp(['Maximum element in A: ', num2str(max_A)]);

In the code above:

  • transpose() calculates the transpose of a matrix.

  • det() calculates the determinant of a matrix.

  • inv() calculates the inverse of a matrix.

  • sum() calculates the sum of all elements of a matrix.

  • mean() calculates the mean of all elements of a matrix.

  • max() finds the maximum element of a matrix.

Specialized functions

MATLAB provides specialized functions to address specific tasks within certain fields, such as signal processing, image processing, optimization, and statistics.

  • Signal processing: fft(), ifft(), filter(), and conv()
  • Image processing: imread(), imshow(), imwrite(), and rgb2gray()
  • Optimization: fminsearch(), fminunc(), linprog(), and quadprog()
  • Statistics: normpdf(), normcdf(), mean(), std(), median(), and hist()

Let’s get an overview of how to use each function in MATLAB in the code widget below:

% Specialized functions
x = linspace(0, 2*pi, 100);
y = sin(x);
% Signal processing
fft_y = fft(y);
% Display results
disp('Specialized Functions Results:');
disp(['FFT of sin(x): ', num2str(fft_y)]);

In the code above:

  • fft() performs the Fast Fourier TransformAn algorithm that computes the Discrete Fourier Transform (DFT) of a sequence or its inverse (IDFT). Fourier analysis converts a signal from its original domain (often time or space) to a representation in the frequency domain and vice versa. (FFT) on a variable.

Polynomial operations

MATLAB has functions to work with polynomials, including polynomial evaluation, root finding, and curve fitting.

  • Polynomial operations: polyval(), polyfit(), roots(), and conv()

Let’s get an overview of how to use each function in MATLAB in the code widget below:

% Polynomial Operations
coefficients = [1, -3, 2]; % Polynomial: x^2 - 3x + 2
x_values = linspace(-2, 4, 100);
% Polynomial evaluation
y_values = polyval(coefficients, x_values);
% Display results
disp('Polynomial Operations Results:');
disp(['Polynomial Evaluation: ', num2str(y_values)]);

In the code above:

  • linspace() generates a specific number of points between two values.

  • polyval() evaluates the polynomial with coefficients at points.

Special functions

MATLAB provides special mathematical functions that find applications across various mathematics, physics, and engineering domains.

  • Bessel functions: besselj(), bessely(), besseli(), and besselk()
  • Gamma and beta functions: gamma() and beta()
  • Error and exponential integrals: erf(), erfc(), and expint()

Let’s get an overview of how to use each MATLAB function in the code widget below.

% Special functions
x = linspace(0, 5, 100);
% Bessel functions
bessel_result = besselj(1, x);
% Gamma and beta functions
gamma_result = gamma(2);
beta_result = beta(2, 3);
% Display results
disp('Special Functions Results:');
disp(['Bessel Function J_1(x): ', num2str(bessel_result)]);
disp(['Gamma(2): ', num2str(gamma_result)]);
disp(['Beta(2, 3): ', num2str(beta_result)]);

In the code above:

  • besselj() calculates bessel functionBessel functions describe the radial part of vibrations of a circular membrane. of the first kind, J_1(x), for each point in 'x'.

  • gamma() calculates the gamma functionThe gamma function is an extension of the factorial function. of a variable.

  • beta() calculates the beta functionIt is defined in the domains of real numbers. of given parameters.

Random number generation

MATLAB includes functions for generating random numbers from various distributions.

  • Random number generation: rand(), randn(), randi(), and randperm()

Let’s get an overview of how to use each function in MATLAB in the code widget below:

% Random number generation
random_vector = randn(1, 10); % 1x10 vector of random numbers from a standard normal distribution
% Display results
disp('Random Number Generation Results:');
disp('Random Vector:');
disp(random_vector);

In the code above:

  • randn() generates a vector of random numbers from a standard normal distribution.

Conclusion

These are a few examples of the many mathematical functions available in MATLAB. MATLAB’s extensive documentation provides detailed information on each function, including syntax, usage examples, and additional options.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved