library(Deriv)
library(ggplot2)
Lakukan Modifikasi pada fungsi newtonraphson sehingga
output dari fungsi tersebut berupa list dengan tiga object:
root berisi akar persamaanx_values berisi semua nilai x yang
dicobakanerror berisi nilai dari hasil
operasi abs(fx[1]) Gunakan persamaan pada contoh soal 1
untuk menujukkan hasil outputnya!newtonraphson <- function(ftn, x0, tol = 1e-11, iter_max = 100) {
x <- x0
fx <- ftn(x)
iter <- 0
x_value <- NULL
error <- NULL
while ((abs(fx[1]) > tol) && (iter < iter_max)) {
# objek x_values
x_value[iter+1] <- x
error[iter+1] <- abs(fx[1])
# objek root
x <- x - fx[1]/fx[2]
fx <- ftn(x)
iter <- iter + 1
cat("At", iter, "-th iteration, the value of x is", x, "\n")
}
# membuat objek error
if (abs(fx[1]) > tol) {
cat("Algorithm failed to converge \n")
return(NULL)
} else {
cat("\n Algorithm converged at", iter, "-th iteration. \n")
output <- list("root"=x,"x_values"=x_value,"error"=error)
return(output)
}
}
fx <- function(x) {
formula <- function(x) 2*x^3-8*x^2+3*x-7
func <- formula(x)
deriv_func <- Deriv(formula)
derivation <- deriv_func(x)
return(c("fungsi"=func,"turunan"=derivation))
}
newtonraphson(ftn=fx,x0=0)
## At 1 -th iteration, the value of x is 2.333333
## At 2 -th iteration, the value of x is -8.555556
## At 3 -th iteration, the value of x is -5.324988
## At 4 -th iteration, the value of x is -3.188967
## At 5 -th iteration, the value of x is -1.773957
## At 6 -th iteration, the value of x is -0.80584
## At 7 -th iteration, the value of x is -0.01456329
## At 8 -th iteration, the value of x is 2.163783
## At 9 -th iteration, the value of x is -2.852905
## At 10 -th iteration, the value of x is -1.548945
## At 11 -th iteration, the value of x is -0.6415338
## At 12 -th iteration, the value of x is 0.1685111
## At 13 -th iteration, the value of x is 14.32306
## At 14 -th iteration, the value of x is 10.07165
## At 15 -th iteration, the value of x is 7.285731
## At 16 -th iteration, the value of x is 5.510972
## At 17 -th iteration, the value of x is 4.467051
## At 18 -th iteration, the value of x is 3.978494
## At 19 -th iteration, the value of x is 3.854504
## At 20 -th iteration, the value of x is 3.846622
## At 21 -th iteration, the value of x is 3.846591
## At 22 -th iteration, the value of x is 3.846591
##
## Algorithm converged at 22 -th iteration.
## $root
## fungsi
## 3.846591
##
## $x_values
## [1] 0.00000000 2.33333333 -8.55555556 -5.32498786 -3.18896676 -1.77395732
## [7] -0.80583995 -0.01456329 2.16378288 -2.85290503 -1.54894451 -0.64153377
## [13] 0.16851110 14.32306020 10.07165324 7.28573054 5.51097225 4.46705129
## [19] 3.97849387 3.85450384 3.84662161 3.84659056
##
## $error
## [1] 7.000000e+00 1.814815e+01 1.870738e+03 5.518043e+02 1.627834e+02
## [6] 4.866229e+01 1.565913e+01 7.045393e+00 1.770283e+01 1.271112e+02
## [11] 3.827321e+01 1.274519e+01 6.712065e+00 4.271522e+03 1.255010e+03
## [16] 3.636826e+02 1.013118e+02 2.504075e+01 4.254666e+00 2.401804e-01
## [21] 9.388547e-04 1.454203e-08
Selidiki penggunaan fungsi browser() di R. Gunakan
fungsi newtonraphson untuk ilustrasi penggunaan. Laporkan
hasil penyelidikanmu beserta ilustrasi koding!
newtonraphson_browse <- function(ftn, x0, tol = 1e-11, iter_max = 100) {
x <- x0
fx <- ftn(x)
iter <- 0
x_value <- NULL
error <- NULL
browser()
while ((abs(fx[1]) > tol) && (iter < iter_max)) {
# objek x_values
x_value[iter+1] <- x
error[iter+1] <- abs(fx[1])
# objek root
x <- x - fx[1]/fx[2]
fx <- ftn(x)
iter <- iter + 1
cat("At", iter, "-th iteration, the value of x is", x, "\n")
}
# membuat objek error
if (abs(fx[1]) > tol) {
cat("Algorithm failed to converge \n")
return(NULL)
} else {
cat("\n Algorithm converged at", iter, "-th iteration. \n")
output <- list("root"=x,"x_values"=x_value,"error"=error)
return(output)
}
}
fx_browser <- function(x) {
formula <- function(x) log10(x)-21*x^3
func <- formula(x)
browser()
deriv_func <- Deriv(formula)
derivation <- deriv_func(x)
return(c("fungsi"=func,"turunan"=derivation))
}
newtonraphson_browse(ftn=fx_browser,x0=0)
## Called from: ftn(x)
## debug at <text>#5: deriv_func <- Deriv(formula)
## debug at <text>#6: derivation <- deriv_func(x)
## debug at <text>#7: return(c(fungsi = func, turunan = derivation))
## Called from: newtonraphson_browse(ftn = fx_browser, x0 = 0)
## debug at <text>#8: while ((abs(fx[1]) > tol) && (iter < iter_max)) {
## x_value[iter + 1] <- x
## error[iter + 1] <- abs(fx[1])
## x <- x - fx[1]/fx[2]
## fx <- ftn(x)
## iter <- iter + 1
## cat("At", iter, "-th iteration, the value of x is", x, "\n")
## }
## debug at <text>#10: x_value[iter + 1] <- x
## debug at <text>#11: error[iter + 1] <- abs(fx[1])
## debug at <text>#13: x <- x - fx[1]/fx[2]
## debug at <text>#14: fx <- ftn(x)
## Called from: ftn(x)
## debug at <text>#5: deriv_func <- Deriv(formula)
## debug at <text>#6: derivation <- deriv_func(x)
## debug at <text>#7: return(c(fungsi = func, turunan = derivation))
## debug at <text>#15: iter <- iter + 1
## debug at <text>#16: cat("At", iter, "-th iteration, the value of x is", x, "\n")
## At 1 -th iteration, the value of x is NaN
## debug at <text>#8: (while) (abs(fx[1]) > tol) && (iter < iter_max)
## Error in while ((abs(fx[1]) > tol) && (iter < iter_max)) {: missing value where TRUE/FALSE needed
browser()Fungsi browser() digunakan ketika ingin dilakukan proses
debugging pada R. Debugging adalah proses pengecekan
error pada sebuah fungsi yang dijalankan dengan melakukan
tracking satu per satu langkah yang dieksekusikan dalam sebuah
fungsi. Penerapan debugging ini akan semakin terlihat gunanya
ketika diterapkan pada fungsi yang melibatkan proses iterasi/perulangan
sebagaimana fungsi newtonraphson() di atas.
Cara penggunaan fungsi browser() adalah dengan
meletakkan pemanggilan fungsi browser() di dalam baris
kode/instruksi yang diindikasikan terdapat kesalahan di sana. Kemudian,
fungsi tersebut akan secara otomatis akan menampilkan hasil dari
pengaplikasian program untuk melihat sumber atau tahapan dimana error
mulai muncul. Untuk melanjutkan proses tracking dapat dengan
mengetik tombol continue pada console atau dengan
perintah lain seperti F10 (untuk melanjutkan ke perintah
berikutnya), Shift+F6 (untuk melanjutkan sampai proses
looping selesai), dan Shift+F8 (untuk menghentikan
proses). Dari proses debugging di atas, bisa diindikasikan bahwa error
terjadi pada pembentukan fungsi turunan yang akan digunakan dalam proses
iterasi untuk menemukan akar fungsi dengan metode Newton Raphson.
Buatlah gambar fungsi pada contoh soal 2 dengan menggunakan package
ggplot dengan menggunakan fungsi
geom_curve!
fx <- function(x) {
2*x^3-8*x^2+3*x-7 #fungsi yang sama pada soal 1 (krn soal 2 error)
}
x0 <- c(-100:150)*0.1 # batas-batas, dari -10 hingga 15
x <- x0[1:250] # nilai sepanjang sumbu x
xend <- x0[2:251]
y0 <- fx(x0)
y <- y0[1:250]
yend <- y0[2:251]
data <- data.frame(x,y)
ggplot(data, aes(x,y)) +
geom_curve(x=x,y=y,xend=xend,yend=yend, ncp=100, curvature=0, color="blue") +
geom_line(aes(y = 0), lty=5)+
geom_line(aes(x = 0), lty=5)+
theme_bw()