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_limit<- function(delta, x){
#delta <-0
# let call 2x^2 = u
# let call w = x^3
fprime_u <- (2* (x +delta)^2 - x^2) / delta
fprime_u <- (2* (x^2 + 2*x*delta + delta^2) - 2*x^2 ) / delta
fprime_u <- (4*x*delta +2*delta^2 ) /delta
fprime_u <- delta*(4*x + 2*delta) /delta
fprime_u <- (4 * x + 2* delta)
#as delta goes toward 0, 4x + delta becomes x. hence
#fprime <- 2 * x
fprime_w <- ((x + delta)^3 - x^3 ) / delta
fprime_w <- (x^3 + 3*x^2*delta + 3 * x * delta^2 + delta^3 - x^3 ) / delta
fprime_w <- (3*x^2*delta + 3 * x * delta^2 + delta^3 ) / delta
fprime_w <- delta * ( 3*x^2 + 3 * x * delta + delta^2) / delta
fprime_w <- ( 3*x^2 + 3 * x * delta + delta^2)
#as delta goes toward 0, ( 3*x^2 + 3 * x * delta + delta^2) becomes 3*x^2
fprime <- fprime_w + fprime_u
return(fprime)
}
derivative_limit(.00005,1)
## [1] 7.00025
derivative_limit(.00002,3)
## [1] 39.00022
derivative_limit(.0000001,1)
## [1] 7.000001
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.
x <- seq(1, 3, by=1e-6)
y <- 3*(x^2) + 4*x
yareas <- y*1e-6
sum(yareas)
## [1] 42.00002
Plotting the area of the curve for the function 3x^2+4x:
library(ggplot2)
z<- data.frame(x,y)
ggplot(z,aes(x = x,y = y,fill=TRUE)) +
geom_area( position = 'stack') +
geom_area( position = 'stack', colour="black", show_guide=FALSE)
Let u = sin(x), then d(u)/dx = cos(x)
And d(x) = d(u) /cos(x)
Hence, Integral (sin(x)cos(x)dx) = Integral[ u cos(x) dx ]
= Integral [ u cos(x) d(u) /cos(x)]
= Integral [ u d(u) ]
= 1/2 u^2
= 1/2 sin^2(x)
Using the below formula: Integral[ u * v dx ] = u * Integral [v dx] - Integral[ uā * Integral(v)] Let u = x^2 and v = e^x Hence: Integral[ u * v dx] = x^2 * Integral(e^x dx) - Integral [2 x e^x ]
= x^2 * e^x - 2 Integral[ x * e^x ]
= x^2 * e^x - 2 [ x * e^x - Integral( 1 * e^x) ]
= x^2 * e^x - 2 [ x * e^x - e^x ]
= x^2 * e^x - (2 * x * e^x ) + 2 * e^x
d (x cos(x)) / dx = xā*cos(x) + x cos(x)ā
= cos(x) - x*sin(x)
Using chain rule:
d e^u(x)/dx = e^u(x) * d(u)/dx
In our case u(x) = x4, hence:
d e^u(x) /dx = e^u(x) * 4 * x^3
= e^(x^4) * 4 * x^3