Simulation HW 1

  1. Chapter 3, Exercise 2 Let h(x,n) = 1+x+x2 +···+
#for n = 0
#h(x,0) = 1

#for n=1
#h(x, 1)= 1 + x

#for n = 2
#h(x, 2)= 1 + x + x^2
n=6
h=1
x=1
for (i in 1:n){
  h <- h + x^i
  #print(h)
}

print(h)
## [1] 7
  1. Chapter 3, Exercise 7 How would you find the sum of every third element of a vector x?
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#sample <- c(1,2,3,4,5,6,7)

sum_3rd <- function(x) {
  x[seq(0, length(x), by = 3)] %>%
  sum()
}

#sum_3rd(sample)
  1. Chapter 3, Exercise 10 Write a program that uses a loop to find the minimum of a vector x, without using any predefined functions like min(…) or sort(…). You will need to define a variable, x.min say, in which to keep the smallest value you have yet seen. Start by assigning x.min <- x[1] then use a for loop to compare x.min with x[2], x[3], etc. If/when you find x[i] < x.min, update the value of x.min accordingly.
#test <- c(10:1, 50)
Minimum_function <- function(x){
  x.min <- x[1]
  for (i in 1:length(x)){
    if (x[i] < x.min) {
      x.min <- x[i] 
    }
  }
  print(x.min)
}

#Minimum_function(test)
  1. Chapter 3, Exercise 12 The game of craps is played as follows. First, you roll two six-sided dice; let x be the sum of the dice on the first roll. If x = 7 or 11 you win, otherwise you keep rolling until either you get x again, in which case you also win, or until you get a 7 or 11, in which case you lose. Write a program to simulate a game of craps. You can use the following snippet of code to simulate the roll of two (fair) dice: x <- sum(ceiling(6*runif(2)))
#set.seed(1) for testing

craps_testing <- function() {
  roll_1 <- sum(ceiling(6*runif(2)))
  if (roll_1 == 7 | roll_1 == 11) {print( paste("rolled a", roll_1, "you win"))}
  else {
  print(roll_1)
  dice_roll <- sum(ceiling(6*runif(2)))
  while (dice_roll != roll_1 & dice_roll != 7 & dice_roll != 11)
 {
    print(dice_roll)
dice_roll <- sum(ceiling(6*runif(2)))
  }
  if (dice_roll == roll_1) print(paste("Your roll matched your first roll, YOU WIN!!!", dice_roll))
  else print(paste("You lose, life is hard...", dice_roll))
}}

craps_testing()
## [1] 5
## [1] "You lose, life is hard... 11"

Answer

craps <- function() {
  roll_1 <- sum(ceiling(6*runif(2)))
  if (roll_1 == 7 | roll_1 == 11) {print( paste("rolled a", roll_1, "you win"))}
  else {
  dice_roll <- sum(ceiling(6*runif(2)))
  while (dice_roll != roll_1 & dice_roll != 7 & dice_roll != 11)
 {
dice_roll <- sum(ceiling(6*runif(2)))
  }
  if (dice_roll == roll_1) print("Your roll matched your first roll, YOU WIN!!!")
  else print("You lose, life is hard...")
}}

craps()
## [1] "rolled a 11 you win"
  1. Chapter 3, Exercise 13 Suppose that (x(t), y(t)) has polar coordinates (t, 2πt). Plot (x(t), y(t)) for t ∈ [0, 10]. Your plot should look like Figure 3.3.
t <- seq(0, 10, by = 0.0001)
x <- sqrt(t) *cos(2*pi*t)
y <- sqrt(t) *sin(2*pi*t)
plot(x, y, cex=.1)