Universitas : UIN Maulana Malik Ibrahim Malang

Prodi : Teknik Informatika

Fakultas : Sains dan Teknologi

Dosen : Prof. Dr. SUHARTONO, M.Kom

Certainly! If you want to create a simple program in R to perform differentiation, you might use the symbolic computation capabilities of R through the Deriv package. If you haven’t installed the Deriv package yet, you can do so by running install.packages(“Deriv”). Once installed, you can use it to find derivatives.

Here’s a simple example of an R script that calculates the derivative of a function and evaluates it at a specific point:

# Install and load the mosaicCalc package
# install.packages("mosaicCalc")
library(mosaicCalc)
## Warning: package 'mosaicCalc' was built under R version 4.3.2
## 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
# Function to differentiate using mosaicCalc
f <- expression(x^3 + 6*x + 1)

# Differentiate the function
df <- D(f, "x")

# Display the original function and its derivative
print(f)
## expression(x^3 + 6 * x + 1)
print(df)
## 3 * x^2 + 6
# Create a function from the expression
f_function <- function(x) eval(f)
df_function <- function(x) eval(df)

# Plot the original function and its derivative
curve(f_function(x), from = -5, to = 5, col = "blue", main = "Differentiation using mosaic calculus")
curve(df_function(x), from = -5, to = 5, col = "red", add = TRUE)
legend("topright", legend = c("Original Function", "Derivative"), col = c("blue", "red"), lty = 1)

This script defines a simple quadratic function, calculates its derivative symbolically, and then evaluates the derivative at a specific point.

Please note that the Deriv package is just one of many ways to perform symbolic differentiation in R. Depending on your needs and the complexity of your functions, other packages or methods might be more appropriate.

Remember to adapt the code according to the specific function you want to differentiate. If you have a different function in mind or more specific requirements, feel free to provide more details, and I can help you further.