Root Finding

Muhammad Hasan Itqoni

G1501221029

No 1

Lakukan modifikasi pada fungsi newtonraphson sehingga output dari fungsi tersebut berupa list dengan tiga object: a. Object pertama bernama root berisi akar persamaan. b. Object kedua bernama x_values berisi semua nilai x yang dicobakan c. Object ketiga bernama error berisi nilai dari hasil operasi abs(fx[1])

Gunakan persamaan pada contoh soal 1 untuk menunjukkan hasil outputnya!

Import Library

library(Deriv)
## Warning: package 'Deriv' was built under R version 4.2.3
library(ggplot2)

fungsi matematika \[ f(x) = x^2 - 2x - 5 = 0\]

fx <- function(x) {
  x^3 - 2 * x - 5
}

Mendefinisikan persamaan dalam bentuk fungsi untuk newton-raphson

fx <- function(x) {
rumus_fungsi <- function(x) x^3-2*x-5
fungsi <- rumus_fungsi(x)
# mencari turunan dengan menggunakan fungsi Deriv
# dari package Deriv
rumus_turunan <- Deriv(rumus_fungsi)
turunan <- rumus_turunan(x)
return(c("fungsi"=fungsi,"turunan"=turunan))
}

Fungsi newtonraphson

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)) {
    # object x_values
    x_value[iter+1] <- x
    error[iter+1] <- abs(fx[1])
    
    # object 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")
  }
  # objek error
  if (abs(fx[1]) > tol) {
    cat("Algoritm failed to converge \n")
    retun(NULL)
  } 
  else {
    cat("\n Algortim converged at", iter, "-th iteration. \n")
    output <- list("root" = x, "x_values" = x_value, "error" = error)
    return(output)
  }
}

Mengaplikasikan fungsi yang sudah dibuat

newtonraphson(ftn = fx, x0=0)
## At 1 -th iteration, the value of x is -2.5 
## At 2 -th iteration, the value of x is -1.567164 
## At 3 -th iteration, the value of x is -0.5025924 
## At 4 -th iteration, the value of x is -3.820706 
## At 5 -th iteration, the value of x is -2.549393 
## At 6 -th iteration, the value of x is -1.608111 
## At 7 -th iteration, the value of x is -0.5761004 
## At 8 -th iteration, the value of x is -4.59771 
## At 9 -th iteration, the value of x is -3.083543 
## At 10 -th iteration, the value of x is -2.022194 
## At 11 -th iteration, the value of x is -1.123764 
## At 12 -th iteration, the value of x is 1.208652 
## At 13 -th iteration, the value of x is 3.58079 
## At 14 -th iteration, the value of x is 2.655233 
## At 15 -th iteration, the value of x is 2.216106 
## At 16 -th iteration, the value of x is 2.102125 
## At 17 -th iteration, the value of x is 2.094584 
## At 18 -th iteration, the value of x is 2.094551 
## At 19 -th iteration, the value of x is 2.094551 
## 
##  Algortim converged at 19 -th iteration.
## $root
##   fungsi 
## 2.094551 
## 
## $x_values
##  [1]  0.0000000 -2.5000000 -1.5671642 -0.5025924 -3.8207065 -2.5493934
##  [7] -1.6081115 -0.5761004 -4.5977096 -3.0835431 -2.0221943 -1.1237641
## [13]  1.2086516  3.5807900  2.6552332  2.2161063  2.1021250  2.0945836
## [19]  2.0945515
## 
## $error
##  [1] 5.000000e+00 1.562500e+01 5.714632e+00 4.121770e+00 5.313249e+01
##  [6] 1.647076e+01 5.942390e+00 4.039002e+00 9.299526e+01 2.815198e+01
## [11] 9.224909e+00 4.171613e+00 5.651658e+00 3.375152e+01 8.409627e+00
## [16] 1.451367e+00 8.489238e-02 3.582354e-04 6.472653e-09

Pada output di atas dapat dilihat bahwa, nilai akar yang didapat sebesar 2.094551 yang sudah konvergen pada iterasi ke 19 dengan nilai error yang sudah di dibawah nilai toleransi yaitu 1e-11

No 2

Selidiki penggunaan fungsi browser() di R. Gunakan fungsi newtonraphson untuk ilustrasi penggunaan. Laporkan hasil penyelidikanmu beserta ilustrasi koding!

fungsi yang akan diaplikasikan

fx_browser <- function(x) {
  rumus_fungsi <- function(x) 3*x^2 + 4*x - 2
  fungsi <- rumus_fungsi(x)
  browser()
  rumus_turunan <- Deriv(rumus_fungsi)
  turunan <- rumus_turunan(x)
  return(c("fungsi"=fungsi,"turunan"=turunan))
}

Fungsi newtonraphson dengan browser()

newtonraphson_browser <- 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")
  }
  # 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)
  }
}

Melakukan Pemanggilan fungsi yang sudah dibuat dan debugging

