Dosen Pengampu : Prof. Dr. Suhartono M.Kom Mata Kuliah : Kalkulus
Prodi : Teknik Informatika
Lembaga : Universitas Islam Negeri Maulana Malik Ibrahim Malang

Bab Aljabar Linier

Latihan Soal

Selesaikan sistem persamaan linier berikut menggunakan eliminasi Gauss!

soal -4x+4y=-1 -4x+2y-3z=-3 3x+1y-3z=-3

gauss_jordan <- function (a){
    m <- nrow (a)
    n <- ncol (a)
    piv <- 1
    
# cek elemen diagonal utama apakah bernilai nol
    for(row_curr in 1:m){
        if(piv <= n){
            i <- row_curr
            while(a[i, piv] == 0 && i < m){
                i <- i + 1
                if(i > m){
                    i <- row_curr
                    piv <- piv + 1
                    if(piv > n)
                        return (a)
                }
            }

# jika diagonal utama bernilai nol,lakukan row swapping
            if(i != row_curr)
                a <- swap_row(a, i, row_curr)
            
# proses pembentukan matriks reduced row echelon form
            piv_val <- a[row_curr , piv]
            a <- scale_row (a, row_curr , 1 / piv_val)
            for(j in 1: m){
                if(j != row_curr){
                    k <- a[j, piv]/a[row_curr, piv]
                    a <- replace_row (a, row_curr, j, -k)
                }
            }
            piv <- piv + 1
        }
    }
    return (a)
}
(m <- matrix (c(-4,4,-1,-4,2,-3,
              -3,3,1,3,-3,-3), nrow=3))
##      [,1] [,2] [,3] [,4]
## [1,]   -4   -4   -3    3
## [2,]    4    2    3   -3
## [3,]   -1   -3    1   -3

Bab Akar Persamaan Non Linier

Latihan Soal

Temukan akar persamaan dari persamaan non-linier f(x) = x3 - 2*x + 2 menggunakan metode tertutup dengan x0 = 0 dan x0 = 1/2!

root_fpi <- function(f, x0, tol=1e-7, N=100){
  iter <- 1
  xold <- x0
  xnew <- f(xold)
  
  while(abs(xnew-xold)>tol){
    iter <- iter+1
    if(iter>N){
      stop("No solutions found")
    }
    xold <- xnew
    xnew <- f(xold)
  }
  
  root <- xnew
  return(list(`function`=f, root=root, iter=iter))
}
root_fpi(function(x){1/exp(x)}, x0=0)
## $`function`
## function(x){1/exp(x)}
## <bytecode: 0x0000000014f36608>
## 
## $root
## [1] 0.5671433
## 
## $iter
## [1] 30
root_fpi(function(x){1/exp(x)}, x0=1/2)
## $`function`
## function(x){1/exp(x)}
## <bytecode: 0x0000000015828898>
## 
## $root
## [1] 0.5671433
## 
## $iter
## [1] 26

Bab Interpolasi dan Ekstrapolasi

Latihan Soal

Diberikan data titik (3,5), (0,-2), dan (4,1). Tentukan persamaan polinomial untuk melakukan interpolasi pada ketiga titik tersebut!

poly_inter <- function(x, y){
  if(length(x) != length(y))
    stop("Lenght of x and y vectors must be the same")
  
  n <- length(x)-1
  vandermonde <- rep(1, length(x))
  for(i in 1:n){
    xi <- x^i
    vandermonde <- cbind(vandermonde, xi)
  }
  beta <- solve(vandermonde, y)
  
  names(beta) <- NULL
  return(beta)
}
x <- c(3, 0, 4)
y <- c(5, -2, 1)

Koefisien persamaan polinomial dihitung menggunakan fungsi poly_inter()

(coeff <- poly_inter(x, y))
## [1] -2.000000  7.083333 -1.583333

Berdasarkan hasil perhitungan, diperoleh nilai β. Nilai tersebut selanjutnya digunakan untuk membentuk persamaan polinomial. Berikut merupakan persamaan polinomial yang terbentuk:

f(x)= -2x^2 + 7x - 1

Fungsi horner_poly() selanjutnya digunakan untuk mengevaluasi polinomial tersebut. Berikut adalah hasil substitusi x pada persamaan tersebut:

horner_poly <- function(x, coeff){
  n <- length(x)
  y <- rep(0, n)
  
  for(i in length(coeff):1){
    y <- coeff[i] + x * y
  }
  return(y)
}
horner_poly(0, coeff)
## [1] -2

Bab Diferensiasi dan Integrasi Numerik

Latihan Soal

Tuliskan fungsi R yang dapat melakukan integrasi Riemann dengan aturan titik kiri !

riemann <- function(f, a, b, m = 100){
  n_width <- (b-a)/m
  x <- seq(a, b-n_width, length.out = m) + n_width/2
  y <- f(x)
  
  return(sum(y)*abs(b-a)/m)
}

# m=2
riemann(function(x) x^2, a=0, b=1, m=2)
## [1] 0.3125

Bab Persamaan Diferensial

Latihan Soal

Tunjukkan 10 hasil iterasi metode Euler untuk persamaan diferensial f’(x,y) = y dimana x0=0, y0=2 dan step size h= 0,1!

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))
}
# metode numerik
f1 <- function(x,y){y}
num <- euler(f1, x0=0, y0=2, h=0.1, n=10)

# metode analitik
f2 <- function(x){sqrt(y)}
x0 <- 0
y0 <- 2
x <- x0
y <- y0

for(i in 1:10){
  y0 <- f2(x0+0.1)
  x0 <- x0+0.1
  x <- c(x, x0)
  y <- c(y, y0)
}
true <- data.frame(x=y, y=y)

Hasil Visualisasi

# membuat vektor data 
x <- c(0:11); y <- x*0.1

# loop
type <- c("p")
for (i in type){
  plot(x,y, type= i,
       main= paste(i))
}

Referensi 1. https://bookdown.org/moh_rosidi2610/Metode_Numerik