The limit definition of the derivative: \[ f'(x) = \lim_{h \to 0} \frac{f(x+h)-f(x)}{h} \]
To calculate this limit we will compute the following two limits and average those results together:
\[ f'(x) = \lim_{h \to 0^+} \frac{f(x+h)-f(x)}{h} \ f'(x) = \lim_{h \to 0^-} \frac{f(x+h)-f(x)}{h} \]
Let \(h\) be a small value, say 1e-6. Once we have the above values we can then average the two limits to get a linear approximation of the actual value of the limit.
derivative <- function(x, h=1e-6){
# Approximates the derivative of the function f(x)=x^3 + 2x^2 using an approximation of the limit.
#
# Inputs:
# x = The value for which we need to calculate the derviative.
# h = The small value that is added or subtracted from x to approximate the limit.
#
# Returns:
# The approximate value of the derivative calculated by approximating the limit.
fxhPos <- (x+h)^3 + 2*(x+h)^2
fxhNeg <- (x-h)^3 + 2*(x-h)^2
fx <- x^3 + 2*x^2
upperdx <- (fxhPos - fx)/h
lowerdx <- (fxhNeg - fx)/-h
dx <- (lowerdx + upperdx)/2
return(dx)
}
Client Calls:
derivative(4)
## [1] 64
derivative(-2)
## [1] 4
To cross check our results lets calculate these manually: \[ f'(x) = 3x^2 + 4x \] , when \(x = 5\) and \(x = -2\), we expect to get \(f'(4) = 64\) and \(f'(x) = 4\) respectively.
we will be calculating the integral of a given function by taking a series of small intervals and adding up the area of each rectangle. This can be formulated as: \[ \int_{a}^{b} f(x) = \lim_{x \to \infty} \sum_{i=1}^{n} {f(x_i)} \Delta x \]
For this problem we approximate the integral of \(f(x) = 3x^2 + 4x\) over the interval \(x = [1,3]\).
To calculate this value we will divide the interval into a number of small subintervals using \(\Delta x =\) 1e-6. We will then sum up the area of the small rectangles that are created by \(f(x_i) \Delta x\) and return this value.
areaunderCurve <- function(){
# Compute the area under the curve of 3x^2 + 4x, in the range x = [1, 3]
#
# Inputs:
# none
#
# Returns:
# area = The approximation of the area under the curve.
a <- 1
b <- 3
deltax <- 1e-6
subint <- (b-a)/deltax # here, we are dividing the region into subintervals
area <- 0
for(i in 1:subint){
area <- area + (3*a^2 + 4*a)*deltax # using the above formula
#increment a now by deltax
a <- a + deltax
}
return(area)
}
Client Call:
areaunderCurve()
## [1] 42
Lets cross verify against the known value of the integral: \[ \int_{1}^{3} {3x^2 + 4x} dx = \left. {x^3 + 2x^2} \right|_{1}^{3} = 42 \]
Use integration by parts to find \(\int \sin(x) \cos(x) dx\)
From the integration by parts formula \[ \int f g' dx = f g - \int f' g dx \], first we need to pick our \(f\) and \(g'\) and calculate our \(f'\) and \(g\): \[ f = \cos(x) \; f' = -\sin(x) \ g' = \sin(x) \; g = -\cos(x) + C \]
Applying integration by parts we get: \[ \int \sin(x) \cos(x) dx = \cos(x) * -\cos(x) + C - \int -\sin(x) * - \cos(x) dx \ \int \sin(x) \cos(x) dx = -\cos^2(x) + C - \int \sin(x) \cos(x) dx \ 2 \int \sin(x) \cos(x) dx = -\cos^2(x) + C \ \int \sin(x) \cos(x) dx = -\frac{\cos^2(x)}{2} + C \]
Use integration by parts to find \(\int x^2 e^x dx\)
Similar to prior problem, first we need to pick our \(f\) and \(g'\) and calculate our \(f'\) and \(g\): \[ f = x^2 \; f' = 2x \ g' = e^x \; g = e^x + C \]
Applying integration by parts we get: \[ \int x^2 e^x dx = x^2 e^x + C - \int 2x e^x dx \]
Apply integration by parts again to calculate the integral. So picking new values for \(f\) and \(g'\) we get the following: \[ f = 2x \; f' = 2 \ g' = e^x \; g = e^x + C \]
Applying this to the previous interval gives us the following: \[ \int x^2 e^x dx = x^2 e^x + C - (2x e^x - 2 \int e^x dx) \ \int x^2 e^x dx = x^2 e^x - 2x e^x - 2e^x + C \ \int x^2 e^x dx = e^x(x^2 - 2x - 2) + C \]
Find \(\frac{d}{dx} (x \cos(x))\)
Apply the product rule:
\[(\frac{d}{dx}(x \cos(x)) = \cos(x) - x\sin(x))\]
Find \(\frac{d}{dx} (e^{x^4})\)
Apply the chain rule.
\[(\frac{d}{dx}(e^{x^4})=4x^3e^{x^4})\]