UAS Metode Numerik

~ Ujian Akhir Semester ~


Kontak : \(\downarrow\)
Email
Instagram https://www.instagram.com/dsciencelabs/
RPubs https://rpubs.com/dsciencelabs/

Soal 1

Kaidah Simpson 1/3
Penyelesaian:

f <- function(x) {
  return(exp(-x^2))}
simpson <- function(f, a, b, m){
  h <- (b-a)/m # jarak selang
  x <- a # awal selang
  I <- f(a)+f(b)
  sigma <- 0
  
  if(m%%2 != 0){
    stop("Jumlah panel harus genap")
  }else{
    for(i in 1:(m-1)){
    x <- x+h
    if(i%%2==0){
      sigma <- sigma + 2*f(x)
    }else{
      sigma <- sigma + 4*f(x)
      }
    }
  }
  
  return((h/3)*(I+sigma))
}
simpson(f, a=0, b=1, m=10)
## [1] 0.7468249

Soal 2

Misalkan D(2h) dan D(4h) adalah hampiran dengan lebar selang 2h dan 4h menggunakan rumus hampiran selisih-pusat orde O(h4). Tentukan hampiran f`(1,2) jika diketahui fungsi f(x)=exp(x) dalam selang interval dengan batas bawah 0,8 dan batas atas 1,6 serta h = 0,1 mengunakan Ektrapolasi Richardson.
penyelesaian:

f <- function(x) {
  return(exp(x))}
x = c(0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6)
f1 = f(x)
f1
## [1] 2.225541 2.459603 2.718282 3.004166 3.320117 3.669297 4.055200 4.481689
## [9] 4.953032
# D(2h)

h = 0.1
f_1  <- f(1.3)
f_2 <- f(1.4)
fmin1 <- f(1.1)
fmin2 <- f(1.0)
D_2h <- (((-f_2) +(8*f_1)-(8*fmin1)+(fmin2))/(12*2*h))
D_2h
## [1] 1.660053
# D(4h)

D_4h = (((-f_2) +(8*f_1)-(8*fmin1)+(fmin2))/(12*4*h))
D_4h
## [1] 0.8300265

Selanjutnya dengan menggunakan rumus Richardson Ekstrapolasi sebagai berikut :

\(f'(1.2)= D(2h)+\frac{D(2h)-D(4h)}{15}\)

# Richardson Ekstrapolasi

f1.2 = D_2h +(((D_2h)-(D_4h))/15)
f1.2
## [1] 1.715388

Soal 3

Hampiran selisih pusat
Penyelesaian:
Diberikan tabel yang berisi titik-titik sebuah fungsi f(x) sebagai berikut :

x <- c(1.000 , 1.100, 1.198, 1.1999, 1.200, 1.201, 1.202, 1.300, 1.400)
y <- c(1.54030, 0.45360, 0.36422, 0.36329, 0.36236, 0.36143, 0.36049, 0.26750, 0.16997)

Menentukan nilai f’(1,2) dan f``(1,2) untuk h = 0,1 dan h = 0,001 dengan rumus hampiran selisih-pusat orde O(h2).

library(DT)
df<-data.frame(x,y)
datatable(df)
#turunan pertama
# f'(1.2)

x0 = 1.2
x1 = 1.201
x_1 = 1.199

fx0 = 0.36236
fx1 = 0.36143
fx_1 = 0.36329

h1 = 0.1
h2 = 0.001
f0_1 = (fx1 - fx_1)/(2*h1)
print(f0_1, digits = 5)
## [1] -0.0093
# turunan kedua
# f''(1.2)

x0 = 1.2
x1 = 1.201
x_1 = 1.199

fx0 = 0.36236
fx1 = 0.36143
fx_1 = 0.36329

h1 = 0.1
h2 = 0.001
f0_1 = (fx1 - (2*fx0) + fx_1)/0.01
print(f0_1,digits = 5)
## [1] -5.5511e-15

Soal 4

Aproksimasi dengan kaidah trapesium
Penyelesaian:

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

f <- function(x){
 (x^2)*cos(x^2)
}
trapezoid(f,1.5,2.5,n = 10)
## [1] -0.4400977

Soal 5

Metode euler
Penyelesaian:

f1 <- function(x,y){(y^2)*(1+2*x)
}
euler <- function(f, x0, y0, h, n){
  x <- x0
  y <- y0
  
  for(i in 1:n){
    y0 <- y0 + h*f(x0, y0)
    x0 <- x0 + h
    x <- c(x,x0)
    y <- c(y, y0)
  }
  
  return(data.frame(x=x, y=y))
}
eu = euler(f1, x0=0, y0=1, h=-0.5, n=2)
eu