In my previous article I talked a little bit about optimization. Where we took a random function, and brute forced our computer to iterate through a range of values to find the maximum and minimum result of that function. Now, differentiation actually has a part in optimization, which we were not using in that article.

Defining the derivative of a function is quite simple.

The Power Rule

Let’s say we have defined a function:

\[f(x) = x^2\]

What could be the \(f{\prime}(x) = x^2\)?

Well, the power rule states that the derivative of a powered item in a function would be two times that item and powered by the original power subtracted by 1.

\[\begin{align*} f(x) &= x^n \\ f{\prime}(x) &= nx^{n-1} \end{align*}\]

So, the derivative of our function would be,

\[\begin{align*} f{\prime}(x) &= nx^{n-1} \\ f{\prime}(x) &= 2x^{2 - 1} \\ f{\prime}(x) &= 2x^{1} \\ f{\prime}(x) &= 2x \end{align*}\]

What if we have \(f(x) = x^3\)?
The derivative would be,

\[\begin{align*} f{\prime}(x) &= nx^{n-1} \\ f{\prime}(x) &= 3x^{3 - 1} \\ f{\prime}(x) &= 3x^{2} \end{align*}\]

Definition Formula of Derivatives

The definition formula of a derivative is known as:

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

This formula is used to find the derivative of a function. The limit expression is used to keep the value of \(h\) in the formula, as we will need it until we need to substitute \(h\) with something. And when calling \(f(x + h)\), that means we need to add \(h\) to every \(x\) in the function. Let’s try an example. How about \(f(x) = 2x + 1\).

\[f(x) = 2x + 1\]

\[\begin{align*} f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{f(x + h) - f(x)}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{2(x + h) + 1 - (2x + 1)}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{2x + 2h + 1 -2x -1}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{{\cancel{2x}} + 2h + 1 {\cancel{-2x}} -1}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{{\cancel{2x}} + 2h + {\cancel{1}} {\cancel{-2x}} {\cancel{-1}}}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{2h}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{2{\cancel{h}}}{{\cancel{h}}} \\ f{\prime}(x) &= 2 \\ \end{align*}\]

That’s it. The derivative of \(f(x) = 2x + 1\) is just written as \(f{\prime}(x) = 2\).
That’s the gist of it.
Maybe we can use this formula to prove the power rule we talked about before. Let’s try the function

\[f(x) = x^2\]

\[\begin{align*} f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{f(x + h) - f(x)}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{(x + h)^2 - (x^2)}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{(x + h)(x + h) - (x^2)}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{x^2 + xh + hx + h^2 - x^2}{h} \\ \end{align*}\]

Now we just cross out stuff that cancel each other out

\[\begin{align*} f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{{\cancel{x^2}} + xh + hx + h^2 {\cancel{- x^2}}}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} \frac{2xh + h^2}{h} \\ f{\prime}(x) &= \displaystyle \lim_{h \to 0} {2x + h} \\ \end{align*}\]

Here we substitute \(h\) with \(0\), and with that, we can leave the limit expression out

\[\begin{align*} f{\prime}(x) &= {2x + 0} \\ f{\prime}(x) &= {2x} \\ \end{align*}\]

And we just proved that the power rule is true.

R

In R, with mosaicCalc, you can define the derivative of a function with the D() function. To try this, let’s first define a function in an R environment.

f <- makeFun(x^2 ~ x)

This is the R function equivalent of the mathematical function \(f(x) = x^2\).
What would \(f(3)\) result in?

f(3)
## [1] 9

\[\begin{align*} f(x) &= {x^2} \\ f(3) &= {3^2} \\ f(3) &= {9} \\ \end{align*}\]

We can define the derivative of our new f() by:

ff <- D(f(x) ~ x)

PS: Starting from October 2023, you may need to specify which package D() comes from, because another D() already exists natively in R. In our case, we’re using mosaicCalc’s, so we will have to call it like mosaicCalc::D().

So, what would \(f{\prime}(3)\) result in?

ff(3)
## [1] 6

Answer is \(6\). But, is it, though? Let’s try to solve this manually. By the power rule, the derivative of the function would be:

\[f(x) = {x^2}\]

\[\begin{align*} f{\prime}(x) &= {2x} \\ f{\prime}(3) &= {2(3)} \\ f{\prime}(3) &= {6} \\ \end{align*}\]

Reference:

Kaplan, Daniel. 2022. MOSAIC Calculus. GitHub Pages. https://dtkaplan.github.io/MC2/


contra