Numerical Methods: Assignment 2

  1. Complete sections 1 (Conditionals and Control Flow) and 2 (Loops) of the “Intermediate R” course at DataCamp. If you’ve already done this in the past, then your account at DataCamp should reflect this. If for some reason it doesn’t, then you can show me that you’ve done it.

  2. Let \(x=3248\). Use the functions floor() and/or %% to write an expression that returns the second decimal place in \(x\). Make sure your code also works for \(x = -3248\).

    # notice the spacing of this chunk...what is its purpose? 
    x <- 3248
    # write question 1 code inside this chunk
  3. Write an expression that returns the following matrix.
    \(\left(\begin{array}{rrr}0 & 2 & 3 \\0 & 5 & 0 \\7 & 0 & 0\end{array}\right)\)

    # write question 2 code inside this chunk
  4. Produce a vector containing all integers from 1 to 100 that are not divisible by 2, 3, or 7. You want to achieve this by appropriately subsetting the vector 1:100.

    # write question 3 code inside this chunk
  5. Write a simple expression that returns a \(10\times 10\) diagonal matrix whose nonzero entries are each equal to 3.

  6. Exercise 3.9.1. The author mentions the ifelse() function. I would avoid using this function. See here. There are much better approaches.

  7. Exercise 3.9.2. Use \(x=0.8\) and \(n=10\).

  8. Same as the previous exercise except don’t use a loop. That is, use a vectorized expression.

  9. Exercise 3.9.3. Does your code for the formula handle the \(x=1\) case?

  10. Exercise 3.9.5. A test vector is provided. Why is it a matrix? Also, a test angle, \(\theta\), is given. Why are these good choices for a test vector and angle?

    x <- matrix(c(1, 0), 2, 1)
    theta <- pi / 2
  11. Exercise 3.9.6.

  12. Exercise 3.9.7.

  13. Exercise 3.9.10.