Write a program to compute the derivative at a given value. Write a program to compute hte integral for a given range. Solve derivatives and integrals analytically for given functions.
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.
\[f^{ ' }\left( x \right) = \lim _{ x\rightarrow h }{ \frac { f\left( x+h \right) -f\left( x \right) }{ h } } \tag{1}\]
slope <- function(f, x) {
d <- 1e-6
fx <- eval(parse(text=gsub("x", x, f)))
fxh <- eval(parse(text=gsub("x", x + d, f)))
derivative <- (fxh - fx) / d
return(derivative)
}
slope("x^3 + 2*x^2", 605)
## [1] 1100495
Now, write a program to compute the area under the curve for the function \(3x^{ 2 }+4x\) in the range \(x=\left[ 1, 3 \right]\). 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.
\[\int _{ a }^{ b }{ f\left( x \right)dx } =\lim _{ n\rightarrow \infty }{ \sum _{ i=1 }^{ n }{ f\left( { x }_{ i } \right) \Delta x } } \tag{2}\]
area <- function(f, a, b){
area <- 0
d <- 1e-6
n <- seq(a, b, d)
for (i in 1:length(n)) {
area <- eval(parse(text=gsub("x", n[i], f))) * d + area
}
return(area)
}
area("3*x^2+4*x", 1, 3)
## [1] 42.00002
Please solve these problems analytically (i.e. by working out the math).
\[\frac { d }{ dx } \left[ uv \right] =uv'+vu' \tag{3.1.1}\] \[\frac { d }{ dx } \left[ x\cos { \left( x \right) } \right] =-x\sin { \left( x \right) } +\cos { \left( x \right) } \tag{3.1.2}\] \[\frac { d }{ dx } \left[ x\cos { \left( x \right) } \right] =\cos { \left( x \right) } -x\sin { \left( x \right) } \tag{3.1.3}\]
\[\frac { d }{ dx } \left[ { e }^{ u } \right]={ e }^{ u }u' \tag{3.2.1}\] \[\frac { d }{ dx } \left[ { e }^{ { x }^{ 4 } } \right] ={ e }^{ { x }^{ 4 } }4{ x }^{ 3 } \tag{3.2.2}\] \[\frac { d }{ dx } \left[ { e }^{ { x }^{ 4 } } \right] =4{ x }^{ 3 }{ e }^{ { x }^{ 4 } } \tag{3.2.3}\]
Please solve these problems analytically (i.e. by working out the math).
\[\int { uv' } =uv-\int { vu' } \tag{4.1.1}\] \[Let\quad \begin{cases} u=\cos { \left( x \right) } \\ v'=\sin { \left( x \right) } dx \end{cases}\Rightarrow \begin{cases} u'=-\sin { \left( x \right) } dx \\ v=-\cos { \left( x \right) } \end{cases} \tag{4.1.1}\] \[\int { \sin { \left( x \right) } \cos { \left( x \right) } dx } =-\cos { \left( x \right) } \cos { \left( x \right) } -\int { \cos { \left( x \right) } \sin { \left( x \right) } dx } +C \tag{4.1.3}\] \[2\int { \sin { \left( x \right) } \cos { \left( x \right) } dx } =-\cos ^{ 2 }{ \left( x \right) } + C \tag{4.1.4}\] \[\int { \sin { \left( x \right) } \cos { \left( x \right) } dx }=-\frac { 1 }{ 2 } \cos ^{ 2 }{ \left( x \right) } +C \tag{4.1.5}\]
\[\int { uv' } =uv-\int { vu' } \tag{4.2.1}\] \[Let\quad \begin{cases} u={ x }^{ 2 } \\ v'={ e }^{ x }dx \end{cases}\Rightarrow \begin{cases} u'=2xdx \\ v={ e }^{ x } \end{cases} \tag{4.2.2}\] \[\int { { x }^{ 2 }{ e }^{ x }dx } ={ x }^{ 2 }{ e }^{ x }-\int { { e }^{ x }2xdx } +C \tag{4.2.3}\] \[Let\quad \begin{cases} u=2x \\ v'={ e }^{ x }dx \end{cases}\Rightarrow \begin{cases} u'=2dx \\ v={ e }^{ x } \end{cases} \tag{4.2.4}\] \[\int { { x }^{ 2 }{ e }^{ x }dx } ={ x }^{ 2 }{ e }^{ x }-\left[ 2x{ e }^{ x }-\int { { e }^{ x }2dx } \right] +C \tag{4.2.5}\] \[\int { { x }^{ 2 }{ e }^{ x }dx } ={ x }^{ 2 }{ e }^{ x }-2x{ e }^{ x }+2{ e }^{ x }+C \tag{4.2.6}\] \[\int { { x }^{ 2 }{ e }^{ x }dx } ={ e }^{ x }\left( { x }^{ 2 }-2x+2 \right) +C \tag{4.2.7}\]