Lembaga : UIN Maulana Malik Ibrahim Malang
Jurusan : Teknik Informatika
Fungsi root_secant() merupakan fungsi yang penulis buat untuk melakukan iterasi menggunakan metode Secant.
Temukan akar persamaan dari persamaan non-linier f(x)=x3−2x+2 menggunakan metode terbuka dengan x 0 =0 dan x 0 =1/2!
Temukan akar persamaan dari persamaan f(x)=sin(x)/x dengan rentang pencarian x=0,5 dan x=1!
Hitung integral fungsi f(x)=sin2(x) pada domain x∈[0,π] !
untuk penyelesaian soal nomer 1 dan nomer 2, saya menggunakan fungsi root_secant. Agar kode untuk menghitung persamaan dapat berjalan, maka diperlukan sebuah sintaks. Berikut ini adalah sintaks yang digunakan pada fungsi root_secant:
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))
}
f(x)=x3−2x+2
root_secant(function(x){x^3-2*x+2}, x=0)
## $`function`
## function(x){x^3-2*x+2}
## <bytecode: 0x0000000015cbf0d0>
##
## $root
## [1] -1.769292
##
## $iter
## [1] 26root_secant(function(x){x^3-2*x+2}, x=1/2)
## $`function`
## function(x){x^3-2*x+2}
## <bytecode: 0x0000000012dc82b0>
##
## $root
## [1] -1.769292
##
## $iter
## [1] 16
root_secant(function(x){sin(x)/x}, x=0.5)
## $`function`
## function(x){sin(x)/x}
## <bytecode: 0x000000001502b1f0>
##
## $root
## [1] 6.283185
##
## $iter
## [1] 7
root_secant(function(x){sin(x)/x}, x=1)
## $`function`
## function(x){sin(x)/x}
## <bytecode: 0x00000000153c6818>
##
## $root
## [1] 3.141593
##
## $iter
## [1] 8
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)
}
mendefinisikan fungsi f(x)
f <- function(x){
sin(x)^2
}
menghitung soal Integral menggunakan Trapezoid dengan permisalan n=6
trapezoid(f,0,pi,n = 6)
## [1] 1.570796
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
Sumber :
Rosidi, M. Metode Numerik Menggunakan R untuk Teknik Lingkungan.
https://bookdown.org/moh_rosidi2610/Metode_Numerik/rootfinding.html#latihan-1
https://bookdown.org/moh_rosidi2610/Metode_Numerik/diffinteg.html#latihan-3