newtonraphson <- function(ftn, x0, tol = 1e-9, max.iter = 100, trace = TRUE) {
x <- x0
fx <- ftn(x)
iter <- 0
while ((abs(fx[1]) > tol) && (iter < max.iter)) {
if (is.na(fx[2]) || fx[2] == 0) {
if (trace) cat("Iterasi", iter, ": f'(x) = 0 atau NA, berhenti.\n")
return(list(root = NA, iter = iter, converged = FALSE))
}
x <- x - fx[1] / fx[2]
fx <- ftn(x)
iter <- iter + 1
if (trace) {
cat("At iteration", iter, "value of x is:", format(x, digits = 12),
"| f(x) =", format(fx[1], digits = 4), "\n")
}
}
converged <- abs(fx[1]) <= tol
if (trace) {
if (!converged) cat("Algorithm failed to converge\n")
else cat("Algorithm converged\n")
}
list(root = x, iter = iter, converged = converged)
}
secant <- function(ftn, x0, x1, tol = 1e-9, max.iter = 100, trace = TRUE) {
f0 <- ftn(x0)
f1 <- ftn(x1)
iter <- 0
while ((abs(f1) > tol) && (iter < max.iter)) {
if (f1 == f0) {
if (trace) cat("error: f(x_i) = f(x_{i-1}), pembagi nol\n")
return(list(root = NA, iter = iter, converged = FALSE))
}
x2 <- x1 - f1 * (x1 - x0) / (f1 - f0)
x0 <- x1; f0 <- f1
x1 <- x2; f1 <- ftn(x1)
iter <- iter + 1
if (trace) {
cat("At iteration", iter, "value of x is:", format(x1, digits = 12),
"| f(x) =", format(f1, digits = 4), "\n")
}
}
converged <- abs(f1) <= tol
if (trace) {
if (!converged) cat("Algorithm failed to converge\n")
else cat("Algorithm converged\n")
}
list(root = x1, iter = iter, converged = converged)
}
bisection_root <- function(f, a, b, tol = 1e-10, max.iter = 1000) {
fa <- f(a); fb <- f(b)
if (fa * fb > 0) stop("Tanda fungsi pada ujung selang tidak berlawanan.")
iter <- 0
while (iter < max.iter) {
iter <- iter + 1
mid <- (a + b) / 2
fmid <- f(mid)
if (abs(fmid) <= tol || (b - a) / 2 < tol) {
return(list(root = mid, iter = iter))
}
if (fa * fmid < 0) {
b <- mid; fb <- fmid
} else {
a <- mid; fa <- fmid
}
}
list(root = (a + b) / 2, iter = iter)
}
f1_val <- function(x) x^3 - x - 2
f1_der <- function(x) 3*x^2 - 1
f1_nr <- function(x) c(f1_val(x), f1_der(x))
res_nr1 <- newtonraphson(f1_nr, x0 = 1.5, tol = 1e-10)
## At iteration 1 value of x is: 1.52173913043 | f(x) = 0.002137
## At iteration 2 value of x is: 1.52137980596 | f(x) = 5.894e-07
## At iteration 3 value of x is: 1.5213797068 | f(x) = 4.53e-14
## Algorithm converged
res_bis1 <- bisection_root(f1_val, a = 1, b = 2, tol = 1e-10)
res_uni1 <- uniroot(f1_val, interval = c(1, 2), tol = 1e-10)
tabel_s1 <- data.frame(
Metode = c("Newton-Raphson (x0 = 1.5)", "Bisection ([1, 2])", "uniroot ([1, 2])"),
Akar = c(res_nr1$root, res_bis1$root, res_uni1$root),
Jumlah_Iterasi = c(res_nr1$iter, res_bis1$iter, res_uni1$iter)
)
print(tabel_s1, digits = 11)
## Metode Akar Jumlah_Iterasi
## 1 Newton-Raphson (x0 = 1.5) 1.5213797068 3
## 2 Bisection ([1, 2]) 1.5213797068 33
## 3 uniroot ([1, 2]) 1.5213797068 8
Interpretasi Soal 1: Ketiga metode sama-sama
menemukan akar pada \([1, 2]\) sekitar
\(1,5213797068\) dengan toleransi
1e-10, namun Newton-Raphson mencapai konvergensi dalam
jumlah iterasi yang jauh lebih sedikit dibandingkan Bisection
karena memanfaatkan informasi turunan \(f'(x)\) dan memiliki konvergensi
kuadratik di dekat akar, sedangkan Bisection membelah selang
secara linear. Sementara itu, uniroot() (metode Brent)
menggabungkan pengurungan selang dengan interpolasi sehingga jumlah
iterasinya jauh lebih hemat daripada Bisection.
ft_nr <- function(x) c(pt(x, df = 9) - 0.975, dt(x, df = 9))
res_t_bagus <- newtonraphson(ft_nr, x0 = 1, tol = 1e-10)
## At iteration 1 value of x is: 1.64032526729 | f(x) = -0.04268
## At iteration 2 value of x is: 2.04707276854 | f(x) = -0.01047
## At iteration 3 value of x is: 2.22961190638 | f(x) = -0.001365
## At iteration 4 value of x is: 2.26132531652 | f(x) = -3.401e-05
## At iteration 5 value of x is: 2.26215660868 | f(x) = -2.264e-08
## At iteration 6 value of x is: 2.2621571628 | f(x) = -1.01e-14
## Algorithm converged
nilai_qt <- qt(0.975, df = 9)
cat("Akar Newton (x0 = 1):", format(res_t_bagus$root, digits = 12),
"dalam", res_t_bagus$iter, "iterasi\n")
## Akar Newton (x0 = 1): 2.2621571628 dalam 6 iterasi
cat("Nilai qt(0.975, 9) :", format(nilai_qt, digits = 12), "\n")
## Nilai qt(0.975, 9) : 2.2621571628
cat("Selisih absolut :", format(abs(res_t_bagus$root - nilai_qt), digits = 4), "\n\n")
## Selisih absolut : 2.469e-13
res_t_jauh <- newtonraphson(ft_nr, x0 = 10, tol = 1e-10, max.iter = 50)
## At iteration 1 value of x is: -16776.4194955 | f(x) = -0.975
## At iteration 2 value of x is: 7.51474263792e+37 | f(x) = 0.025
## Iterasi 2 : f'(x) = 0 atau NA, berhenti.
Interpretasi Soal 2: Dengan tebakan awal \(x_0 = 1\), metode Newton-Raphson konvergen
cepat ke nilai kuantil \(t_{0,975}\)
untuk \(df = 9\) (sekitar \(2,2621571628\)) yang sesuai dengan
qt(0.975, 9). Ketika dimulai dari \(x_0 = 10\) yang berada jauh di ekor kanan
distribusi \(t\), kepadatan peluang
dt(10, 9) sangat kecil sehingga garis singgung hampir
mendatar dan rasio \(-f(x)/f'(x)\)
menghasilkan lompatan besar yang rawan gagal konvergen dalam batas
iterasi, menunjukkan bahwa Newton-Raphson sensitif terhadap tebakan
awal.
eval_secant <- 0
f3_sec <- function(x) {
eval_secant <<- eval_secant + 1
cos(x) - x
}
eval_nr_f <- 0
eval_nr_df <- 0
f3_nr <- function(x) {
eval_nr_f <<- eval_nr_f + 1
eval_nr_df <<- eval_nr_df + 1
c(cos(x) - x, -sin(x) - 1)
}
tol_s3 <- 1e-10
res_sec3 <- secant(f3_sec, x0 = 0, x1 = 1, tol = tol_s3)
## At iteration 1 value of x is: 0.685073357326 | f(x) = 0.0893
## At iteration 2 value of x is: 0.736298997614 | f(x) = 0.00466
## At iteration 3 value of x is: 0.739119361912 | f(x) = -5.729e-05
## At iteration 4 value of x is: 0.739085112127 | f(x) = 3.529e-08
## At iteration 5 value of x is: 0.739085133215 | f(x) = 2.668e-13
## Algorithm converged
total_eval_sec3 <- eval_secant
res_nr3 <- newtonraphson(f3_nr, x0 = 1, tol = tol_s3)
## At iteration 1 value of x is: 0.75036386784 | f(x) = -0.01892
## At iteration 2 value of x is: 0.739112890911 | f(x) = -4.646e-05
## At iteration 3 value of x is: 0.739085133385 | f(x) = -2.847e-10
## At iteration 4 value of x is: 0.739085133215 | f(x) = 0
## Algorithm converged
total_eval_nr3 <- eval_nr_f + eval_nr_df
tabel_s3 <- data.frame(
Metode = c("Secant (x0 = 0, x1 = 1)", "Newton-Raphson (x0 = 1)"),
Akar = c(res_sec3$root, res_nr3$root),
Iterasi = c(res_sec3$iter, res_nr3$iter),
Evaluasi_f = c(total_eval_sec3, eval_nr_f),
Evaluasi_df = c(0L, eval_nr_df),
Total_Evaluasi_Fungsi = c(total_eval_sec3, total_eval_nr3)
)
print(tabel_s3, digits = 11)
## Metode Akar Iterasi Evaluasi_f Evaluasi_df
## 1 Secant (x0 = 0, x1 = 1) 0.73908513322 5 7 0
## 2 Newton-Raphson (x0 = 1) 0.73908513322 4 5 5
## Total_Evaluasi_Fungsi
## 1 7
## 2 10
Interpretasi Soal 3: Kedua metode konvergen ke akar \(\cos x - x = 0\) sekitar \(0,7390851332\). Newton-Raphson memiliki orde konvergensi kuadratik (\(p \approx 2\)) sedangkan Secant memiliki orde superlinear (\(p \approx 1,618\)), namun Secant tidak membutuhkan evaluasi turunan analitik \(f'(x)\) sehingga cukup mengevaluasi \(f(x)\) saja pada tiap langkah.
set.seed(112026)
data_gamma <- rgamma(n = 50, shape = 2.5, rate = 0.5)
x_bar <- mean(data_gamma)
mean_log <- mean(log(data_gamma))
s_sd <- sd(data_gamma)
alpha0 <- 1 / (2 * s_sd)
f_gamma <- function(a) {
if (a <= 0) return(c(NA, NA))
val <- log(a) - digamma(a) - (log(x_bar) - mean_log)
dval <- (1 / a) - trigamma(a)
c(val, dval)
}
res_gamma <- newtonraphson(f_gamma, x0 = alpha0, tol = 1e-10)
## At iteration 1 value of x is: 0.205950032142 | f(x) = 3.318
## At iteration 2 value of x is: 0.372010122526 | f(x) = 1.551
## At iteration 3 value of x is: 0.649317574063 | f(x) = 0.7015
## At iteration 4 value of x is: 1.07274351308 | f(x) = 0.2946
## At iteration 5 value of x is: 1.60488874677 | f(x) = 0.1037
## At iteration 6 value of x is: 2.05184849612 | f(x) = 0.02389
## At iteration 7 value of x is: 2.22586369573 | f(x) = 0.001975
## At iteration 8 value of x is: 2.24296529822 | f(x) = 1.594e-05
## At iteration 9 value of x is: 2.24310557225 | f(x) = 1.055e-09
## At iteration 10 value of x is: 2.24310558153 | f(x) = 1.11e-16
## Algorithm converged
fit_mass <- MASS::fitdistr(data_gamma, densfun = "gamma")
alpha_mass <- unname(fit_mass$estimate["shape"])
rate_mass <- unname(fit_mass$estimate["rate"])
tabel_s4 <- data.frame(
Pendekatan = c("Newton-Raphson (Persamaan Skor)", "MASS::fitdistr (MLE Gamma)"),
Estimasi_Shape_alpha = c(res_gamma$root, alpha_mass),
Selisih_Absolut = c(abs(res_gamma$root - alpha_mass), 0)
)
print(tabel_s4, digits = 10)
## Pendekatan Estimasi_Shape_alpha Selisih_Absolut
## 1 Newton-Raphson (Persamaan Skor) 2.243105582 1.626285739e-06
## 2 MASS::fitdistr (MLE Gamma) 2.243107208 0.000000000e+00
cat("Estimasi rate (beta) dari MASS::fitdistr:", format(rate_mass, digits = 10), "\n")
## Estimasi rate (beta) dari MASS::fitdistr: 0.4177691471
Interpretasi Soal 4: Estimasi parameter bentuk \(\alpha\) melalui metode Newton-Raphson pada
persamaan skor Gamma menghasilkan nilai yang sangat dekat (identik
hingga ketelitian numerik) dengan estimasi shape dari
MASS::fitdistr(..., densfun = "gamma"), dan menjaga nilai
iterasi tetap positif (\(\alpha >
0\)) penting agar log(), digamma(), dan
trigamma() terdefinisi dengan baik.