Using the definition of the derivative, \(f'(x) = \lim_{h\to 0} \frac{f(x+h)-f(x)}{h}\), for \(f(x) = x^3 +2x^2\), the function will take the form:
\[f'x = \frac{(x+h)^3 + 2(x+h)^2 - x^3 - 2x^2}{h}\] The smallest value R likes to deal with, the machine’s epsilon, was too low and caused zeros to be returned. The square root of the epsilon (~1e-6) seemed to fix this.
#funciton def
foo <- function(x) {
return (x^3 + 2*x^2)
}
#Derivative using limit
f_prime <- function(foo, x) {
delta <- sqrt(.Machine$double.eps)
return ((foo(x+delta)- foo(x))/delta)
}
To compare our derivative function to a more exact computation, we will compare it to the derivative function, D, in R’s mosaic package.
fPrime_R <- D((x^3 + 2*x^2) ~ x)
Comparing our functions versus mosaic’s to see if they are “nearly” equal using R’s base all.equal, we see that they are very close:
all.equal(fPrime_R(seq(1:100)), f_prime(foo, seq(1:100)), tolerance = .Machine$double.eps)
## [1] "Mean relative difference: 8.514949e-09"
foo2 <- function(x) {
return(3*x^2 + 4*x)
}
anti_fPrime <- function(foo, lower, upper) {
delta <- 1e-6
anti_tmp <- 0
#Sum area each small defined strip under the funciton
for (x in seq(lower, upper, delta)) {
anti_tmp <- anti_tmp + foo(x)*delta
}
return(anti_tmp)
}
(custom_anti <- anti_fPrime(foo2, 1, 3))
## [1] 42.00002
Using mosaic again, we can calculate the anti derivative and compare results with our custom function.
anti_fPrimeR <- antiD((3*x^2 + 4*x) ~x, lower.bound = 1, force.numeric = TRUE)
(m_anti <- anti_fPrimeR(3))
## [1] 42
The values are very close. The custom function has a relative error to moasic’s antiD function of 5.476190410^{-7}.
Using integration by parts, solve for \(\int sin(x)cos(x) dx\)
Using the form \(\int u'v = uv - \int uv'\).
\[\int cos(x)sin(x) dx= sin^2(x) - \int sin(x)cos(x) dx\\ 2\int cos(x)sin(x) dx = sin^2(x)\\ \rightarrow \int sin(x)cos(x) dx = \frac{sin^2(x)}{2} + C\]
Using integration by parts, solve for \(\int x^2e^x dx\)
Using the form \(\int u'v = uv - \int uv'\).
\[\int e^xx^2 dx = e^xx^2 - \int e^x2x \space dx\]
\[\int e^xx^2 dx = e^xx^2 - [e^x2x - 2\int e^x \space dx]\\ = e^xx^2 - (e^x2x - 2e^x) + C\\ \rightarrow e^x(x^2 - 2x + 2) + C\]
What is \(\frac{d}{dx}(x cos(x))\)
Using the product rule
\(\frac{d}{dx}(x cos(x)) = cos(x) - x sin(x)\)
What is \(\frac{d}{dx}e^{x^4}\)
Using the chain rule
\(\frac{d}{dx}e^{x^4} = 4x^3e^{x^4}\)