Metode Numerik

~ Ujian Akhir Semester ~

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

soal 1

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

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
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 = (((-f_2) +(8*f_1)-(8*fmin1)+(fmin2))/(12*4*h))
D_4h
## [1] 0.8300265

Berikutnya, dengan menggunakan rumus Richardson Ekstrapolasi, maka :

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

soal 3

x <- c(1.000,1.100,1.198,1.199,1.200,1.201,1.202,1.300,1.400)
fx<-c(0.54030,0.45360,0.6422,0.36329,0.36236,0.36143,0.36049,0.26750,0.16997)
library(DT)
df<-data.frame(x,fx)
datatable(df)
#turunan pertama
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
f0_2 = (fx1 - fx_1)/(2*h2)
print(f0_2,digits = 5)
## [1] -0.93
# Turunan Kedua
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
f0_2 = (fx1 - (2*fx0) + fx_1)/h2**2
print(f0_2,digits = 5)
## [1] -5.5511e-11

soal 4

trapezoid <- function(ftn, a, b, n = 10) {
     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){
 (x^2)*cos(x^2)
}
trapezoid(f,1.5,2.5,n = 10)
## [1] -0.4400977

soal 5

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))
}
Jawaabeuler = euler(f1, x0=0, y0=1, h=-0.5, n=2)
Jawaabeuler