Metode Numerik
~ Ujian Akhir Semester ~
Kontak | : \(\downarrow\) |
naufal3433@gmail.com | |
https://www.instagram.com/m_naufalardiansyah/ | |
RPubs | https://www.rpubs.com/muhammad_naufal/ |
soal 1
<- function(x) {
f return(exp(-x^2))}
<- function(f, a, b, m){
simpson <- (b-a)/m # jarak selang
h <- a # awal selang
x <- f(a)+f(b)
I <- 0
sigma
if(m%%2 != 0){
stop("Jumlah panel harus genap")
else{
}for(i in 1:(m-1)){
<- x+h
x if(i%%2==0){
<- sigma + 2*f(x)
sigma else{
}<- sigma + 4*f(x)
sigma
}
}
}
return((h/3)*(I+sigma))
}simpson(f, a=0, b=1, m=10)
## [1] 0.7468249
soal 2
<- function(x) {
f return(exp(x))}
= c(0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6)
x = f(x)
f1 f1
## [1] 2.225541 2.459603 2.718282 3.004166 3.320117 3.669297 4.055200 4.481689
## [9] 4.953032
= 0.1
h = f(1.3)
f_1 = f(1.4)
f_2 = f(1.1)
fmin1 = f(1.0)
fmin2 = (((-f_2) +(8*f_1)-(8*fmin1)+(fmin2))/(12*2*h))
D_2h D_2h
## [1] 1.660053
= (((-f_2) +(8*f_1)-(8*fmin1)+(fmin2))/(12*4*h))
D_4h D_4h
## [1] 0.8300265
Berikutnya, dengan menggunakan rumus Richardson Ekstrapolasi, maka :
.2 = D_2h +(((D_2h)-(D_4h))/15)
f1.2 f1
## [1] 1.715388
soal 3
<- c(1.000,1.100,1.198,1.199,1.200,1.201,1.202,1.300,1.400)
x <-c(0.54030,0.45360,0.6422,0.36329,0.36236,0.36143,0.36049,0.26750,0.16997) fx
library(DT)
<-data.frame(x,fx)
dfdatatable(df)
#turunan pertama
= 1.2
x0 = 1.201
x1 = 1.199
x_1
= 0.36236
fx0 = 0.36143
fx1 = 0.36329
fx_1
= 0.1
h1 = 0.001
h2
= (fx1 - fx_1)/(2*h1)
f0_1 print(f0_1, digits = 5)
## [1] -0.0093
= (fx1 - fx_1)/(2*h2)
f0_2 print(f0_2,digits = 5)
## [1] -0.93
# Turunan Kedua
= 1.2
x0 = 1.201
x1 = 1.199
x_1
= 0.36236
fx0 = 0.36143
fx1 = 0.36329
fx_1
= 0.1
h1 = 0.001
h2
= (fx1 - (2*fx0) + fx_1)/0.01
f0_1 print(f0_1,digits = 5)
## [1] -5.5511e-15
= (fx1 - (2*fx0) + fx_1)/h2**2
f0_2 print(f0_2,digits = 5)
## [1] -5.5511e-11
soal 4
<- function(ftn, a, b, n = 10) {
trapezoid <- (b-a)/n
h <- seq(a, b, by = h)
x.vec <- sapply(x.vec, ftn) # ftn(x.vec)
f.vec <- h*(f.vec[1]/2 + sum(f.vec[2:n]) + f.vec[n+1]/2)
Trap return(Trap)
}
<- function(x){
f ^2)*cos(x^2)
(x
}trapezoid(f,1.5,2.5,n = 10)
## [1] -0.4400977
soal 5
<- function(x,y){(y^2)*(1+2*x)
f1
}<- function(f, x0, y0, h, n){
euler <- x0
x <- y0
y
for(i in 1:n){
<- y0 + h*f(x0, y0)
y0 <- x0 + h
x0 <- c(x,x0)
x <- c(y, y0)
y
}
return(data.frame(x=x, y=y))
}= euler(f1, x0=0, y0=1, h=-0.5, n=2)
Jawaabeuler Jawaabeuler