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(), andatan2() - Exponential and logarithmic functions:
exp(),log(), andlog10() - Others:
abs(),sqrt(),round(),ceil(), andfloor()
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.
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(), andeigenvectors() - Array operations:
sum(),mean(),median(),max(),min(),std(),var(), andsort()
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.
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(), andconv() - Image processing:
imread(),imshow(),imwrite(), andrgb2gray() - Optimization:
fminsearch(),fminunc(),linprog(), andquadprog() - Statistics:
normpdf(),normcdf(),mean(),std(),median(), andhist()
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 (FFT) on a variable.Fast Fourier Transform An 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.
Polynomial operations
MATLAB has functions to work with polynomials, including polynomial evaluation, root finding, and curve fitting.
- Polynomial operations:
polyval(),polyfit(),roots(), andconv()
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.
Special functions
MATLAB provides special mathematical functions that find applications across various mathematics, physics, and engineering domains.
- Bessel functions:
besselj(),bessely(),besseli(), andbesselk() - Gamma and beta functions:
gamma()andbeta() - Error and exponential integrals:
erf(),erfc(), andexpint()
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 of the first kind,bessel function Bessel functions describe the radial part of vibrations of a circular membrane. J_1(x), for each point in'x'. -
gamma()calculates the of a variable.gamma function The gamma function is an extension of the factorial function. -
beta()calculates the of given parameters.beta function It is defined in the domains of real numbers.
Random number generation
MATLAB includes functions for generating random numbers from various distributions.
- Random number generation:
rand(),randn(),randi(), andrandperm()
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.
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