Chapter 8 Integrals and integration
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
##
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
##
## D
f <- makeFun( A * x ^ 2 ~ x, A = 0.5)
f(1)
## [1] 0.5
f(2)
## [1] 2
f(3)
## [1] 4.5
df <- D(f(x) ~ x)
df(1)
## [1] 1
df(2)
## [1] 2
df(3)
## [1] 3
slice_plot(f(x) ~ x, domain(x = -1:1)) %>%
gf_labs(title = "Original function f(x)")
slice_plot(df(x) ~ x, domain(x =-1:1), color = "pink") %>%
gf_labs(title = "New function df(x), the derivative of f(x)")
f1 <- makeFun(sin(x ^ 2) ~ x)
f2 <- makeFun(sin(x ^ 2) + 3 ~ x)
f3 <- makeFun(sin(x ^ 2) - 100 ~ x)
f1(1)
## [1] 0.841471
f2(1)
## [1] 3.841471
f3(1)
## [1] -99.15853
df1 = D(f1(x) ~ x)
df2 = D(f2(x) ~ x)
df3 = D(f3(x) ~ x)
df1(1)
## [1] 1.080605
df2(1)
## [1] 1.080605
df3(1)
## [1] 1.080605
f
## function (x, A = 0.5)
## A * x^2
## <bytecode: 0x000001e599556908>
df
## function (x, A = 0.5)
## 2 * A * x
## <bytecode: 0x000001e59a3b36d0>
antiD(f(x) ~ x)
## function (x, A, C = 0)
## (x^3 * A)/3 + C
antiD(df(x) ~ x)
## function (x, A, C = 0)
## A * x^2 + C
fun = antiD( x^2 ~ x )
fun
## function (x, C = 0)
## x^3/3 + C
fun(x = 2) - fun(x = -1)
## [1] 3
gaussian <-
makeFun((1/sqrt(2*pi*sigma^2)) *
exp( -(x-mean)^2/(2*sigma^2)) ~ x,
mean=2, sigma=2.5)
slice_plot(gaussian(x) ~ x, domain(x = -5:10)) %>%
slice_plot(gaussian(x, mean=0, sigma=1) ~ x, color="blue")
erf <- antiD(gaussian(x, mean=m, sigma=s) ~ x)
erf
## function (x, C = 0, m, s)
## {
## F <- makeF(gaussian(x, mean = m, sigma = s))
## evalFun(F, x = x, m = m, s = s, .const = C)
## }
## <environment: 0x000001e598eec098>
LATIHAN
erf <- antiD(gaussian(x, mean=m, sigma=s) ~ x)
erf
## function (x, C = 0, m, s)
## {
## F <- makeF(gaussian(x, mean = m, sigma = s))
## evalFun(F, x = x, m = m, s = s, .const = C)
## }
## <environment: 0x000001e59933cae8>
erf(x = 1, m=0, s=1) - erf(x = 0, m=0, s=1)
## [1] 0.3413447
erf <- antiD(gaussian(x, mean=m, sigma=s) ~ x)
erf
## function (x, C = 0, m, s)
## {
## F <- makeF(gaussian(x, mean = m, sigma = s))
## evalFun(F, x = x, m = m, s = s, .const = C)
## }
## <environment: 0x000001e59a828040>
erf(x = 2, m=0, s=1) - erf(x = 0, m=0, s=1)
## [1] 0.4772499
erf <- antiD(gaussian(x, mean=m, sigma=s) ~ x)
erf
## function (x, C = 0, m, s)
## {
## F <- makeF(gaussian(x, mean = m, sigma = s))
## evalFun(F, x = x, m = m, s = s, .const = C)
## }
## <environment: 0x000001e59b034e30>
erf(x = 0, m=0, s=2) - erf(x = 2, m=0, s=2)
## [1] -0.3413447
erf <- antiD(gaussian(x, mean=m, sigma=s) ~ x)
erf
## function (x, C = 0, m, s)
## {
## F <- makeF(gaussian(x, mean = m, sigma = s))
## evalFun(F, x = x, m = m, s = s, .const = C)
## }
## <environment: 0x000001e59b48d938>
erf(x = -Inf, m=3, s=10) - erf(x = 3, m=3, s=10)
## [1] -0.5
erf <- antiD(gaussian(x, mean=m, sigma=s) ~ x)
erf
## function (x, C = 0, m, s)
## {
## F <- makeF(gaussian(x, mean = m, sigma = s))
## evalFun(F, x = x, m = m, s = s, .const = C)
## }
## <environment: 0x000001e59ba21068>
erf(x = Inf, m=3, s=10) - erf(x = -Inf, m=3, s=10)
## [1] 1
REFERENSI
https://dtkaplan.github.io/RforCalculus/derivatives-and-differentiation.html#partial-derivatives