Dosen Pengampu : "Prof. Dr. Suhartono, M.Kom"

Mata Kuliah : "Kalkulus"

Prodi : "Teknik Informatika"

Lembaga : "Universitas Islam Negeri Maulana Malik Ibrahim Malang"

root_secant <- function(f, x, tol=1e-7, N=100){
  iter <- 0
  
  xold <- x
  fxold <- f(x)
  x <- xold+10*tol
  
  while(abs(x-xold)>tol){
    iter <- iter+1
    if(iter>N)
      stop("No solutions found")
    
    fx <- f(x)
    xnew <- x - fx*((x-xold)/(fx-fxold))
    xold <- x
    fxold <- fx
    x <- xnew
  }
  
  root<-xnew
  return(list(`function`=f, root=root, iter=iter))
}

Soal

1.Temukan akar persamaan dari persamaan non-linier f(x)=x^3−2x+2 menggunakan metode terbuka dengan x0=0 dan x0=1/2 !

root_secant(function(x){(x^3)-(2*x)+2}, x=0)
## $`function`
## function(x){(x^3)-(2*x)+2}
## <bytecode: 0x0000000007ecbec8>
## 
## $root
## [1] -1.769292
## 
## $iter
## [1] 26
## $`function`
## function(x){(x^3)-(2*x)+
## <bytecode: 0x0839f5a8>
## 
## $root
## [1] -1.769292
## 
## $iter
## [1] 26

2.Temukan akar persamaan dari persamaan f(x)=sin(x)/x dengan rentang pencarian x=0,8 dan x=1,5 !

root_secant(function(x)
  {(sin(x)/x)},
  x=0.8)
## $`function`
## function(x)
##   {(sin(x)/x)}
## <bytecode: 0x0000000007495b28>
## 
## $root
## [1] 3.141593
## 
## $iter
## [1] 9
## $`function`
## function(x)
##   {(sin(x)/x)}
## <bytecode: 0x051c3b38>
## 
## $root
## [1] 6.283185
## 
## $iter
## [1] 7
## `function`
root_secant(function(x)
  {(sin(x)/x)},
  x=1.5)
## $`function`
## function(x)
##   {(sin(x)/x)}
## <bytecode: 0x0000000008ace640>
## 
## $root
## [1] 3.141593
## 
## $iter
## [1] 6
## $`function`
## function(x)
##   {(sin(x)/x)}
## <bytecode: 0x07ec1450>
## 
## $root
## [1] 3.141593
## 
## $iter
## [1] 8

3.Hitung integral fungsi f(x)=sin2(x) pada domain x∈[0,π]

trapezoid <- function(ftn, a, b, n = 100) {
         h <- (b-a)/n
         x.vec <- seq(a, b, by = h)
         f.vec <- sapply(x.vec, ftn)     # ftn(x.vec)
         Trap <- h*(f.vec[1]/2 + sum(f.vec[2:n]) + f.vec[n+1]/2)
         return(Trap)
}
f <- function(x){
      sin(x)^2
    }

Menghitung integral menggunakan trapezoid dengan permisalan n=6

trapezoid(f,0,pi,n = 6)
## [1] 1.570796

Referensi

https://bookdown.org/moh_rosidi2610/Metode_Numerik/rootfinding.html#latihan-1

https://bookdown.org/moh_rosidi2610/Metode_Numerik/diffinteg.html#latihan-3