Optimization

Taufiq Luthfi Nurrohim

2023-12-08

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

Di artikel ini kita akan membahas apa itu Optimization. Optimization adalah proses mencari nilai minimum atau maksimum dari suatu fungsi. Nilai minimum atau maksimum ini disebut sebagai nilai optimal. Optimization sering digunakan dalam berbagai bidang, seperti ekonomi, teknik, ilmu alam, dan lainnya, untuk menyelesaikan masalah yang melibatkan biaya, keuntungan, efisiensi, keseimbangan, dan sebagainya.

Salah satu fungsi yang dapat Kita gunakan untuk Optimization adalah optimize (). Fungsi ini memungkinkan Kita untuk mencari nilai minimum atau maksimum dari suatu fungsi dalam suatu interval. Kita perlu memberikan tiga argumen ke fungsi optimize (): fungsi yang ingin dioptimalkan, interval pencarian, dan apakah Kita ingin mencari nilai minimum atau maksimum.

# Langkah 1: Mendefinisikan fungsi yang ingin dioptimalkan
f <- function(x) { x^3 - 6*x^2 + 9*x - 4 }

# Langkah 2: Mencari nilai minimum dan maksimum fungsi f di interval [-1, 4]
x <- seq(-1, 4, length.out = 1000)
y <- f(x)
min_f_x <- x[which.min(y)]
min_f_y <- min(y)
max_f_x <- x[which.max(y)]
max_f_y <- max(y)

# Langkah 3: Menampilkan hasil Optimization
cat("Minimum:", min_f_x, "f(min) =", min_f_y, "\n")
## Minimum: -1 f(min) = -20
cat("Maksimum:", max_f_x, "f(max) =", max_f_y, "\n")
## Maksimum: 4 f(max) = 0
# Langkah 4: Membuat plot fungsi f
plot(x, y, type = "l", xlim = c(min_f_x - 1, 4), ylim = c(min_f_y - 2, max_f_y + 2))
points(min_f_x, min_f_y, col = "black", pch = 19)
points(max_f_x, max_f_y, col = "deeppink", pch = 19)
text(min_f_x, min_f_y - 1, "Min", col = "black")
text(max_f_x, max_f_y + 1, "Max", col = "deeppink")