Kelompok 5 - OS

Afris Setiya Intan Amanda

2/17/2023

Kelompok 5

Nama Anggota :

1. Afris Setiya Intan Amanda (G1401201018)

2. Reza Arya Sukma (G1401201025)

3. Irpando Sagala (G1401201038)

4. Ainaini Salsabila (G1401201055)

5. Naura Tirza Ardhani (G1401201073)

Latihan Soal 5.9

Soal 5.9

Latihan 5.9 (Manual)

library(numDeriv)
quadraticinterpolation <- function(phi.f, l = 0, u = 1, eps = 1e-6)
{
#polynomials evaluated at 3 points(l, m, u) for interpolation: phi_fl, phi_fm, phi_fu
  
#l = lower
#u = upper
  if(l == u)
  {
    l <- 0
  }
  m <- (l + u)/2
  phi.fl <- phi.f(l)
  phi.fm <- phi.f(m)
  phi.fu <- phi.f(u)
  
  
  #a, b and c are coefficients of the polynomial (hx)
  y <- ((l - m)*(m - u)*(u - l))
  a <- (phi.fl*m*u*(u - m) + phi.fm*u*l*(l - u) + phi.fu*l*m*(m - l))/y
  b <- (phi.fl*(m^2 - u^2) + phi.fm*(u^2 - l^2) + phi.fu*(l^2 - m^2))/y
  c <- -(phi.fl*(m - u) + phi.fm*(u - l) + phi.fu*(l - m))/y
  
  alpha <- -b/(2*c)
  falpha <- phi.f(alpha)
  
  ncf <- 4
  
  hx <- function(x)  c*x^2 + b*x + a #environment global
  
  while((abs((hx(alpha) - falpha)) > eps))
  {
    #choice of l, m and C new values
    if(alpha <= m)
    {
      if(phi.fm >= falpha){
        u <- m; phi.fu <- phi.fm
        m <- alpha; phi.fm <- falpha
      }else{
        l <- alpha; phi.fl <- falpha
      }
    }else{
      if(phi.fm >= falpha){
        l <- m; phi.fl <- phi.fm
        m <- alpha; phi.fm <- falpha
      }else{
        u <- alpha; phi.fu <- falpha
      }
      
    }
    
    
    #refitting the function
    y <- ((l - m)*(m - u)*(u - l))
    a <- (phi.fl*m*u*(u - m) + phi.fm*u*l*(l - u) + phi.fu*l*m*(m - l))/y
    b <- (phi.fl*(m^2 - u^2) + phi.fm*(u^2 - l^2) + phi.fu*(l^2 - m^2))/y
    c <- -(phi.fl*(m - u) + phi.fm*(u - l) + phi.fu*(l - m))/y
    
    #quadratic interpolation
    
    alpha <- -b/(2*c)
    falpha <- phi.f(alpha)
    ncf <- ncf + 1
    
  }
  message("Method: Quadratic Interpolation. Number of calls of the objective function: ", ncf)
  return(alpha)
}

f <- function(x){
  0.5/sqrt(1+x^2)-sqrt(1+x^2)*(1-0.5/(1+x^2))+x
  }
quadraticinterpolation(f)
## Method: Quadratic Interpolation. Number of calls of the objective function: 12
## [1] 0.6831249
#Penentuan lower, upper, dan eps subjektif
quadraticinterpolation(f, l = 0, u=1, eps=1e-6)
## Method: Quadratic Interpolation. Number of calls of the objective function: 12
## [1] 0.6831249
#Nilai y
0.5/sqrt(1+0.6831249^2)-sqrt(1+0.6831249^2)*(1-0.5/(1+0.6831249^2))+0.6831249
## [1] 0.2977925
curve(f,xlim=c(0,3), ylim = c(0,0.4), col='purple',lwd=2)
abline(h=0)
abline(v=0)
points(x = 0.6831249, y = 0.2977925, pch = 19)
text(0.7, 0.32, "(0.683, 0.297)")

Latihan 5.9 (Library “Remotes”)

f2 <- function(x)
  {
  0.5/sqrt(1+x^2)-sqrt(1+x^2)*(1-0.5/(1+x^2))+x
  }

quadraticinterpolation(f2, l = 0, u = 1)
## Method: Quadratic Interpolation. Number of calls of the objective function: 12
## [1] 0.6831249
curve(f2,xlim=c(0,3), ylim = c(0,0.4), col='purple',lwd=2)

0.5/sqrt(1+0.6831249^2)-sqrt(1+0.6831249^2)*(1-0.5/(1+0.6831249^2))+0.6831249
## [1] 0.2977925
points(x = 0.6831249, y = 0.2977925, pch = 19)
text(0.7, 0.32, "(0.683, 0.297)")

Kesimpulan

Berdasarkan fungsi manual dan fungsi dari library remotes menghasilkan nilai optimum yang sama yaitu (0.683, 0.297).