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:
These include basic arithmetic operations, trigonometric, exponential, and logarithmic functions.
+
, -
, *
, /
, and \
sin()
, cos()
, tan()
, asin()
, acos()
, atan()
, and atan2()
exp()
, log()
, and log10()
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 functionsa = 5;b = 3;% Arithmetic operationsaddition_result = a + b;subtraction_result = a - b;multiplication_result = a * b;division_result = a / b;% Trigonometric functionsangle_rad = pi/4;sin_result = sin(angle_rad);cos_result = cos(angle_rad);tan_result = tan(angle_rad);% Exponential and logarithmic functionsexp_result = exp(a);log_result = log(a);log10_result = log10(a);% Display resultsdisp('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.
MATLAB is well-known for its matrix computation capabilities, and it offers numerous functions for manipulating matrices and arrays.
transpose()
, inv()
, det()
, eigenvalues()
, and eigenvectors()
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 operationsA = [1, 2, 2; 4, 5, 6; 7, 8, 9];% Matrix operationstranspose_A = transpose(A);determinant_A = det(A);inverse_A = inv(A);% Array operationssum_A = sum(A);mean_A = mean(A);max_A = max(A);% Display resultsdisp('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.
MATLAB provides specialized functions to address specific tasks within certain fields, such as signal processing, image processing, optimization, and statistics.
fft()
, ifft()
, filter()
, and conv()
imread()
, imshow()
, imwrite()
, and rgb2gray()
fminsearch()
, fminunc()
, linprog()
, and quadprog()
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 functionsx = linspace(0, 2*pi, 100);y = sin(x);% Signal processingfft_y = fft(y);% Display resultsdisp('Specialized Functions Results:');disp(['FFT of sin(x): ', num2str(fft_y)]);
In the code above:
fft()
performs the MATLAB has functions to work with polynomials, including polynomial evaluation, root finding, and curve fitting.
polyval()
, polyfit()
, roots()
, and conv()
Let’s get an overview of how to use each function in MATLAB in the code widget below:
% Polynomial Operationscoefficients = [1, -3, 2]; % Polynomial: x^2 - 3x + 2x_values = linspace(-2, 4, 100);% Polynomial evaluationy_values = polyval(coefficients, x_values);% Display resultsdisp('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.
MATLAB provides special mathematical functions that find applications across various mathematics, physics, and engineering domains.
besselj()
, bessely()
, besseli()
, and besselk()
gamma()
and beta()
erf()
, erfc()
, and expint()
Let’s get an overview of how to use each MATLAB function in the code widget below.
% Special functionsx = linspace(0, 5, 100);% Bessel functionsbessel_result = besselj(1, x);% Gamma and beta functionsgamma_result = gamma(2);beta_result = beta(2, 3);% Display resultsdisp('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 J_1(x)
, for each point in 'x'
.
gamma()
calculates the
beta()
calculates the
MATLAB includes functions for generating random numbers from various distributions.
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 generationrandom_vector = randn(1, 10); % 1x10 vector of random numbers from a standard normal distribution% Display resultsdisp('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.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