Numerical Integration and Differentiation

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.

The derivative of a function is defined as:

\[ f^{\prime}(x) = \lim_{x \to 0}\frac{f(x + h) - f(x)}{h} \]

Create the function for \(f(x)\)

fx <- function(x) {
  return (x^3 + 2*x^2)
}

Apply the function fx to the derivative formula. An example would look like:

(fx(2 + 0.00000001) - fx(2))/ 0.00000001 

# where h = 0.00000001

Create the R function to calculate the derivative of fx for any given x value using limits.

## Parameters
# ---------------------------------------
#  1. eq = function to be applied to x
#  2. x  = input value to function
#  3. h  = limit value approaching 0

deriv_lim <- function(eq, x, h) {
  
  f_xh <- eq(x + h)  # f(x + h)
  f_x <- eq(x)       # f(x)

  return ((f_xh - f_x) / h)
}

# create a function to calculate the actual derivative value (analytical form)

f <- function(x) (x^3 + 2*x^2)
deriv_f <- function(x) {}

body(deriv_f) <- D(body(f), 'x')

# derivative of f(x) = x^3 + 2*x^2
body(deriv_f) 
## 3 * x^2 + 2 * (2 * x)
# example : calculate derivative of x for 1 : 3
deriv_f(1:3)
## [1]  7 20 39

Calculate derivative of f(x) using limits vs. analytical for x = (1:20)

n <- 1:20

results = data.frame(x = n,
                     deriv_limit = deriv_lim(fx, n, 0.00000001),
                     deriv_calc  = deriv_f(n))

Results:

##   x deriv_limit deriv_calc
##   1      7.0000          7
##   2     20.0000         20
##   3     39.0000         39
##   4     64.0000         64
##   5     95.0000         95
##   6    132.0000        132
##   7    175.0000        175
##   8    224.0000        224
##   9    279.0000        279
##  10    340.0000        340
##  11    407.0000        407
##  12    480.0001        480
##  13    559.0000        559
##  14    644.0001        644
##  15    735.0000        735
##  16    832.0001        832
##  17    935.0001        935
##  18   1044.0001       1044
##  19   1159.0001       1159
##  20   1280.0001       1280

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.

f2 <- function(x) {
  return (3*x^2 + 4*x)
}

# create the range of small intervals using a delta of 1e-6
intervals <- seq(from=1, to=3, by=1e-6)

head(intervals, 10)
##  [1] 1.000000 1.000001 1.000002 1.000003 1.000004 1.000005 1.000006
##  [8] 1.000007 1.000008 1.000009

Number of intervals = 2000001

area.curve <- function(func, x, delta) {

  # calculate the heights of each interval
  height <- func(x)
  # width will be the delta value
  width  <- delta

  return (sum( height * width))
  
}

(area_approx <- area.curve(func=f2, x=intervals, delta=1e-6))
## [1] 42.00002

Let’s compare this value calculated by R using the integrate function.

(area_R <- integrate(f2, 1, 3))
## 42 with absolute error < 4.7e-13

R Integrate function = 42
Approximataion function = 42.000023


Integration by Parts

Let: \[u = f(x) \] \[du = f^{\prime}(x) dx\] \[dv = g^{\prime}(x) dx\] \[v = g(x) \]

Then the Integration by Parts formula becomes:

\[ \int{u dv} = uv - \int{v du} \]

Problem 1:

Use integration by parts to solve for \(\int{sin(x)cos(x)}dx\)

For this problem, set the following:

\(u = -cos(x)\)
\(du = sin(x)dx\)

\(dv = -sin(x)dx\)
\(v = cos(x)\)

Apply the Integration by Parts formula:

\[\int{sin(x)cos(x)}dx = -cos(x) cos(x) - \int{ -cos(x) -sin(x) dx }\]

re-write to:

\[\int{sin(x)cos(x)}dx = -cos(x) cos(x) - \int{cos(x) sin(x) dx }\]

adding \(\int{cos(x) sin(x) dx}\) to LHS we get:

\[ 2 * \int{cos(x) sin(x) dx} = -cos^2(x) \]

Finally

\[\int{cos(x) sin(x) dx} = -\frac{cos^2(x)}{2} + C \]

Problem 2

Use integration by parts to solve for \(\int{x^2 e^x}\)

For this problem, set the following:

\(u = x^2\)
\(du = 2xdx\)

\(dv = e^xdx\)
\(v = e^x\)

u = x^2 v = e^x du = 2xdx dv = e^xdx

\[\int{x^2 e^x} = x^2 e^x - \int{e^x 2xdx}\]

\[ = x^2 e^x - 2 \int{x e^x dx}\]

Calculate second anti-derivative for

\[ \int{x e^x dx}\]

let:

\(u = x\)
\(du = 1\)
\(v = e^x\)
\(dv = e^x\)

\[ = x e^x - \int{ 1 e^x dx} = x e^x - e^x \]

Putting it all together:

\[ = x^2 e^x - 2(x e^x - e^x) + C = x^2 e^x -2xe^x + 2e^x + C\]

Problem 3

What is \(\frac{d}{dx} (x cos(x))\)?

To solve use the Product Rule which states that:

\[(fg)^{\prime} = f^{\prime} * g + f * g^{\prime} \]

\(f = x\) \(f^{\prime} = 1\)

\(g = cos(x)\) \(g^{\prime} = -sin(x)\)

\(f^{\prime} * g + f * g^{\prime} = 1*cos(x) + x(-sin(x)) = cos(x) - x(sin(x))\)

Problem 4

What is \(\frac{d}{dx}(e^x)^4\)?

Use the Power Rule and regroup: \(\frac{d}{dx}(e^4x)\)

Next apply the Chain Rule where u = 4x

\[(e^4x) * \frac{d}{dx}4x = 4 \cdot \frac{d}{dx}x \cdot (e^4x)\]

\[ = 4 \cdot 1(e^4x) = 4e^4x\]