Write a program to compute the derivative of \(f(x) = x^3 + 2x^2\) at any value of x. Your function should take in a value of x and return back an approximation to the derivative of \(f(x)\) evaluated at that value. You should not use the analytical form of the derivative to compute it. Instead, you should compute this approximation using limits.
\(f(x)' = \lim_{h\to 0} \frac {f(x + h) - f(x)}{h} = \lim_{h\to 0} \frac {(x + h)^3 + 2(x + h)^2 - x^3 - 2x^2}{h}\)
\(= \lim_{h\to 0} \frac {x^3 + 3hx^2 +3xh^2 + h^3 + 2x^2 + 4hx + 2h^2 - x^3 - 2x^2}{h} = \lim_{h\to 0} \frac {3hx^2 +3xh^2 + h^3 + 4hx + 2h^2}{h}\)
\(= \lim_{h\to 0} h^2 + 2h + 3xh + 4x + 3x^2 = 3x^2 + 4x\)
dervx <- function(x) {
# Take x, square it, multiply by 3, then add it to x multiplied by 4
dx <- 3 * x^2 + 4 * x
return(dx)
}
dervx(0)## [1] 0
dervx(1)## [1] 7
dervx(2)## [1] 20
dervx(3)## [1] 39
dervx(9)## [1] 279
dervx(-1)## [1] -1
dervx(-5)## [1] 55
Now, write a program to compute the area under the curve for the function \(3x^2 + 4x\) in the range \(x = |1, 3|\). You should first split the range into many small intervals using some really small \(\Delta x\) value (say 1e-6) and then compute the approximation to the area under the curve.
\(\int_{a \to b} f(x)dx = \lim_{n \to \infty} \sum_{i=1}^{n}f(x_i) \Delta x\)
So, for the range 1 to 3, we calculate \(3x^2 + 4x\) for x = 1 + delta, then multiply by delta to get the area, and keep adding up these little pieces of area until we get to 3.
# Initialize variables
sum <- 0
delt <- 1 * 10^-6
i <- 1
while (i < 3) {
i <- i + delt
sum <- sum + (3 * i^2 + 4 * i) * delt
}
sum## [1] 42.00002
Please solve these problems analytically (i.e. by working out the math) and submit your answers.
Use integration by parts to solve for \(\int sin(x)cos(x)dx\)
If \(u = sin(x)\), then \(du = cos(x)dx\), which gives \(\int sin(x)cos(x)dx = \int udu\)
\(\int udu = \frac{1}{2} u^2 + C\), since \(u = sin(x) = \frac{1}{2} sin^2(x) + C\)
\(\int sin(x)cos(x)dx = \frac{1}{2} sin^2(x) + C\)
Not sure if this technically integration by parts, but it is the way I could figure it out.
Use integration by parts to solve for \(\int x^2e^xdx\)
From our handout: \(\int fg'dx = fg - \int f'gdx\)
if \(f = x^2\), then \(f' = 2x\) and if \(g = e^x\), then \(g' = e^x\), then
\(\int x^2e^xdx = x^2e^x - \int 2xe^xdx\), then is we integrate by parts again, we get
\(= x^2e^x - 2xe^x + \int 2e^xdx = x^2e^x - 2xe^x + 2e^x + C\)
\(\int x^2e^xdx = e^x(x^2 + 2x + 2) + C\)
What is \(\frac{d}{dx}(xcos(x))\)?
\(\frac{d}{dx}(xcos(x)) = \frac{d}{dx} x \frac{d}{dx} cos(x) = 1 * -sin(x) = -sin(x)\)
What is \(\frac{d}{dx}(e^{x^4})\)?
\(\frac{d}{dx}(e^{x^4}) = e^{x^4}\)