Nama : Sausan Shalihah Alfirdausi

NIM : 230605110064

Mata Kuliah : Kalkulus

Dosen Pengampu : Prof. Dr. Suhartono,M.Kom

Program Studi : Teknik Informatika

Universitas : Universitas Islam Negeri Malang

library(mosaicCalc)
## Loading required package: mosaic
## Warning: package 'mosaic' was built under R version 4.3.2
## 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

Kode yang diberikan adalah potongan kode dalam bahasa pemrograman R yang menggunakan paket-paket mosaicCalc dan ggplot2 untuk membuat grafik fungsi dan turunannya. Berikut adalah penjelasan perbaris dari kode tersebut:

  1. library(mosaicCalc): Memuat paket mosaicCalc. Paket ini mungkin digunakan untuk melakukan kalkulasi statistik dan visualisasi data.

  2. library(ggplot2): Memuat paket ggplot2, yang merupakan paket yang sangat populer untuk membuat visualisasi grafik dalam R.

  3. f <- function(x) {...}: Mendefinisikan fungsi f(x) yang merepresentasikan fungsi kuadratik x^2 - 2*x + 1.

  4. df <- D(expression(x^2 - 2*x + 1), "x"): Menghitung turunan pertama dari fungsi terhadap variabel x dan menyimpannya dalam objek df. Fungsi D di sini mungkin berasal dari paket mosaicCalc.

  5. x_vals <- seq(-5, 5, by = 0.1): Membuat vektor nilai x dari -5 hingga 5 dengan interval 0.1.

  6. y_vals <- sapply(x_vals, f): Menghitung nilai fungsi f(x) pada setiap nilai x dan menyimpannya dalam vektor y_vals.

  7. dy_vals <- sapply(x_vals, function(x) eval(df)): Menghitung nilai turunan pertama fungsi pada setiap nilai x dan menyimpannya dalam vektor dy_vals.

  8. df <- data.frame(x = x_vals, y = y_vals, dy = dy_vals): Membuat dataframe dari vektor x_vals, y_vals, dan dy_vals.

  9. Blok berikutnya menggunakan ggplot untuk membuat grafik dari dataframe yang telah dibuat:

    • ggplot(df, aes(x = x)): Menentukan dataframe df dan memberikan estetika variabel x.
    • geom_line(aes(y = y), color = "pink"): Menambahkan garis untuk nilai y dengan warna merah muda.
    • geom_line(aes(y = dy), color = "skyblue"): Menambahkan garis untuk nilai turunan dy dengan warna biru langit.
    • ggtitle("Function and Its Derivative"): Menambahkan judul plot.
    • xlab("x") dan ylab("y"): Menambahkan label sumbu x dan y.
    • scale_color_manual(values = c("pink", "skyblue"), labels = c("Function", "Derivative")): Mengatur warna garis secara manual dan menambahkan legenda.
    • theme(legend.position = c(0.8, 0.9)): Menentukan posisi legenda pada koordinat (0.8, 0.9) di dalam plot.

Kode ini menghasilkan plot yang menunjukkan grafik fungsi kuadratik dan grafik turunannya dalam satu plot menggunakan ggplot2.

library(ggplot2)


f <- function(x) {
  return(x^2 - 2*x +1)
}


df <- D(expression(x^2 - 2*x +1), "x")

# Membuat rentang nilai x
x_vals <- seq(-5, 5, by = 0.1)

# Menghitung nilai y dari fungsi dan turunannya
y_vals <- sapply(x_vals, f)
dy_vals <- sapply(x_vals, function(x) eval(df))

# Membuat dataframe dari vektor
df <- data.frame(x = x_vals, y = y_vals, dy = dy_vals)

# Membuat plot fungsi dan turunannya
ggplot(df, aes(x = x)) +
  geom_line(aes(y = y), color = "pink") +
  geom_line(aes(y = dy), color = "skyblue") +
  ggtitle("Function and Its Derivative") +
  xlab("x") +
  ylab("y") +
  scale_color_manual(values = c("pink", "skyblue"), labels = c("Function", "Derivative")) +
  theme(legend.position = c(0.8, 0.9))