Metode Pencarian Akar Persamaan Non-Linear

Dicky Lihardo Girsang

2023-02-10

Akar persamaan non-linear

Secara garis besar, terdapat 2 metode dalam menentukan akar persamaan dari suatu persamaan non-linear, yakni dengan metode terbuka dan metode tertutup

1. Metode tertutup

1.1 Metode tabel

Adapun langkah2 dalam membuat fungsi dengan metode tabel sebagai berikut:

a. Buat fungsi

root_table <- function(f, a, b, N=20){
  h <- abs((a+b)/N)
  x <- seq(from=a, to=b, by=h)
  fx <- rep(0, N+1)
  for(i in 1:(N+1)){
    fx[i] <- f(x[i])
  }
  data <- data.frame(x=x, fx=fx)
  return(data)
}

b. Mencari akar persamaan

#Carilah akar persamaan f(x) = x + e^x pada rentang x = [-1,0]
tabel <- root_table(f=function(x){x+exp(x)},
                    a=-1, b=0, N=10)
tabel
##       x          fx
## 1  -1.0 -0.63212056
## 2  -0.9 -0.49343034
## 3  -0.8 -0.35067104
## 4  -0.7 -0.20341470
## 5  -0.6 -0.05118836
## 6  -0.5  0.10653066
## 7  -0.4  0.27032005
## 8  -0.3  0.44081822
## 9  -0.2  0.61873075
## 10 -0.1  0.80483742
## 11  0.0  1.00000000

c. Buat plot persamaan

f=function(x){x+exp(x)}
curve(f, from = -1, to = 0)
abline(h = 0, col="red", lty=2)

1.2 Metode Biseksi

a. Buat fungsi persamaan

root_bisection <- function(f, a, b, tol=1e-7, N=100){
  iter <- 0
  fa <- f(a)
  fb <- f(b)
  
  while(abs(b-a)>tol){
    iter <- iter+1
    if(iter>N){
      warning("iterations maximum exceeded")
      break
    }
    x <- (a+b)/2
    fx <- f(x)
    if(fa*fx>0){
      a <- x
      fa <- fx
    } else{
      b <- x
      fb <- fx
    }
  }
  
  # iterasi nilai x sebagai return value
  root <- (a+b)/2
  return(list(`function`=f, root=root, iter=iter))
}

b. Mencari akar persamaan. Contoh soal:

Carilah akar persamaan f(x) = x*e^(-x) + 1 pada rentang x = [-1,0] dengan nilai toleransi sebesar 10^(-7)

root_bisection(function(x){x*exp(-x)+1},
               a=-1, b=0)
## $`function`
## function(x){x*exp(-x)+1}
## <bytecode: 0x00000181edee1930>
## 
## $root
## [1] -0.5671433
## 
## $iter
## [1] 24

c. Buat plot

f=function(x){x*exp(-x)+1}
curve(f, from = -1, to = 0)
abline(h = 0, col="red", lty=2)

root_bisection(function(x){x^4 - x^3 + 2*x^2 -2*x -12},
               a=-2, b=0)
## $`function`
## function(x){x^4 - x^3 + 2*x^2 -2*x -12}
## <bytecode: 0x00000181ee50fae8>
## 
## $root
## [1] -1.344389
## 
## $iter
## [1] 25

2. Metode Terbuka

2.1 Metode Newton-raphson

Metode penyelesaian persamaan non-linier dengan menggunakan pendekatan satu titik awal dan mendekatinya dengan memperhatikan slope atau gradien

a. Buat fungsi persamaan

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

b. Mencari akar persamaan. Contoh soal:

Selesaikan persamaan non-linier x- e^(-x) = 0 dengan metode Newton Raphson

result_newraph <- root_newton(function(x){x-exp(-x)},
            function(x){1+exp(-x)},
            x0=6)
result_newraph
## $`function`
## function(x){x-exp(-x)}
## <bytecode: 0x00000181eed7e508>
## 
## $root
## [1] 0.5671433
## 
## $iter
## [1] 5

c. Buat plot fungsi

f=function(x){x - exp(-x)}
curve(f, from = 0, to = 1)
abline(h = 0, col="red", lty=2)
abline(v = 0.567, col="blue", lty=2)

d. Additional: contoh soal lainnya

result_newraph <- root_newton(function(x){x^3 +x^2 -3*x - 3},
                              function(x){3*x^2 + 2*x - 3},
                              x0=1)
result_newraph
## $`function`
## function(x){x^3 +x^2 -3*x - 3}
## <bytecode: 0x00000181ef60dd98>
## 
## $root
## [1] 1.732051
## 
## $iter
## [1] 7

2.2 Metode Secant

Yakni sebagai perbaikan dari metode regula-falsi dan Newton Raphson, dimana kemiringan dua titik dinyatakan secara diskrit dengan mengambil bentuk garis lurus yang melalui satu titik

a. Buat fungsi persamaan

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))
}

b. Mencari akar persamaan. Contoh Soal:

result_secant <- root_secant(function(x){x-exp(-x)}, x=0)
result_secant #1
## $`function`
## function(x){x-exp(-x)}
## <bytecode: 0x00000181e9f6e4f8>
## 
## $root
## [1] 0.5671433
## 
## $iter
## [1] 6
result_secant <- root_secant(function(x){x^3 +x^2 -3*x - 3}, x=1)
result_secant #2
## $`function`
## function(x){x^3 +x^2 -3*x - 3}
## <bytecode: 0x00000181e86b2f88>
## 
## $root
## [1] 1.732051
## 
## $iter
## [1] 9

c. Plot fungsi

#1
f=function(x){x - exp(-x)} 
curve(f, from = 0, to = 1)
abline(h = 0, col="red", lty=2)
abline(v = 0.567, col="blue", lty=2)

#2
f=function(x){x^3 +x^2 -3*x - 3}
curve(f, from = 0, to = 3)
abline(h = 0, col="red", lty=2)
abline(v = 1.73205, col="blue", lty=2)