Matlab

How to Use Arithmetic Operators in MATLAB

Arithmetic operators in MATLAB help perform mathematical operations. These operators include addition (+), subtraction (-), multiplication (*), division (/), power (^), and transpose (‘), along with the backslash operator () for solving systems of linear equations. By utilizing these operators, you can manipulate numerical values and arrays, enabling you to solve complex mathematical problems and analyze data efficiently.

This article will explore the functionality and usage of these arithmetic operators in MATLAB with scalars, vectors, and matrices, along with examples.

1: Use Arithmetic Operators With Scalars

Arithmetic operators can be used to perform basic mathematical operations with scalar values in MATLAB.

Let’s consider two scalar variables, x/y, and explore how different operators can be applied to them:

1.1: Addition (+) and Subtraction (-)

  • Addition: x + y will yield the sum of x and y.
  • Subtraction: x – y will give the difference between x and y.

1.2: Multiplication (*) and Division (/ or \)

  • Multiplication: x * y will provide the product of x and y.
  • Right Division: x / y will give the quotient by dividing x with y.
  • Left Division: x \ y will give the quotient by dividing y with x.

1.3: Exponentiation (^)

  • Exponentiation: x^y will raise x to the power of y.

1.4: Transpose (‘)

  • Transpose: x’ will transpose the scalar x, resulting in the same value.

The MATLAB code given below uses the arithmetic as mentioned earlier operators on two scalar values x and y.

x= 18;

y= 8;

sum= x+y

sub= x-y

mult= x*y

right_div= x/y

left_div= x\y

exp= x^y

trans=x'

2: Use MATLAB as a Calculator

MATLAB can also be used as a powerful calculator to perform complex mathematical computations and here are some key aspects to consider:

2.1: Order of Precedence

  • Parenthesis is executed first. If nested parentheses exist, the inner one will be computed first.
  • Exponents are calculated secondly.
  • Multiplication and division are calculated thirdly.
  • Addition and subtraction are computed fourthly.

2.2: Parentheses

In MATLAB, parentheses can be utilized to override the default order of operations and give priority to specific computations.

2.3: Mathematical Expressions

  • MATLAB allows you to write complex mathematical expressions for evaluation.
  • Expressions can involve multiple arithmetic operators and follow the order of precedence.

For example:

result1 = 64^(1/4)+25^0.5

result2 = 64^1/4+25^0.5

result3 = 0.5-(0.5)^3/(1*2*3)+0.5^5/(1*2*3*4*5)-(0.5)^7/(1*2*3*4*5*6*7)

The above example computes three mathematical expressions having multiple arithmetic operations. Here, the first two expressions have the same values and arithmetic operators, but both have different results because, in the first one, 1/4 is considered as the power of 64 while in the second one, 64 has the power of 1, and then it is divided by 4. The third expression is the Taylor series of sin(pi/6) having the first four terms.

3: Use Arithmetic Operations With Vectors

Arithmetic operations can also be performed with vectors in MATLAB, subject to certain conditions; let’s consider the following scenarios:

3.1: Addition and Subtraction

  • Vectors of equal size can be added or subtracted by performing element-wise operations.
  • For example, given vectors x and y, x + y will add the corresponding elements, while x – y will subtract them.

3.2: Multiplication

  • Vector multiplication follows specific rules, such as the number of columns in the first vector being equal to the number of rows in the second vector.
  • Multiplication can be performed using the * operator: x * y.
  • For element-by-element multiplication, you can use .* instead of *.

3.3: Division and Exponentiation

  • To perform division between two vectors, you can use / for division. However, ^ is not directly supported for exponentiation between vectors in MATLAB.
  • For element-by-element division and exponential, you can use ./ and .^ for division and exponential.

3.4: Transpose

  • The transpose operation can be applied to vectors using the ‘ operator.
  • Transposing a vector swaps its rows and columns.

For example:

x = [2 4 6];

y = [1 2 3];

sum= x+y

sub= x-y

mult=x.*y

div= x/y

exp= x.^y

trans= x'

3.5: Apply Matrix Multiplication Rule on Matrix

According to the rule of vector multiplication, the number of columns contained by the first vector must be equal to the number of rows contained by the second vector. So in the given example, we multiply two vectors x and y by following the vector multiplication rule.

x= [2:9];

y= [1; 3; 5; 7; 9; 11; 13; 15];

mult= x*y

In the above example, vector x has 1 row and 8 columns while vector y has 8 rows and 1 column. As the

vector multiplication rule allows the multiplication between these two vectors, they are multiplied and

the calculated result is displayed on the screen.

4: Use Arithmetic Operations With Matrices

Arithmetic operations can also be applied to matrices in MATLAB. Let’s explore the following scenarios:

4.1: Addition and Subtraction

  • Matrices with identical dimensions can be added or subtracted by performing element-wise operations.
  • For example, given matrices x and y, x + y will add the corresponding elements, while x – y will subtract them.

4.2: Multiplication

  • Matrix multiplication follows specific rules, such as the number of columns in the first matrix being equal to the number of rows in the second matrix.
  • Multiplication can be performed using the * operator: x * y.
  • For element-by-element matrix multiplication, you can use .*.

4.3: Division

Matrix division in MATLAB is represented by the backslash operator (\). It is also known as the left division or matrix left division.

  • To perform matrix division, you can use the backslash operator (), which is:

x = A \ B that finds the solution vector x that satisfies the equation Ax = B.

  • It is equivalent to multiplying the A inverse with vector B.
  • Matrix division should not be confused with element-wise division, which is performed using the slash operator (/).

4.4: Exponentiation

  • Exponentiation is possible for square matrices.
  • For example, given a square matrix x, x^n will raise x to the power of n.
  • For element-by-element exponentiation of the matrix, you can use .^.

4.5: Transpose

  • Transposing a matrix swaps its rows and columns.

For example:

x = [1:6; 7:12];

y = [1:2:12; 2:2:12];

add= x + y

sub= x - y

mult = x.*y

div= x \ y

exp= x.^y

trans= x'

4.6: Apply Matrix Multiplication Rule on Matrix

The multiplication between matrices exists by following the matrix multiplication rule that states that the number of columns contained by the first matrix must be equal to the number of rows contained by the second matrix. So in the given example, we multiply two matrices x and y by following the matrix multiplication rule.

x= [1:6; 7:12];

y= [1:2:12; 2:2:12];

mult= x*y'

In the above code, both matrices have the same size which is 2-by-6, but the values within each matrix are different thus matrix multiplication cannot take place between them. To perform multiplication we take the transpose of the matrix y and then multiply it with the matrix x. The resultant matrix can be shown on the screen.

4.7: Exponentiation Support on Matrix

Matrices support exponentiation operation whenever they are square. For example

x= [1:3; 4:6; 7:9];

exp= x^4

In the above code, we created a square matrix of the size 3-by-3, then we calculated the power of the given matrix. As the specified power is 4, so the matrix is multiplied by itself four times; the calculated results are displayed on the screen.

Conclusion

The arithmetic operators allow us to perform mathematical operations on the scalars, vectors, and, matrices in MATLAB. These operators include the addition “+”, subtraction “-”, multiplication “*”, left division “\”, right division “/”, and exponentiation “^”. All these operations can be performed on the scalars but some of the operations are not supported by the vectors and matrices. This guide demonstrated the functionality of MATLAB arithmetic operators using scalars, vectors, and matrices.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.