Numerical Methods: Assignment 3

  1. Complete sections 1 (quick refresher), 2 (when and how you should write a function) and 3 (functional programming) of the “Writing Functions in 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. Exercise 5.7.1. Note that you are asked here to write a function and not simply an expression.

  3. Exercise 5.7.2

  4. Exercise 5.7.3. You can omit part (d) but you should read the rationale.

    1. Simulate one round of the game described in the problem. This shouldn’t be a function.

    2. Now a function:

    3. To be clear, this function will provide an estimate of the probability of winning (getting at least one six in n rolls) by simulating the game N times and calculating the proportion of wins. The output should be a vector containing the estimate, the exact probability and the difference between them.

  5. Exercise 5.7.5

  6. What will be the output of the following code? Try to answer before you run the code.

    rabbit <- function(n) {
      if (n == 1 || n == 2) {
        1
      } else {
        rabbit(n - 2) + rabbit(n - 1)
      }
    }
    rabbit(10)
  7. Exercise 5.7.7