Quiz 3 - Go!!

Instructions

  1. You are not expected to finish the entire quiz in the allotted time. It has been designed to be reasonably challenging so that, among other things, everyone in the class gets a good Matlab workout.

  2. Hint: Don't forget about the linspace command. If you didn't remember it enough to forget it, use

    > help linspace
    

    to refamiliarize yourself with it.

  3. Note that these instructions are identical to the ones for Quiz 2, except for the specification of the file that you must create to complete the quiz. In particular, the name of the startup script is again

    q2_setup
    

    I am repeating the instructions here so that the quiz is self-contained.

  4. All of the commands that you use to complete this quiz must be entered in the file

    /phys210/$LOGNAME/matlab/q3.m
    
  5. The first statement in this file must be

    q2_setup
    

    If this line is not included you will be unable to complete the quiz. Also, if you prefer to work the questions interactively and then transfer your answers to q3.m be sure to type

    >> q2_setup
    

    before you do anything else.

  6. q2_setup is a script that defines 13 variables: 8 row vectors

    v1, v1_el, m1_row, m1_col, w1, w2, w3 and w4
    

    and 5 matrices

    m1, a1, a2, a3, a4
    

    You can display any of these by simply entering their names at the Matlab prompt, but be warned that v1 and m1 contain many elements. I'll leave it as an exercise for you to determine how many.

  7. Try to answer the questions using as few Matlab statements as possible, but note that in several cases it may be advantageous to first copy a vector or matrix and then modify it, i.e,

    >> foo1 = foo
    >> foo1(...) = ...
    

    I will use (copy) to label the instances where I suggest you do this.

    I will indicate the number of statements you are expected to use in the parenthesis following the question label.

  8. IMPORTANT: Several questions ask you to create vectors or matrices by selecting subsets of elements other vectors and matrices. Unless specified otherwise, the elements in the new arrays should be in the "same order" as those in the original.

    Example 1: Given

    a = 1  2  3  4  5  6  7  8  9  10
    

    the vector

    a1 = 3  9  10
    

    is a subset of elements of a that are in the "same order" whereas

    a2 = 9  3  10
    

    is not.

    Example 2: Given

    m =
        10  15  20  25  
        30  35  40  45
        50  55  60  75
    

    the matrix

    m1 =
        10  25
        50  75
    

    is a subset of elements of a that are in the "same order" whereas

    m2 =
        20  15
        35  40
    

    is not.

    I will denote the questions where the order of elements is to be preserved in this manner with (order)

OK, on with the quiz, and remember that the first line in q3.m, or the first command that you type should you wish to first solve the problems interactively, must be

q2_setup

Q3.1 (1)

a1, a2, a3 and a4 are all 4 x 4 matrices. Use them to construct an 8 x 8 matrix m7 having the structure:

\[ \left[ \begin{array}{cc} a_1 & a_2 \\\ a_3 & a_4 \end{array}\right] \]

Q3.2 (1)

Denoting the 4 x 4 zero and identity matrix by 0 and 1, respectively, construct a 12 x 8 matrix m8 that has the structure:

\[ m_8 \equiv \left[ \begin{array}{cc} {\bf 0} & a_2 \\\ a_3 & a_4^T \\ a_1 & {\bf 1} \end{array}\right] \]

where \( a_4^T \) denotes the transpose of \( a_4 \).

Q3.3 (1)

Construct the following vector using a single assignment statement:

\[ v_7 \equiv \left[ \begin{array}{c} 0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 \end{array}\right] \]

Assign to the variable v7.

Q3.4 (2)

Construct the following vector using at most two assignment statements:

\[ v_8 \equiv \left[ \begin{array}{c} 0 & 0 & 2 & 0 & 0 & 0 & 0 & 0 & 0 & 8 \end{array}\right] \]

Assign to the variable v8.

Q3.5 (1)

Construct the following matrix using a single assignment statement: \[ m_9 \equiv \left[ \begin{array}{ccccccc} 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 2 \end{array}\right] \]

Assign to the variable m9.

Q3.6 (2)

Define a column vector m1_md that contains every 10th element of the main diagonal of m1, starting with the first element of the diagonal.

Q3.7 (1)

Construct a matrix m10 which has a main diagonal given by every 11073-rd element of v1, starting with the 2001-st element, with 0 elements everywhere else.

Q3.8 (1)

Use a single Matlab statement to produce the following matrix:

