MATLAB LESSON 1

 

Why should we use MATLAB (Matrix Laboratory)?

 

MATLAB has several advantages over other methods or languages:

 

 

There are also disadvantages:

 

 

USING MATLAB

 

Matlab in not only a programming language, but a programming environment as well.

You can perform operations from the command line, as a sophisticated calculator.

Or you can create programs and functions that perform repetitive tasks, just as any other computer language.

 

Try a simple operation now:

2 + 2 <enter>

 

To run a program, type its name:

demo <enter>

 

One of the most important features of the MATLAB interface is the help. It is very thorough and you can learn almost anything you need from it.

 

Let’s start doing something interesting with MATLAB (Help Manipulating Matrices)

 

 

 

The best way for you to get started with MATLAB is to learn how to handle matrices.

 

You can enter matrices into MATLAB in several different ways:

 

  1. Enter an explicit list of elements.
  2. Load matrices from external data files.
  3. Generate matrices using built-in functions.
  4. Create matrices with your own functions in M-files.

 

We will use a curious example. A magic square.

 

Use these conventions to create a Matrix:

 

  1. Separate the elements of a row with blanks or commas.
  2. Use a semicolon, ; , to indicate the end of each row.
  3. Surround the entire list of elements with square brackets, [ ]

 

A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

 

This is what MATLAB displays after you hit <enter>

 

 

A =

    16     3     2    13

     5    10    11     8

     9     6     7    12

     4    15    14     1

 

Let’s prove it is a magic square. Let’s get the sum of all columns by typing sum(A)

The answer (ans) is:

 

ans =

    34    34    34    34

 

This result is a row vector. Each column in A adds up to 34. That’s magic!

 

How about the row sums? MATLAB has a preference for working with the columns of a matrix, so the easiest way to get the row sums is to transpose the matrix, compute the column sums of the transpose, and then transpose the result. The transpose operation is denoted by an apostrophe or single quote, '. It flips a matrix about its main diagonal and it turns a row vector into a column vector.

 

A'

 

ans =

    16     5     9     4

     3    10     6    15

     2    11     7    14

    13     8    12     1

 

Now:      sum(A')'    produces a column vector containing the row sums

 

ans =

    34

    34

    34

    34

 

Notice the double ‘. This is a very important concept to develop when using Matlab. A’ is the transpose of A. “ sum(A’) “ looks the same as “ sum(A) “, but the first is a sum of the rows of A, the second the sum of columns of A. So, the double ‘ , makes the result reflect its original configuration.

 

The sum of the elements on the main diagonal is easily obtained with the help of the diag function, which picks off that diagonal.

 

diag(A)

 

ans =

    16

    10

     7

     1

 

sum(diag(A))

 

ans =

    34

 

The other diagonal, the so-called anti-diagonal, is not so important mathematically, so MATLAB does not have a ready-made function for it. But a function originally intended for use in graphics, fliplr, flips a matrix from left to right.

 

sum(diag(fliplr(A))

ans =

    34

 

There you have it! it was a magical square. Everything adds up to 34.

 

Another good example to illustrate the use of ‘

 

B = [1 1 1; 2 2 2; 3 3 3]

 

B =

     1     1     1

     2     2     2

     3     3     3

 

sum(B)

 

ans =

     6     6     6

 

sum(B')

 

ans =

     3     6     9

 

sum(B')'

 

ans =

     3

     6

     9

 

 

Subscripts work as in any other language

 

The element in row i and column j of A is denoted by A(i,j). For example, A(4,2) is the number in the fourth row and second column. For our magic square, A(4,2) is 15. So it is possible to compute the sum of the elements in the fourth column of A by typing

 

A(1,4) + A(2,4) + A(3,4) + A(4,4)

ans =

     34

 

The most effective way to perform this operation is using the ‘:’ operator, one of Matlab’s workhorses :

 

sum(A(:,4))

ans =

     34

 

It reads as “add every element in column 4”

 

If you want to see these elements, simply type

A(:,4)

 

ans =

    13

     8

    12

     1

 

This operation preserves the original format of the data. Column 4 looks like a column

 

We can also refer to the elements of a matrix with a single subscript, A(k). This is the usual way of referencing row and column vectors. But it can also apply to a fully two-dimensional matrix, in which case the array is regarded as one long column vector formed from the columns of the original matrix. So, for our magic square, A(8) is another way of referring to the value 15 stored in A(4,2).

 

t = A(4,5) gives you this error :

???  Index exceeds matrix dimensions.

 

This happens because A has only 4 columns and 4 rows. A(4,5) is undefined. Verify this by typing size(A)

ans =

     4     4

However, you can store a value in an element outside of the matrix, and the size increases to accommodate it

 

X = A;

X(4,5) = 17

 

X =

    16     3     2    13     0

     5    10    11     8     0

     9     6     7    12     0

     4    15    14     1    17

 

Now verify that size(X)

 

ans =

4                 5

 

MORE ON ‘:’

 

1:10

ans =

     1     2     3     4     5     6     7     8     9    10

 

creates a row vector containing the integers from 1 to 10.

 

You can also use real or negative steps between numbers:

 

100:-7:50

 

100    93    86    79    72    65    58    51

 

0:pi/4:pi

 

0    0.7854    1.5708    2.3562    3.1416

 

Subscript expressions involving colons refer to portions of a matrix.

 

A(1:k,j) is the first k elements of the jth column of A. So sum(B(:,end))computes the sum of the elements in the last column of B.

ans =

     6

 

Why is the magic sum for a 4-by-4 square equal to 34? If the integers from 1 to 16 are sorted into four groups with equal sums, that sum must be

 

sum(1:16)/4

ans =

     34