**Matlab Tutorial **– You can find all the Matlab Tutorial here. In this tutorial we will discuss about Array operations and Linear equations.

**Array operations**

MATLAB has two different types of arithmetic operations: matrix arithmetic operations and array arithmetic operations. We have seen matrix arithmetic operations in the previous lab. Now, we are interested in array operations.

**Matrix arithmetic operations**

As we mentioned earlier, MATLAB allows arithmetic operations: +, −, ∗, and ˆ to be carried out on matrices. Thus,

**A+B** or **B+A** is valid if A and B are of the same size

**A*B** is valid if A’s number of column equals B’s number of rows

**A^2** is valid if A is square and equals **A*A **

**α*A** or **A*α** multiplies each element of A by α

**Array arithmetic operations**

On the other hand, array arithmetic operations or array operations for short, are done element-by-element. The period character, ., distinguishes the array operations from the matrix operations. However, since the matrix and array operations are the same for addition (+) and subtraction (−), the character pairs (.+) and (.−) are not used. The list of array operators is shown below in Table 3.2. If A and B are two matrices of the same size with elements A = [aij ] and B = [bij ], then the command

**>> C = A.*B**

produces another matrix C of the same size with elements cij = aij bij . For example, using the same 3 × 3 matrices,

we have,

>> C = A.*B

C = 10 40 90

160 250 360

490 640 810

To raise a scalar to a power, we use for example the command 10^2. If we want the operation to be applied to each element of a matrix, we use .^2. For example, if we want to produce a new matrix whose elements are the square of the elements of the matrix A, we enter

>> A.^2

ans = 1 4 9

16 25 36

49 64 81

The relations below summarize the above operations. To simplify, let’s consider two vectors U and V with elements U = [ui ] and V = [vj ].

**Solving linear equations**

One of the problems encountered most frequently in scientific computation is the solution of systems of simultaneous linear equations. With matrix notation, a system of simultaneous linear equations is written

Ax = b

where there are as many equations as unknown. A is a given square matrix of order n, b is a given column vector of n components, and x is an unknown column vector of n components.

In linear algebra we learn that the solution to Ax = b can be written as x = A^−1 b, where A^−1 is the inverse of A.

For example, consider the following system of linear equations

The coefficient matrix A is

With matrix notation, a system of simultaneous linear equations is written

Ax = b

This equation can be solved for x using linear algebra. The result is x = A^−1 b

There are typically two ways to solve for x in MATLAB:

- The first one is to use the matrix inverse, inv

**>> A = [1 2 3; 4 5 6; 7 8 0];**

**>> b = [1; 1; 1];**

**>> x = inv(A)*b**

**x = -1.0000**

**1.0000**

**-0.0000** - The second one is to use the backslash (\)operator. The numerical algorithm behind this operator is computationally efficient. This is a numerically reliable way of solving system of linear equations by using a well-known process of Gaussian elimination.

**>> A = [1 2 3; 4 5 6; 7 8 0];**

**>> b = [1; 1; 1];**

**>> x = A\b**

**x = -1.0000**

**1.0000**

**-0.0000**

This problem is at the heart of many problems in scientific computation. Hence it is important that we know how to solve this type of problem efficiently.

Now, we know how to solve a system of linear equations. In addition to this, we will see some additional details which relate to this particular topic.

**Matrix inverse**

Let’s consider the same matrix A

Calculating the inverse of A manually is probably not a pleasant work. Here the hand-calculation of A^−1 gives as a final result:

In MATLAB, however, it becomes as simple as the following commands:

>> A = [1 2 3; 4 5 6; 7 8 0];

>> inv(A)

ans = -1.7778 0.8889 -0.1111

1.5556 -0.7778 0.2222

-0.1111 0.2222 -0.1111

and the determinant of A is

>> det(A)

ans = 27

**Matrix functions**

MATLAB provides many matrix functions for various matrix/vector manipulations; see Table 3.3 for some of these functions. Use the online help of MATLAB to find how to use these functions.

You can see the complete Matlab Matlab here:

- Introduction to Matlab
- Chapter 2 on Matlab
- Matrix on Matlab
- Array And Linear Operation Matlab
- Programming in Matlab
- Control Flow and Operation in Matlab
- Debugging M-files in Matlab