\[ m_{11} \equiv \left[ \begin{array}{ccccccc} 0.0500 & 0.0600 & 0.0700 & 0.0800 & 0.0900 & 0.1000 & 0.1100 \\ 0.1200 & 0.1300 & 0.1400 & 0.1500 & 0.1600 & 0.1700 & 0.1800 \\ 0.1900 & 0.2000 & 0.2100 & 0.2200 & 0.2300 & 0.2400 & 0.2500 \\ 0.2600 & 0.2700 & 0.2800 & 0.2900 & 0.3000 & 0.3100 & 0.3200 \\ 0.3300 & 0.3400 & 0.3500 & 0.3600 & 0.3700 & 0.3800 & 0.3900 \\ 0.4000 & 0.4100 & 0.4200 & 0.4300 & 0.4400 & 0.4500 & 0.4600 \\ 0.4700 & 0.4800 & 0.4900 & 0.5000 & 0.5100 & 0.5200 & 0.5300 \\ 0.5400 & 0.5500 & 0.5600 & 0.5700 & 0.5800 & 0.5900 & 0.6000 \\ 0.6100 & 0.6200 & 0.6300 & 0.6400 & 0.6500 & 0.6600 & 0.6700 \\ 0.6800 & 0.6900 & 0.7000 & 0.7100 & 0.7200 & 0.7300 & 0.7400 \end{array}\right] \]

Assign to the variable m11.

Q3.9 (copy, 3)

Use 3 Matlab statements, including a copy, to make a matrix m12 in which the upper left 3 x 3 submatrix of m11 has been replaced with the 3 x 3 identity matrix, and the lower right 4 x 4 submatrix has elements:

\[ \left[ \begin{array}{cccc} 16 & 15 & 14 & 13 \\ 12 & 11 & 10 & 9 \\ 8 & 7 & 6 & 5 \\ 4 & 3 & 2 & 1 \end{array}\right] \]

Q3.10 (2)

Evaluate the following function at 101 uniformly spaced values of \(x\) ranging from \(-\pi/2\) to \(\pi/3\) inclusive.

\[ \sin\left(x^3\right) + \frac{\cos\left(\sqrt{|x|^4}\right)}{x+1} + 6 \]

Assign the result, which should be a row vector of length 101, to the variable fv. You should first create a row vector with a name of your own choosing (other than a name that is used in one of the other questions in the quiz) to store the values of \(x\).

Q3.11 (3)

Set up and solve the following linear system:

\[ \begin{eqnarray} 78 v + 17 w + 4 y + 88 z &=& 64 \\ 44 v + 85 w + 11 z &=& 42 \\ 3 v + 15 w + 48 y + 37 z &=& 62 \\ 12 v + 28 w + 80 y + 98 z &=& 57 \\ \end{eqnarray} \]

Assign the solution vector (a column vector) \[ \left[\begin{array}{c} v \\ w \\ y \\ z \end{array}\right] \] to x_44.

Choose variable names of your own for the coefficient matrix and right hand vector of the system.

Q3.12 (2)

Find the maximum and minimum values of the vector v1. Assign to the variables v1_max and v1_min respectively.

Q3.13 (2 assignment statements, function nesting allowed)

Use the sort command (and some other piece of syntax) to find the maximum value of the vector cos(v1) without using the max function. Assign to the variable cos_v1_max.

Q3.14 (2)

Find the average (mean) and median values of the matrix m1. Assign to the variables m1_mean and m1_median respectively, and note that both answers should be a single scalar value (i.e. not a vector).

Q3.15 (BONUS)

Compute \(17!\) using an assignment statement with exactly 10 non white-space characters on the right hand of the = sign. Assign to fact_17 (the value will be not be exact, as it is in Maple).

Q3.16 (BONUS)

How many elements in m1 are equal to 23? Assign the value to n23.

This can be done with one statement using nested function calls, but you certainly aren't expected to know how to do it at this point.

Try to resist using google, though.

Hint: The Matlab relational operator for equality is == and Matlab uses the values 1 and 0 for Boolean (logical) true and false, respectively. Try evaluating

2 == 2

and

3 == 2

Finishing up

IMPORTANT

When you've completed the quiz, execute the following from the terminal command line:

% date > /phys210/$LOGNAME/matlab/date

AS USUAL, YOU CAN THEN WORK ON THE HOMEWORK OR TALK TO ME ABOUT YOUR TERM PROJECTS WHILE YOU WAIT FOR THE CLASS TO FINISH THE QUIZ