{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars} #Muhammad Fahrillah Haekal Yahya #3338250043, 3B #Komstat 11
newtonraphson <- function(ftn, x0, tol = 1e-9, max.iter = 100) { x <- x0 fx <- ftn(x) iter <- 0 while ((abs(fx[1]) > tol) && (iter < max.iter)) { x <- x - fx[1] / fx[2] fx <- ftn(x) iter <- iter + 1 cat(“At iteration”, iter, “value of x is:”, x, “”) } if (abs(fx[1]) > tol) { cat(“Algorithm failed to converge”) return(NULL) } else { cat(“Algorithm converged”) return(x) } }
secant <- function(ftn, x0, x1, tol = 1e-9, max.iter = 100) { f0 <- ftn(x0) f1 <- ftn(x1) iter <- 0 while ((abs(f1) > tol) && (iter < max.iter)) { if (f1 == f0) { cat(“error: f(x_i) = f(x_{i-1}), pembagi nol”) return(NULL) } x2 <- x1 - f1 * (x1 - x0) / (f1 - f0) x0 <- x1; f0 <- f1 x1 <- x2; f1 <- ftn(x1) iter <- iter + 1 cat(“At iteration”, iter, “value of x is:”, x1, “”) } if (abs(f1) > tol) { cat(“Algorithm failed to converge”) return(NULL) } else { cat(“Algorithm converged”) return(x1) } } #soal 1 f <- function(x) x^3 - x - 2 df <- function(x) 3*x^2 - 1 fn <- function(x) c(f(x), df(x))
r_newton <- newtonraphson(fn, 1.5, tol = 1e-10) print(r_newton, digits = 12)
r_bis <- bisection(f, 1, 2, tol = 1e-10)
u <- uniroot(f, c(1, 2), tol = 1e-10) u\(root; u\)iter
#soal 2 ft <- function(x) c(pt(x, 9) - 0.975, dt(x, 9))
newtonraphson(ft, 1, tol = 1e-10) qt(0.975, 9)
try(newtonraphson(ft, 10, tol = 1e-10))
#soal 3 f3 <- function(x) cos(x) - x fn3 <- function(x) c(cos(x) - x, -sin(x) - 1)
secant(f3, 0, 1, tol = 1e-10) newtonraphson(fn3, 1, tol = 1e-10)
#soal 4 x <- c(4.79, 1.61, 12.99, 6.96, 7.18, 13.64, 8.29, 9.65, 6.50, 7.92, 6.15, 7.37, 13.02, 5.11, 3.25, 3.44, 5.99, 14.56, 4.43, 5.96, 12.00, 9.93, 4.66, 12.66, 28.93, 34.16, 4.90, 14.51, 15.94, 8.54)
n <- length(x) s <- log(mean(x)) - mean(log(x)) # ln(x-bar) - (1/n) sum ln(xi)
f4 <- function(a) c(log(a) - digamma(a) - s, # f(alpha) 1/a - trigamma(a)) # f’(alpha)
a0 <- 1/(2*s) # tebakan awal a_hat <- newtonraphson(f4, a0, tol = 1e-10) print(a_hat, digits = 10)
rate_hat <- a_hat / mean(x) # rate = alpha / x-bar rate_hat
library(MASS) fitdistr(x, “gamma”)
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.