# 1. Create a new vector

You can use one vector in a list for another one. Type in the following:
a = [4,5,6];
b = [2, 3, 4];
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) 2,4,6,8,10
(b) 1/2, 1, 3/2, 2, 5/2
(c) 1, 1/2, 1/3, 1/4, 1/5
(d) 1, 1/2^2, 1/3^2, 1/4^2, 1/5^2

Matlab code:

a=[2,4,6,8,10]

a =

2     4     6     8    10

>> b=[1/2,1,1/3,2,5/2]

b =

0.5000    1.0000    0.3333    2.0000    2.5000

>> c=[1,1/2,1/3,1/4,1/5]

c =

1.0000    0.5000    0.3333    0.2500    0.2000

>> d=[1,1/2^2,1/3^2,1/4^2,1/5^2]

d =

1.0000    0.2500    0.1111    0.0625    0.0400

<span style="background-color:yellow"> You are not supposed to write these explicitly  (-1 pt)</span>

= 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:
<pre>

>> a=[1,2,3;4,5,6;7,8,9]

a =

1     2     3
4     5     6
7     8     9

>> a(1:3,2)=a(1:3,3)

a =

1     3     3
4     6     6
7     9     9

>> a(1:3,3)=[2;5;8]

a =

1     3     2
4     6     5
7     9     8

>> a(1:3,4)=0

a =

1     3     2     0
4     6     5     0
7     9     8     0

# 3. Matrix Operation

The following matrix is defined in MATLAB:

$\mathbf {N} =\begin{bmatrix} 15 & 1 & 9 & 14 & 30 \\ 20 & 28 & 11 & 18 & 4 \\ 16 & 15 & 6 & 2 & 3 \\ 14 & 12 & 1 & 6 & 4 \\\end{bmatrix}.$

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)

A =

15    28
1    11
9    18
14     4

>> B=[N(:,3)',N(3,:)'] Error using horzcat Dimensions of matrices being concatenated are not consistent.

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

C =

0     0     0     0     0     0
0     0     0     0     0     0
0     0     0     0    18     4
0     0     0     0     2     3

. B is doable (-0.5 pt)

# 4. Matrix Calculation

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

$\mathbf{A} = \begin{bmatrix} 2 & 4 \\ 1 & 3 \\ 4 & 5 \\\end{bmatrix}.$
$\mathbf{B} = \begin{bmatrix} 2 & 1 & 4\\ 4 & 3 & 5\\\end{bmatrix}.$

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

d) Compute (AB)20

Matlab code:

>> A=[2,4;1,3;4,5]

A =

2     4
1     3
4     5

>> B=[2,1,4;4,3,5]

B =

2     1     4
4     3     5

>> sum(A)

ans =

7    12

>> sum(sum(B))

ans =

19

>> (A*B)^20

ans =

1.0e+36 *
2.0923    1.4430    2.9941
1.4430    0.9952    2.0649
2.9941    2.0649    4.2846

>> (A*B')^20 Error using * Inner matrix dimensions must agree.

C is missing (-0.5 pt)

Missing 7 questions (-7 pt)