Academic Block

Basic Arithmetic Operations in MATLAB Get Certified

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_val = a + b;        % Addition
difference = a - b;     % Subtraction
product = a * b;        % Multiplication
quotient = a / b;       % Division
remainder = mod(a, b);  % Modulus

disp(sum_val);
disp(difference);
disp(product);
disp(quotient);
disp(remainder);

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);

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 (A * inv(B))
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);
disp(power_result);
disp(log_value);
disp(sin_value);

Common Arithmetic Errors

Be mindful of these common pitfalls:

  • Division by zero produces Inf or NaN.
  • Matrix dimensions must agree for matrix operations.

Useful MATLAB Functions

Function
Explanation
sum
If N is a vector, sum(N) returns the sum of all elements. If N is a matrix, sum(N) returns the sum of each column as a row vector.
prod
If N is a vector, prod(N) returns the product of all elements. If N is a matrix, prod(N) returns the product of each column as a row vector.
.* / times
A = N.*M multiplies corresponding elements. times(N,M) is an alternative syntax.
.^ / power
A = N.^M raises each element of N to the power of the corresponding element of M. power(N,M) is an alternative syntax.
round
round(N) rounds each element of N to the 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.