Write a program to compute the derivative of f(x) = x3 + 2x2 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_x = lim h->0 (f(x+h) -f(x))/h
#lets create f(x)
f_x <- function(x){
result <- (x^3) + (2*x^2)
return(result)
}
#create Derivative of x using limit theorem
#use h that is very small, I will use 0.0000001
f_derivative <- function(x){
(f_x(x+0.0000001) - f_x(x))/0.0000001
}
Lets test the derivative function
f_derivative(2)
## [1] 20
Let’s compare with the analytical function of the derivative
f_analytical_der <- function(x){
3*x^2 + 4*x
}
f_analytical_der(2)
## [1] 20
Now, write a program to compute the area under the curve for the function 3x2+4x in the range x = [1; 3]. You should rst 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.
#Create F(x)
integral.f_x <- function(x){
3*x^2 + 4*x
}
#define delta_x
interval <- 1e-6
#Break up the integration range into small intervals of delta x
array <- seq(from=1, to=3, by= interval)
#compute the Approximate integral of F(x)
integral <-0
for(i in 2:length(array))
{
integral <- integral + interval * integral.f_x(array[i])
}
integral
## [1] 42.00002
Use integration by parts to solve for R sin(x)cos(x)dx
We have to solve this in terms of u substitution
Intergral(sin(x)cos(x)dx) let u = sin(x) du = cos(x)dx
interIntergral(sin(x)cos(x)dx) = integral(u*du) = u^2/2 + C
= sin^2(x)/2 + C
Use Integration by parts to sovle for integral(x^2 * e^x dx)
We can solve this with integration by parts
let u = x^2 dv = e^xdx du = 2xdx v= e^x
integral(x^2 * e^x dx) = x2ex -2(integral(xe^xdx))
Now we have to also solve integral(xe^xdx) with integration by parts method integral(xe^xdx) let u=x dv = e^x dx du=dx v = e^x
integral(x*e^xdx) = xe^x - integral(e^xdx) = xe^x -e^x
Now lets go back to the original equation and finish the solution
integral(x^2 * e^x dx)
= x^2e^x -2*(integral(x*e^xdx))
= (x^2)*e^x - 2*(xe^x -e^x)
= e^x*(x^2 -2x +2)
What is d/dx (x cos(x))?
We will use chain rule
d/dx(x)cos(x) + xd/dx(cos(x))
=1cos(x) + x(-sin(x)) =cos(x)-xsin(x)
What is d/dx (e(x4))?
Using chain rule
= (e^x4) * d/dx(x^4) = 4(x^3)(e^x4)