# 2015F001/Homework 6

### From CDS 130

# 1. Create a new vector

You can use one vector in a list for another one. Type in the following:

a = [2 3 4 5];

b = [2, 3, 4, 5];

c = [a, -b];

Work out what c will look like before displaying it.

# 2. Vector operations

Set up a vector n with elements 1, 2, 3, 4, 5. Use MATLAB array operations on it to set up the following four vectors, each with five elements:

(a) 3,4,5,6,7 (b) 1/2, 1, 3/2, 2, 5/2 (c) 1/2, 1/4, 1/6, 1/8 (d) 1, 1/2^2, 1/3^2, 1/4^2, 1/5^2

Matlab code:

# 3. Vector Assignment

Use a single command to create a row vector (assign it to a variable named *b*) with 19 elements such that

*b* = [ 1 1/2 1/3 1/4 1/5 1/6 1/7 1/8 1/9 1/10 1/9 1/8 1/7 1/6 1/5 1/4 1/3 1/2 1 ]

**Do not** type the vector explicitly.

# 4. Vector Arithmetic

Define the variables *x* = 15, *y* = 25 , and then use them to create a column vector (with 10 elements) that has the following elements: *y*, *e*^{y}, ln(*x* / *y*), , and *x* − *y*.

# 5. Create a Vector

Create a column vector with 9 equally spaced elements in which the first element is -2 and the last element is 25.

# 6. Create a Matrix

Create a 5x5 matrix and fill it initially with zeros. Next, store into matrix elements (1,3), (2,4), (3,5), (4,1) and (5,2) values equal to the row number plus the column number for that element. Print out the matrix. Finally, add 3 to every element of the matrix and save each result back into the matrix. Print out the matrix.

Matlab code:

# 7. Create a Matrix

Create a 8 by 8 matrix with all elements having random values between 0 and 5

Matlab code:

# 8. Create a Matrix

Create the following matrix by using vector notation (i.e. the colon operator) for creating vectors with constant spacing. Do not type individual elements explicitly.

Matlab code:

# 9. Let x=(0:pi/60:2*pi). What Matlab commands will create a matrix whose ﬁrst row is x, whose second row is the sine of each entry in x, and whose third row is the cosine of each entry in x?

# 10. Matrix Operation

Set up any 3 x 3 matrix *A* with nonempty elements. Write some command-line statements to perform the following operations:

(a) Interchange column 2 and 3;

(b) Add a fourth column (of 0s);

Matlab code:

# 11. Matrix Operation

The following matrix is defined in MATLAB:

By hand (pencil and paper) write what will be displayed in the following commands are executed by MATLAB. Check your answers by executing the commands with MATLAB

a. A=[N(1,1:4)’, N(2,2:5)’]

b. B=[N(:,3)’ N(3,:)]

c. C(3:4,5:6)=N(2:3,4:5)

# 12. Matrix Calculation

Matlab has been widely used for matrix calculations. Assign the following matrix to variable A and B, respectively.

Use MATLAB to perform the following calculation, and record the MATLAB output below.

a) sum(A):

b) sum(sum(B));

c) Compute (*A*. * *B*')^{20} [4,16;1,9;16,25]^20

d) Compute (*A**B*)^{20}

Matlab code: