Exercise 1: Basic Syntax and Command-Line Exercises

The following exercises are meant to be answered by a single MATLAB command. The command may be involved (i.e., it may use a number of parentheses or calls to functions) but can, in essence, be solved by the execution of a single command. If the command is too complicated, feel free to break it up over two or more lines.

 

1. Create a vector of the odd whole numbers between 31 and 75.

 

2. Let x = [2 6 1 4 9 3 7 2 8 5].

 

  a. Add 12 to each element

  b. Insert one element in the end of x and set the value to 5.

  c. Add 3 to the odd-index elements only.

  d. Compute the square of each element.

  e. Compute the square root of each element.

  f. Delete the odd-index elements of x

 

3. Let x = [3 1 7 8]' and y = [4 2 3 5]' (NB. x and y should be column vectors).

 

  a. Sum up the vectors of x and y.

  b. Divide each element of x by the corresponding element in y

  c. Raise each element of x to the power specified by the corresponding

     element in y.

  d. Multiply each element in x by the corresponding element in y, name the  result "z".

  e. Add up the elements in z

  f. Compute x'*y - z.

 

4. Evaluate the following MATLAB expressions by hand and use MATLAB to check the answers

 

  a. 2 / 2 * 3

  b. 6 - 2 / 5 + 7 ^ 2 - 1

  c. 10 / 2 \ 5 - 3 + 2 * 4

  d. 3 ^ 2 / 5

  e. 3 ^ 2 ^ 2

 

5. Given the vector x = -1:0.1:1, explain the results of the following commands:

 

  a. round(x)

  b. floor(x)

  c. ceil(x)

  d. fix(x)

 

6. Create a vector x with the elements ...

 

  a. 2, 4, 6, 8, 10, 12, 14, ..., 30

  b. 10, 8, 6, 4, 2, 0, -2, -4, ..., -20

  c. 1, 1/2, 1/3, 1/4, 1/5, ..., 1/20

  d. 0, 1/2, 2/3, 3/4, 4/5, ..., 19/20

  e. 1, 3, 5, 7, 9, ..., 25, 27, 29, 31, 29, 31, 29, 27, 25, ..., 9, 7, 5, 3, 1

 

7. Create a vector x with the elements,

 

              xn = (-1)n+3/(2n-1)

             

   Add up the elements of the version of this vector that has 200 elements.

  

    

8. Let t = 1:0.2:3, write down the MATLAB expressions that will correctly compute the following:

 

  a. ln(2 + t + t2)

  b. et(1 + cos(3t))

  c. cos2(t) + sin2(t)

  d. cot(t)

  e. sec2(t) + cot(t) - 1

  f. tan-1(t)  (this is the inverse tangent function)