Source file ⇒ Assignment_6.Rmd
f <- function(year) {
if (year %% 4 == 0 & year %% 100 != 0 | year %% 400 == 0) {
return(TRUE)
} else {
return(FALSE)}
}
f(2001)
## [1] FALSE
y <- 0
number_of_leap_years_lived <- function(yyyy) {
for(year in yyyy:2016) {
if (f(year) == TRUE)
y <- y + 1}
print(y)
}
number_of_leap_years_lived(1995)
## [1] 6
2a)
x_unif <- runif(100, min = -1, max = 1)
y_unif <- runif(100, min = -1, max = 1)
x_y_coordinates <- data.frame(x_unif, y_unif)
vectors_x_y <- function(x , y) {
unit_circle_dist <- sqrt((x^2)+(y^2))
if (unit_circle_dist <= 1) {
return(TRUE)
} else {
return(FALSE)}
}
count <- mapply(vectors_x_y,x = x_unif, y = y_unif)
proportion <- sum(count/100)
print(paste("The proportion of points within the unit circle is", proportion))
## [1] "The proportion of points within the unit circle is 0.81"
#This is the ggplot graph
ggplot(x_y_coordinates, aes(x = x_unif, y = y_unif, color = count)) + geom_point()
area_approximation <- proportion*4
print(paste("The area of the square being sampled (i.e. approximate area)", area_approximation))
## [1] "The area of the square being sampled (i.e. approximate area) 3.24"
2b)
vectors_x_y <- function(x , y) {
unit_circle_dist <- sqrt((x^2)+(y^2))
if (unit_circle_dist <= 1) {
return(TRUE)
} else {
return(FALSE)}
}
sampling <- function(n, plotit = FALSE) {
x_unif <- runif(n, min = -1, max = 1)
y_unif <- runif(n, min = -1, max = 1)
x_y_coordinates <- data.frame(x_unif, y_unif)
count <- mapply(vectors_x_y,x = x_unif, y = y_unif)
proportion <- sum(count/n)
area_approximation <- proportion*4
if(plotit == TRUE) {
ggplot(x_y_coordinates, aes(x = x_unif, y = y_unif, color = count)) + geom_point()
} else {
return(area_approximation)}
}
sampling(300, plotit = TRUE)
2c)
fifty_sample <- replicate(100, sampling(50))
five_hundred_sample <- replicate(100, sampling(500))
2d)
graph <- data.frame(fifty_sample,five_hundred_sample)
glyph_ready_graph <- graph %>% gather(key = n, value = area_approximation, fifty_sample, five_hundred_sample)
ggplot(glyph_ready_graph, aes(x = area_approximation)) + geom_histogram(binwidth = 0.1) + facet_grid(.~n) + geom_vline(xintercept = 3.142)
3a)
newtons_function <- function(initial_guess, tolerance = 0.00001) {
fx <- x^3 + 2*x^2 - 7
while(abs(fx) > tolerance){
fpx <- 3*x^2 + 4*x
x <- x - fx/fpx
fx <- x^3 + 2*x^2 - 7}
return(initial_guess)}
3b)
function_f <- function(x) {
x^3 + 2*x^2 - 7
}
function_fp <- function(x) {
3*x^2 + 4*x
}
3c)
newton_updated <- function(f, fp, x, tolerance=0.00001) {
fx <- f(x)
while(abs(fx) > tolerance) {
fpx <- fp(x)
x <- x - fx/fpx
fx <- f(x)
}
return(x) # (approximate) root
}
3d)
f1 <- function(x) {
x^5 + 19
}
f1p <- function(x) {
5*x^4
}
newton_updated(f1, f1p, -1)
## [1] -1.801983
xseq <- seq(-5, 5, length = 100) #100 x values between -5 and 5
yseq <- xseq^3 + 2*xseq^2 - 7 #100 corresponding y values according to function y=x^3+ 2x^2-7
df <- data.frame(xseq,yseq) #make a data frame with variables xseq and yseq
p <- df %>% ggplot(aes(x=xseq, y=yseq)) + geom_line(stat="identity", col="red")
p + geom_hline(yintercept = 0)