library(mosaicCalc)
## Loading required package: mosaic
## Registered S3 method overwritten by 'mosaic':
##   method                           from   
##   fortify.SpatialPolygonsDataFrame ggplot2
## 
## The 'mosaic' package masks several functions from core packages in order to add 
## additional features.  The original behavior of these functions should not be affected by this.
## 
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
## 
##     count, do, tally
## The following object is masked from 'package:Matrix':
## 
##     mean
## The following object is masked from 'package:ggplot2':
## 
##     stat
## The following objects are masked from 'package:stats':
## 
##     binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
##     quantile, sd, t.test, var
## The following objects are masked from 'package:base':
## 
##     max, mean, min, prod, range, sample, sum
## Loading required package: mosaicCore
## 
## Attaching package: 'mosaicCore'
## The following objects are masked from 'package:dplyr':
## 
##     count, tally
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
##      (status 2 uses the sf package in place of rgdal)
## 
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
## 
##     D

Numerical iteration adalah metode yang digunakan dalam kalkulus numerik untuk menemukan solusi aproksimatif dari persamaan non-linear. Metode ini melibatkan penggunaan suatu tebakan awal untuk solusi, yang kemudian diperbaiki berulang kali sampai ditemukan solusi yang cukup akurat.

Berikut adalah beberapa langkah dasar dalam metode iterasi numerik:

Mulai dengan tebakan awal untuk solusi, biasanya disebut x0. Hitung nilai fungsi pada \(x0\), yaitu \(f(x0)\). Gunakan nilai ini untuk memperbaiki tebakan awal dan mendapatkan tebakan baru, \(x1\). Ulangi langkah-langkah ini sampai perbedaan antara dua tebakan berturut-turut cukup kecil, atau sampai jumlah iterasi mencapai batas tertentu. Dalam konteks Mosaic Calculus, metode ini mungkin melibatkan penggunaan teknik khusus atau penyesuaian untuk meningkatkan kecepatan konvergensi atau akurasi solusi1

# Fungsi yang ingin kita temukan akarnya
f <- function(x) {
  return(x^2 - 4)
}

# Turunan dari fungsi tersebut
df <- function(x) {
  return(2*x)
}

# Nilai awal
x0 <- 1

# Toleransi kesalahan
tol <- 1e-6

# Maksimum iterasi
max_iter <- 100

# Inisialisasi vektor untuk menyimpan nilai-nilai x
x_vals <- c(x0)

for (i in 1:max_iter) {
  # Hitung x berikutnya
  x1 <- x0 - f(x0)/df(x0)
  
  # Tambahkan ke vektor
  x_vals <- c(x_vals, x1)
  
  # Periksa apakah kita telah mencapai toleransi kesalahan
  if (abs(x1 - x0) < tol) {
    break
  }
  
  # Perbarui x0
  x0 <- x1
}

# Buat plot iterasi
plot(x_vals, type = "l", main = "Numerical Iteration", xlab = "Iteration", ylab = "x")