# C.1 Getting Started with MATLAB

MATLAB is a programming language for performing technical computations. It is a powerful language that has become very popular and is rapidly becoming a standard instructional language for courses in mathematics, science, and engineering. MATLAB is available on most campuses, and many universities have site licenses allowing MATLAB to be installed on any machine on campus.

In order to launch MATLAB on a PC, double click on the MATLAB icon. If you want to run MATLAB on a Unix system, type matlab at the prompt. Upon launching MATLAB, you will see the MATLAB prompt:

``>>``

which indicates that MATLAB is waiting for a command for you to type in. When you wish to quit MATLAB, type quit at the command prompt.

MATLAB is able to do the basic arithmetic operations such as addition, subtraction, multiplication, and division. These can be accomplished by the operators +, -, *, and /, respectively. In order to raise a number to a power, we use the operator ^ . Let us look at an example:

If we type  at the prompt and press the Enter key

``>> 2^7 + 125/5``

then MATLAB will return the answer:

````ans =`
`   153````

Notice that in this example, MATLAB performed the exponentiation first, the division next, and then added the two results. The order of operations used in MATLAB is the one that we have grown up using. We can also use parentheses to change the order in which MATLAB calculates its quantities. The following example exhibits this:

````>> 11*( (128/(9+7) - 2^(72/12)))`

`ans =`
`   -616````

In these examples, MATLAB has called the result of the calculations ans, which is a variable that is used by MATLAB to store the output of a computation. It is possible to assign the result of a computation to a specific variable. For example,

````>> spot=17`

`spot =`
`   17````

assigns the value of 17 to the variable spot. It is possible to use variables in computations:

````>> dog=11`

`dog =`
`   11`

`>> cat=7`

`cat =`
`   7`

`>> animals=dog+cat`

`animals =`
`   18````

MATLAB also operates like an advanced scientific calculator since it has many functions available to it. For example, we can do the standard operation of taking a square root by using the sqrt function, as in the following example:

````>> sqrt(1024)`

`ans =`
`   32````

There are many other functions available. Some functions that will be useful for this book are mod, factorial, factor, prod, and size.

Help is available in MATLAB. You may either type help at the prompt, or pull down the Help menu. MATLAB also provides help from the command line by typing help commandname. For example, to get help on the function mod, which we shall be using a lot, type the following:

``>> help mod``

MATLAB has a collection of toolboxes available. The toolboxes consist of collections of functions that implement many application-specific tasks. For example, the Optimization toolbox provides a collection of functions that do linear and nonlinear optimization. Generally, not all toolboxes are available. However, for our purposes, this is not a problem since we will only need general MATLAB functions and have built our own functions to explore the number theory behind cryptography.

The basic data type used in MATLAB is the matrix. The MATLAB programming language has been written to use matrices and vectors as the most fundamental data type. This is natural since many mathematical and scientific problems lend themselves to using matrices and vectors.

Let us start by giving an example of how one enters a matrix in MATLAB. Suppose we wish to enter the matrix



into MATLAB. To do this we type:

``>> A = [1 1 1 1; 1 2 4 8; 1 3 9 27; 1 4 16 64]``

at the prompt. MATLAB returns

````A =`
`    1 1   1    1`
`    1 2   4    8`
`    1 3   9    27`
`    1 4 16   64`
```

There are a few basic rules that are used when entering matrices or vectors. First, a vector or matrix is started by using a square bracket [ and ended using a square bracket ]. Next, blanks or commas separate the elements of a row. A semicolon is used to end each row. Finally, we may place a semicolon at the very end to prevent MATLAB from displaying the output of the command.

To define a row vector, use blanks or commas. For example,

````>> x = [2, 4, 6, 8, 10, 12]`

`x =`
`     2   4   6   8   10   12````

To define a column vector, use semicolons. For example,

````>> y=[1;3;5;7]`

`y =`
`   1`
`   3`
`   5`
`   7````

In order to access a particular element of y, put the desired index in parentheses. For example, y(1) = 1, y(2) = 3, and so on.

MATLAB provides a useful notation for addressing multiple elements at the same time. For example, to access the third, fourth, and fifth elements of x, we would type

````>> x(3:5)`

`ans =`
`    6   8   10````

The 3:5 tells MATLAB to start at 3 and count up to 5. To access every second element of x, you can do this by

````>> x(1:2:6)`

`ans =`
`    2   6   10````

We may do this for the array also. For example,

````>> A(1:2:4,2:2:4)`

`ans =`
`    1   1`
`    3  27`
```

The notation 1:n may also be used to assign to a variable. For example,

``>> x=1:7 ``

returns

````x =`
`    1   2   3   4   5   6   7````

MATLAB provides the size function to determine the dimensions of a vector or matrix variable. For example, if we want the dimensions of the matrix  that we entered earlier, then we would do

````>> size(A)`

`ans =`
`    4   4````

It is often necessary to display numbers in different formats. MATLAB provides several output formats for displaying the result of a computation. To find a list of formats available, type

``>> help format``

The short format is the default format and is very convenient for doing many computations. However, in this book, we will be representing long whole numbers, and the short format will cut off some of the trailing digits in a number. For example,

````>> a=1234567899`

`a =`
`   1.2346e+009````

Instead of using the short format, we shall use the rational format. To switch MATLAB to using the rational format, type

``>> format rat``

As an example, if we do the same example as before, we now get different results:

````>> a=1234567899`

`a =`
`   1234567899````

This format is also useful because it allows us to represent fractions in their fractional form, for example,

````>> 111/323`

`ans =`
`   111/323````

In many situations, it will be convenient to suppress the results of a computation. In order to have MATLAB suppress printing out the results of a command, a semicolon must follow the command. Also, multiple commands may be entered on the same line by separating them with commas. For example,

````>> dogs=11, cats=7; elephants=3, zebras=19;`

`dogs =`
`   11`

`elephants =`
`   3````

returns the values for the variables dogs and elephants but does not display the values for cats and zebras.

MATLAB can also handle variables that are made of text. A string is treated as an array of characters. To assign a string to a variable, enclose the text with single quotes. For example,

``>> txt=’How are you today?’``

returns

````txt =`
`   How are you today?````

A string has size much like a vector does. For example, the size of the variable txt is given by

````>> size(txt)`
`ans =`
`    1   18````

It is possible to edit the characters one by one. For example, the following command changes the first word of txt:

````>> txt(1)=’W’; txt(2)=’h’;txt(3)=’o’`

`txt =`
`   Who are you today?````

As you work in MATLAB, it will remember the commands you have entered as well as the values of the variables you have created. To scroll through your previous commands, press the up-arrow and down-arrow. In order to see the variables you have created, type who at the prompt. A similar command whos gives the variables, their size, and their type information.

Notes. 1. To use the commands that have been written for the examples, you should run MATLAB in the directory into which you have downloaded the file from the Web site `bit.ly/2HyvR8n`

2. Some of the examples and computer problems use long ciphertexts, etc. For convenience, these have been stored in the file `ciphertexts.m`, which can be loaded by typing ciphertexts at the prompt. The ciphertexts can then be referred to by their names. For example, see Computer Example 4 for Chapter 2.