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.
derivative<-function(x){
f<-function(x){
x^3 + 2*x^2
}
h<- 10^-6
value<- (f(x + h) - f(x))/ h
return(value)
}
derivative(2)
## [1] 20.00001
derivative(5)
## [1] 95.00002
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 ∆x value (say 1e-6) and then compute the approximation to the area under the curve.
# split the range into many intervals
x <- seq(1, 3, by=1e-6)
# represent the function
f <- 3*(x^2) + 4*x
# calculate area
area <- f*(1e-6)
sum(area)
## [1] 42.00002
Please solve these problems analytically (i.e. by working out the math) and submit your answers.
\[\int f(x) g'(x) dx = f(x) g(x) - \int f'(x) g(x) dx\] \[f(x) = cos(x); f'(x) = -sin(x); g'(x) = -sin(x); g(x) = cos(x)\] \[\int -sin(x)cos(x) dx = cos^{2}x - \int -sin(x)cos(x) dx\] \[-2\int sin(x)cos(x) dx = cos^{2}x\] \[\int sin(x)cos(x) dx = -\frac{1}{2} cos^2(x) + C\]
Using,\[\int f(x) g'(x) dx = f(x) g(x) - \int f'(x) g(x) dx\], \[f(x)= x^{2}; g'(x)=e^{x}; f'(x)=2x; g(x)=\int e^{x}dx = e^{x}\] \[\int f(x) g'(x) dx = x^{2}e^{x} -\int 2xe^{x} dx\] \[ x^{2}e^{x} - 2\int xe^{x} dx------------eq1 \]
Now solving \[\int xe^{x} dx \] \[ f(x)=x; g'(x)= e^{x}; f'(x)= 1; g(x)=e^{x}\] \[\int xe^x dx = xe^{x} - \int e^{x}dx\] \[xe^{x} - e^{x}\] Now putting the value in eq1 \[x^{2}e^{x} - 2xe^{x} - 2e^{x} + C\]
\[\frac{d}{dx}(xcos(x)) = cos(x)(1) + x(-sin(x))\] \[\frac{d}{dx}(xcos(x)) = cos(x) - xsin(x)\]
\[\frac{d}{dx}(e^{x^{4}}) = e^{x^{4}}(\frac{d}{dx}(x^{4}))\] \[\frac{d}{dx}(e^{x^4}) = e^{x^4}4x^3\].