newtonraphson_browser(ftn=fx_browser,x0=0)
## Called from: ftn(x)
## debug at <text>#5: rumus_turunan <- Deriv(rumus_fungsi)
## debug at <text>#6: turunan <- rumus_turunan(x)
## debug at <text>#7: return(c(fungsi = fungsi, turunan = turunan))
## Called from: newtonraphson_browser(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>#14: x <- x - fx[1]/fx[2]
## debug at <text>#15: fx <- ftn(x)
## Called from: ftn(x)
## debug at <text>#5: rumus_turunan <- Deriv(rumus_fungsi)
## debug at <text>#6: turunan <- rumus_turunan(x)
## debug at <text>#7: return(c(fungsi = fungsi, turunan = turunan))
## debug at <text>#16: iter <- iter + 1
## debug at <text>#17: cat("At", iter, "-th iteration, the value of x is", x, "\n")
## At 1 -th iteration, the value of x is 0.5 
## debug at <text>#8: (while) (abs(fx[1]) > tol) && (iter < iter_max)
## debug at <text>#10: x_value[iter + 1] <- x
## debug at <text>#11: error[iter + 1] <- abs(fx[1])
## debug at <text>#14: x <- x - fx[1]/fx[2]
## debug at <text>#15: fx <- ftn(x)
## Called from: ftn(x)
## debug at <text>#5: rumus_turunan <- Deriv(rumus_fungsi)
## debug at <text>#6: turunan <- rumus_turunan(x)
## debug at <text>#7: return(c(fungsi = fungsi, turunan = turunan))
## debug at <text>#16: iter <- iter + 1
## debug at <text>#17: cat("At", iter, "-th iteration, the value of x is", x, "\n")
## At 2 -th iteration, the value of x is 0.3928571 
## debug at <text>#8: (while) (abs(fx[1]) > tol) && (iter < iter_max)
## debug at <text>#10: x_value[iter + 1] <- x
## debug at <text>#11: error[iter + 1] <- abs(fx[1])
## debug at <text>#14: x <- x - fx[1]/fx[2]
## debug at <text>#15: fx <- ftn(x)
## Called from: ftn(x)
## debug at <text>#5: rumus_turunan <- Deriv(rumus_fungsi)
## debug at <text>#6: turunan <- rumus_turunan(x)
## debug at <text>#7: return(c(fungsi = fungsi, turunan = turunan))
## debug at <text>#16: iter <- iter + 1
## debug at <text>#17: cat("At", iter, "-th iteration, the value of x is", x, "\n")
## At 3 -th iteration, the value of x is 0.3874398 
## debug at <text>#8: (while) (abs(fx[1]) > tol) && (iter < iter_max)
## debug at <text>#10: x_value[iter + 1] <- x
## debug at <text>#11: error[iter + 1] <- abs(fx[1])
## debug at <text>#14: x <- x - fx[1]/fx[2]
## debug at <text>#15: fx <- ftn(x)
## Called from: ftn(x)
## debug at <text>#5: rumus_turunan <- Deriv(rumus_fungsi)
## debug at <text>#6: turunan <- rumus_turunan(x)
## debug at <text>#7: return(c(fungsi = fungsi, turunan = turunan))
## debug at <text>#16: iter <- iter + 1
## debug at <text>#17: cat("At", iter, "-th iteration, the value of x is", x, "\n")
## At 4 -th iteration, the value of x is 0.3874259 
## debug at <text>#8: (while) (abs(fx[1]) > tol) && (iter < iter_max)
## debug at <text>#10: x_value[iter + 1] <- x
## debug at <text>#11: error[iter + 1] <- abs(fx[1])
## debug at <text>#14: x <- x - fx[1]/fx[2]
## debug at <text>#15: fx <- ftn(x)
## Called from: ftn(x)
## debug at <text>#5: rumus_turunan <- Deriv(rumus_fungsi)
## debug at <text>#6: turunan <- rumus_turunan(x)
## debug at <text>#7: return(c(fungsi = fungsi, turunan = turunan))
## debug at <text>#16: iter <- iter + 1
## debug at <text>#17: cat("At", iter, "-th iteration, the value of x is", x, "\n")
## At 5 -th iteration, the value of x is 0.3874259 
## debug at <text>#8: (while) (abs(fx[1]) > tol) && (iter < iter_max)
## debug at <text>#20: 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)
## }
## debug at <text>#24: cat("\n Algorithm converged at", iter, "-th iteration. \n")
## 
##  Algorithm converged at 5 -th iteration. 
## debug at <text>#25: output <- list(root = x, x_values = x_value, error = error)
## debug at <text>#26: return(output)
## $root
##    fungsi 
## 0.3874259 
## 
## $x_values
## [1] 0.0000000 0.5000000 0.3928571 0.3874398 0.3874259
## 
## $error
## [1] 2.000000e+00 7.500000e-01 3.443878e-02 8.804257e-05 5.813465e-10

No 3

Buatlah gambar pada contoh soal 2 dengan menggunakan package ggplot dengan menggunakan fungsi geom_curve!

fx <- function(x) {
   3*x^2 + 4*x - 2
}
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()