Programmed Solutions

Derivative

The derivative of the equation \(f(x) = x^3 + 2x^2\) can be calculated for a given value \(a\) using \[f'(a) = \lim_{x \to a} \frac{f(x) - f(a)}{x - a} = \lim_{x \to a} \frac{x^3 + 2x^2 - a^3 - 2a^2}{x-a}\]

To ease the computational stress of analytically calculating the limit by approaching \(a\) in small steps, this equation can be simplified:

\[\begin{align} f'(a) &= \lim_{x \to a} \frac{x^3 + 2x^2 - a^3 - 2a^2}{x-a} = \lim_{x \to a} \frac{(x - a)(x^2 + a^2 + 2x + 2a + ax)}{x - a} \\ &= \lim_{x \to a} \ x^2 + a^2 + 2x + 2a + ax \end{align}\]

Since directly entering \(x = a\) into this equation does not create division by zero, a fairly simple function can be coded to get the derivative for a given value of \(a\):

fprime <- function(a) {
  x <- a
  x^2 + a^2 + 2*x + 2*a + a*x
}

This function can be tested for a number of values and compared to the results of the built-in deriv function:

a <- -3:3
fprime(a)
[1] 15  4 -1  0  7 20 39
x <- a
as.vector(attr(eval(deriv(~ x^3 + 2*x^2, 'x')), 'gradient'))
[1] 15  4 -1  0  7 20 39

Integral

To calculated the definite integral of the function, the area under the function will be calculated: \[I = \sum_{x=1}^{x=3} 3x^2 + 4x \ \Delta x\] where \(\Delta x = 1 \times 10^{-6}\).

del_x <- 1e-6
x_range <- seq(1, 3, del_x)
area <- 0
for (i in 2:length(x_range)) {
  y_high <- 3 * x_range[i]^2 + 4*x_range[i]
  y_low  <- 3 * x_range[i - 1]^2 + 4*x_range[i - 1]
  area_inc <- (y_high) * del_x
  area <- area + area_inc
}
area
[1] 42.00002

This result of ~42 can be verified analytically, or using the built-in integrate function:

integrate(function(x) {3*x^2 + 4*x}, 1, 3)
42 with absolute error < 4.7e-13

Analytical Solutions

Problem 1

Setting \(u = \sin(x)\) gives \(du = \cos(x) dx\). Rearranging this gives \(du = \frac{dx}{\cos(x)}\). Subtituting this into the original problem, \[\int \sin(x) \cos(x) dx = \int u du = \frac{1}{2} u^2 + C\] Now substituting back in for \(u\), \[\int \sin(x) \cos(x) dx = \frac{1}{2} \sin^2(x) + C\]

Problem 2

\(\int x^2 e^x dx\) can be represented as \(\int u(x)dv\) where \(u(x) = x^2\) and \(dv = e^x dx\). Using these, \(du = 2xdx\) and \(v(x) = e^x\).

Using these functions to integrate by parts:
\[\int u(x)dv = u(x)v(x) - \int v(x)du = x^2 e^x - 2 \int x e^x dx + C\]

Performing integration by parts on the second integral, using \(u(x) = x\) and \(dv = e^x dx\); \(du=dx\) and \(v(x) = e^x\). \[\int x^2 e^x dx = x^2 e^x - 2 \left[ x e^x - \int e^x dx \right] = x^2 - e^x - 2xe^x + C \\ \int x^2 e^x dx = e^x \left( x^2 - 2x +2 \right) + C\]

Problem 3

For a product of functions, \(\left[ f(x) g(x) \right]' = f'(x)g(x) + g'(x)f(x)\).

Substituting in \(f(x) = x\) and \(g(x) = \cos(x)\),

\[\frac{d}{dx} \left[ x \cos(x) \right] = 1 \times \cos(x) + (-\sin(x)) \times x = \cos(x) - x\sin(x)\]

Problem 4

For an exponential function, \(\frac{d}{dx} e^u = e^u \frac{du}{dx}\). Using \(u = 4x^3\), \[\frac{d}{dx} e^{x^4} = e^{x^4} \times \frac{d}{dx} x^4 = 4 x^3 \times e^{x^4}\]