We shall use MATLAB to compute addition and scalar multiplication of vectors in two and three dimensions. This will serve the purpose of introducing some basic MATLAB commands.

Entering Vectors and Vector Operations

Begin a MATLAB session. We now discuss how to enter a vector into MATLAB. The syntax is straightforward; to enter the row vector type1

x = [1 2 1]

and MATLAB responds with
x =
 
     1     2     1

Next we show how easy it is to perform addition and scalar multiplication in MATLAB. Enter the row vector by typing

y = [2 -1 1]

and MATLAB responds with
y =
 
     2    -1     1

To add the vectors and , type
x + y

and MATLAB responds with
ans =
 
     3     1     2

This vector is easily checked to be the sum of the vectors and . Similarly, to perform a scalar multiplication, type
                                                                  

                                                                  
2*x

which yields
ans =
 
     2     4     2

MATLAB subtracts the vector from the vector in the natural way. Type
x - y

to obtain
ans =
 
    -1     3     0

We mention two points concerning the operations that we have just performed in MATLAB.

(a)
When entering a vector or a number, MATLAB automatically echoes what has been entered. This echoing can be suppressed by appending a semicolon to the line. For example, type
     z = [-1 2 3];
     

and MATLAB responds with a new line awaiting a new command. To see the contents of the vector just type z and MATLAB responds with
                                                                  

                                                                  
     z =
      
    -1     2     3

(b)
MATLAB stores in a new vector the information obtained by algebraic manipulation. Type
     a = 2*x - 3*y + 4*z;
     

Now type a to find
     a =
      
    -8    15    11

We see that MATLAB has created a new row vector with the correct number of entries.

Note: In order to use the result of a calculation later in a MATLAB session, we need to name the result of that calculation. To recall the calculation 2*x - 3*y + 4*z, we needed to name that calculation, which we did by typing a = 2*x - 3*y + 4*z. Then we were able to recall the result just by typing a.

We have seen that we enter a row vector into MATLAB by surrounding a list of numbers separated by spaces with square brackets. For example, to enter the -vector just type

w = [1 3 5 7 9]

Note that the addition of two vectors is only defined when the vectors have the same number of entries. Trying to add the -vector with the -vector by typing x + w in MATLAB yields the warning:
??? Error using ==> +
 
Matrix dimensions must agree.

In MATLAB new rows are indicated by typing ;. For example, to enter the column vector just type:

z = [-1; 2; 3]

and MATLAB responds with
z =
 
    -1  
     2  
     3

Note that MATLAB will not add a row vector and a column vector. Try typing x + z.

Individual entries of a vector can also be addressed. For instance, to display the first component of z type z(1).

Entering Matrices

Matrices are entered into MATLAB row by row with rows separated either by semicolons or by line returns. To enter the matrix just type

A = [2 3 1; 1 4 7]

MATLAB has very sophisticated methods for addressing the entries of a matrix. You can directly address individual entries, individual rows, and individual columns. To display the entry in the row, column of , type A(1,3). To display the column of , type A(:,2); and to display the row of , type A(1,:). For example, to add the two rows of and store them in the vector , just type

                                                                  

                                                                  
x = A(1,:) + A(2,:)

MATLAB has many operations involving matrices — these will be introduced later, as needed.

Exercises

Enter the matrix As usual, let denote the entry of in the row and column. Use MATLAB to compute the following:
(a)
.
(b)
Three times the column of .
(c)
Twice the row of minus the row.
(d)
The sum of all of the columns of .

Verify that MATLAB adds vectors only if they are of the same type, by typing
(a)
x = [1 2], y = [2; 3] and x + y.
(b)
x = [1 2], y = [2 3 1] and x + y.

In Exercises ?? – ??, let and use MATLAB to compute the given expression.

.
.

In Exercises ?? – ??, let and use MATLAB to compute the given expression.

.
.