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, were retired 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:.
## 
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
## 
##     D

Zero finding, atau penemuan nol, adalah proses untuk menemukan akar suatu fungsi matematis, yaitu nilai dari variabel independen di mana fungsi tersebut menjadi nol. Ini merupakan topik dalam kalkulus yang memiliki berbagai aplikasi dalam berbagai bidang, termasuk analisis data, ilmu pengetahuan, dan pemodelan statistik.

Untuk memberikan contoh grafik, mari kita lihat fungsi ƒ (x) = x² - 4, yang memiliki dua akar di x = -2 dan x = 2. Kita dapat menggunakan R untuk membuat grafik fungsi ini bersama dengan garis sumbu y = 0.

library(mosaicCalc)
# Install and load necessary libraries
if (!require("ggplot2")) {
  install.packages("ggplot2")
  library("ggplot2")
}

# Define the function
f <- function(x) {
  return(x^2 - 4)
}

# Generate x values
x_values <- seq(-3, 3, by = 0.1)

# Create a data frame
data <- data.frame(x = x_values, y = f(x_values))

# Create the plot
plot <- ggplot(data, aes(x, y)) +
  geom_line() +
  geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
  geom_point(aes(x = c(-2, 2), y = c(0, 0)), color = "blue", size = 3) +
  labs(title = "Graph of f(x) = x^2 - 4",
       x = "x",
       y = "f(x)")

Dalam grafik ini, garis biru adalah grafik fungsi f(x) = x² - 4, dan garis merah adalah garis sumbu y = 0. Titik biru pada sumbu x menunjukkan lokasi akar fungsi (titik di mana f(x) = 0). Dalam contoh ini, fungsi memiliki dua akar di 2 = -2 dan x=2.

Misalkan terdapat f(x) = 10 sehingga mengimplementasikan fungsi yang sedikit berbeda: f(x) - 10. jika dapat menemukan x0 seperti f(x0) - 10 = 0, itu juga akan menjadi x0, f(x) = 10. Mencari angka nol dan fungsi tersebut f(X) - 10 yang sesuai dengan penyelesaian f(x) = 10

library(mosaicCalc)
Zeros(f(x) - 10 ~ x, bounds(x=-4:4))
## # A tibble: 2 x 2
##       x     .output.
##   <dbl>        <dbl>
## 1 -3.74 -0.000000983
## 2  3.74 -0.000000983
## # A tibble: 2 × 2
##         x      .output.
##     <dbl>         <dbl>
## 1 -2.92   -0.0000000344
## 2  0.0795 -0.00000118

Keluaran yang dihasilkan Zeros()berupa bingkai data dengan satu baris untuk masing-masingnya x0 ditemukan. Di sini, dua nilai ditemukan:x0 = -2.92 dan x0 = 0.0795, yang seharusnya output f(x0) adalah nol. Pikirkan Zeros()sebagai cara untuk menyempurnakan jawaban yang Anda temukan secara grafis. Jadi sebelum menggunakan Zeros(), buatlah grafiknya.

library(mosaicCalc)
slice_plot(f(x) ~ x, bounds(x=-4:4)) %>%
  gf_hline(yintercept = ~ 10, color="magenta")