Basic Arithmetic Operations in MATLAB
MATLAB provides a powerful set of tools for performing basic arithmetic operations. These operations are essential for numerical computations and data analysis.
Basic Arithmetic
Arithmetic operations in MATLAB are straightforward and use the standard mathematical operators:
% Basic Arithmetic Operations
a = 10;
b = 5;
sum = a + b; % Addition
difference = a - b; % Subtraction
product = a * b; % Multiplication
quotient = a / b; % Division
remainder = mod(a, b); % Modulus
disp(sum); % Output: 15
disp(difference); % Output: 5
disp(product); % Output: 50
disp(quotient); % Output: 2
disp(remainder); % Output: 0
Explanation: The basic arithmetic operators (+
, -
, *
, /
, and mod
) allow for simple mathematical computations.
Order of Operations
MATLAB follows the standard order of operations (PEMDAS – Parentheses, Exponents, Multiplication and Division, Addition and Subtraction):
% Order of Operations Example
result = (3 + 2) * 4^2 / 2;
disp(result); % Output: 40
Explanation: Parentheses dictate the priority, followed by exponents, then multiplication/division, and finally addition/subtraction.
Matrix Arithmetic
MATLAB excels in matrix computations. You can perform element-wise operations or matrix operations:
% Matrix Arithmetic
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A + B; % Matrix Addition
D = A - B; % Matrix Subtraction
E = A * B; % Matrix Multiplication
F = A .* B; % Element-wise Multiplication
G = A / B; % Matrix Division
H = A ./ B; % Element-wise Division
disp(C); disp(D); disp(E);
disp(F); disp(G); disp(H);
Element-wise Operations
Use a dot (.
) before the operator to apply element-wise arithmetic operations.
Special Mathematical Operations
MATLAB includes functions for special operations like power, square root, logarithm, and trigonometry:
% Special Mathematical Operations
x = 16;
y = 2;
square_root = sqrt(x); % Square root
power_result = x^y; % Exponentiation
log_value = log(x); % Natural logarithm
sin_value = sin(pi/2); % Sine function
disp(square_root); % Output: 4
disp(power_result); % Output: 256
disp(log_value); % Output: ~2.77
disp(sin_value); % Output: 1
Common Arithmetic Errors
Be mindful of these common pitfalls:
- Division by zero produces
Inf
orNaN
. - Matrix dimensions must agree for matrix operations.
Useful MATLAB Functions
TotalSum = sum(N)
will displays the sum of all its elements. If N is 2D array, then
TotalSum = sum(N)
will output the sum of all the individual columns in a row vector. A = prod(N)
will displays the product of all its elements. If N is 2D array, then
A = prod(N)
will output the product of all the individual columns in a row vector. times
A = N.*M
will multiply each element of N to the corresponding element from M. A = timesr(N,M)
is an another way to execute N.*Mpower
A = N.^M
will output each element of N raised to the element-wise power from M. A = power(N,M)
is an another way to execute N.^M N = round(N)
will round all the elements of N to their nearest integer.Practice Questions
Test Yourself
1. Compute the sum, difference, product, and quotient of 8 and 4 in MATLAB.
2. Perform matrix addition and multiplication for the matrices A = [1 2; 3 4]
and B = [5 6; 7 8]
.
3. Calculate the square root, logarithm, and sine of the value 10.