MATLAB TUTORIAL - PART VI

Addition and subtraction are element by element operations. To use them, the two or more arrays or vectors that should be summed together of subtracted from each other should be having the same number of rows as well as columns.

On the other hand, multiplication is not an element by element operation, so the multiplication of 2 matrices is executed according to the rules of linear algebra, where if we have to matrices with sized mxn and pxq, n should be equal to p in order to perform the multiplication that results in an mxq matrix.

As you may know, matrix multiplication is not commutative. And power operations can only be done with square matrices.

When it comes to multiplying vectors, they should be of the same size where one is a row vector and the other is a column vector, and of course the result will be a 1×1 matrix. This is know as the dot product.

Another way to apply the dot product is to use the built-in function dot(A,B).

On the other hand, if we multiply a scalar value with a matrix, we will have this scalar multiplied with all of the elements in the matrix.

To solve linear algebra equations, we can represent any system with matrices.

If we have a system of 3 equations:

  • A1×1 + A2×2 + A3×3 =B1
  • S1×1 + S2×2 + S3×3 = B2
  • Z1×1 + Z2×2 + Z3×3 = B3

This can be represents by UX=B

where U=[A1 A2 A3;S1 S2 S3;Z1 Z2 Z3]

X=[x1;x2;x3]

B=[B1;B2;B3]

Before we continue in the solution, we have to introduce array division, which is also associated with the rules of linear algebra. Like the multiplication, it is not and element by element operation.

We have M x M^-1 = M^-1 x M = I

where M^-1 is the inverse of a matrix and can be executed by the command inv(M).

If you like, you can try this using a square matrix in order to make the example simple as well as clear.

Another function that is usually used is the determinant of a matrix which is calculated by the command det(M) where M is a square matrix.

Back to our system of 3 equations, we have

AX=B

X=(A^-1)B

We can calculate it in MATLAB using inv(A)xB or A\B and here we made use of the left division that we introduced earlier in the tutorial.

Below is an example showing how to use the left division;

>> A=[2 3 4;5 4 5;5 6 7];B=[5;6;7];
>> X=A\B
X =

0.1667
-2.6667
3.1667

In the coming tutorial, we will have a look at element by element operations, some array functions, and plotting. Click below to view the next post.

PART VII

Leave a Reply

You must be logged in to post a comment.