|IS 605 FUNDAMENTALS OF COMPUTATIONAL MATHEMATICS - WEEK 13 | Data Analytics
Question 1:
Write a program to compute the derivative of f(x) = \({ x }^{ 3 }+\quad { 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.
options(warn = -1)
suppressMessages(library(mosaic))
diff <- function(x, h=1e-6){
funcNeg <- (x-h)^3 + 2*(x-h)^2
funcPos <- (x+h)^3 + 2*(x+h)^2
func <- x^3 + 2*x^2
upper_diff <- (funcPos - func)/h
lower_diff <- (funcNeg - func)/-h
diff_X <- (lower_diff + upper_diff)/2
return(diff_X)
}
diff(2)## [1] 20
Checking
funct <- D((x^3 + 2*x^2)~x)
funct(2)## [1] 20
Question 2:
Now, write a program to compute the area under the curve for the function \({ 3x }^{ 2 }+\quad { 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.
AUC <- function(){
a <- 1
b <- 3
h <- 1e-6
intervals <- (b-a)/h
area <- 0
for(i in 1:intervals){
area <- area + (3*a^2 + 4*a)*h
a <- a + h
}
return(area)
}
AUC()## [1] 41.99998
Checking
integrate2 <- function(x)(3*x^2 + 4*x)
integrate(integrate2, lower=1, upper=3)## 42 with absolute error < 4.7e-13
Question a:
Solution:
we shall make us of \(\int { f(g(x).g\prime (x)dx=\int { f(u)du,\quad u=g(x) } }\)
\(u=sin(x),\quad du=cos(x)dx\\ \frac { du }{ dx } \quad =\quad cos(x),\quad du=cos(x)dx,\quad dx=\frac { 1 }{ cos(x) } du\)
\(\int { ucos(x).\frac { 1 }{ cosx } } \quad =\quad \int { udu }\)
Recall that,
\(\int { { x }^{ a } } dx\quad =\quad \frac { { x }^{ a+1 } }{ a+1 } \quad =\quad \frac { { u }^{ 1+1 } }{ 1+1 } ,\quad since\quad u\quad =\quad sin(x)\)
\(\frac { { sin(x) }^{ 2 } }{ 2 } +C\)
Question b:
Solution:
Recall from Intergration by part, that;
\(\int { { uv }^{ \prime } } =\quad uv\quad -\int { { vu }^{ \prime } }\)
where,
\(u\quad ={ x }^{ 2 },\quad { u }^{ \prime }=\quad 2x,\quad { v }^{ \prime }={ e }^{ x },\quad v={ e }^{ x }\)
\(=\quad { x }^{ 2 }{ e }^{ x }\quad -\quad \int { 2x{ e }^{ x } } dx\)
note that \(\quad \int { 2x{ e }^{ x } } dx\) = \(2( { e }^{ x }x\quad -\quad { e }^{ x })\)
Therefore,
\(\int { { x }^{ 2 }{ e }^{ x } } dx\) = \(\quad { x }^{ 2 }{ e }^{ x }\quad\) - \(2( { e }^{ x }x\quad -\quad { e }^{ x })\) + C
Question C:
solution:
Recall from Product Rule,
\((f.g)\prime =f\prime .g\quad +f.g\prime \\ d(\frac { d }{ dx } (x)cos(x)x\quad +\quad \frac { d }{ dx } (cos(x)(x)(x)),\quad where,\\ =\quad -sin(x)(x)\quad +\quad 1.cos(x)\\ =-xsin(x)\quad +\quad cos(x)\)
Question d:
Solution;
\(\frac { df(u) }{ dx } =\frac { df }{ du } .\frac { du }{ dx } ,\quad let\quad u={ x }^{ 4 }\\ =\quad \frac { { d(e }^{ u }) }{ du } \frac { { d(x }^{ 4 }) }{ dx } ,\quad \frac { d }{ du } { (e }^{ u })\quad ={ e }^{ u },\quad \frac { d }{ du } { (x }^{ 4 })\quad ={ 4x }^{ 3 }\\ =\quad { e }^{ u }.{ 4x }^{ 3 },\quad but\quad u={ x }^{ 4 }\\ =\quad { e }^{ { x }^{ 4 } }{ 4x }^{ 3 }\)