10 | APPLICATIONS OF THE DERIVATIVE AND NUMERICAL DIFFERENTIATION

This chapter of Single Variable Calculus by Dr JH Klopper is licensed under an Attribution-NonCommercial-NoDerivatives 4.0 International Licence available at http://creativecommons.org/licenses/by-nc-nd/4.0/?ref=chooser-v1 .

10.1 Introduction

There are many uses and applications for the derivative. This is especially true in the field of health data science, where many of the statistical and machine learning techniques that we use requiring differentiation.
In this chapter we begin with a numerical method for finding the roots of a function. While a typical course in single variable calculus develops techniques for calculating roots exactly, we must sometimes rely on numerical methods.
In the rest of the chapter we explore the use of the derivative in optimization problems. Here, we try and calculate values for an unknown so that we can maximize a specified value. Optimization is used to solve real-world problems.
We also explore how to find the maximum or minimum of a function using the derivative. Deep neural networks use this method to learn from data.
At the end of the chapter we revisit the equation for the derivative and develop numerical methods for calculating a derivative.

10.2 Newton’s method of finding roots

y-
y
0
=m(x-
x
0
)​​y=m(x-
x
0
)+
y
0
(
1
)
The slope
m
is simply the derivative of the function
yf(x)
and is
m
′
f
(x)
. At the point
x
x
0
we have
y
0
f(
x
0
)
and the slope
′
f
(
x
0
)
. We substitute these values into (1) in (2), the equation for the tangent to the function
f
at
(
x
0
,
y
0
)
.
y=f'(
x
0
)(x-
x
0
)+f(
x
0
)
(
2
)
The
x
intercept of the tangent line in (2) is at
y0
. If we let this point be
x
x
1
, we can solve for it as shown in (3).
0=f'(
x
0
)(
x
1
-
x
0
)+f(
x
0
)​​0=f'(
x
0
)
x
1
-f'(
x
0
)
x
0
+f(
x
0
)​​f'(
x
0
)
x
1
=f'(
x
0
)
x
0
-f(
x
0
)​​
x
1
=
x
0
-
f(
x
0
)
f'(
x
0
)
(
3
)
Consider the function
f(x)-
3
(x-2)
+2
shown in Figure 10.2.1. The goal is to find the root. While we could do this analytically, our aim here is to develop a numerical method.
Plot[-
3
(x-2)
+2,{x,-1,4},PlotLabel->"Figure 10.2.1",AxesLabel->{"x","y"},GridLines->Automatic,ImageSize->Large]
Out[]=
It seems at is the root is near
x3
. We can calculate the value of
f
at
x3
and also the slope of the tangent line at
x3
, both of which are required for (3). We create a user-defined function f to hold the function
f
.
In[]:=
f[x_]:=-
3
(x-2)
+2
The value of
yf(3)
where
x
0
is calculated first.
f[3](*Valueoffatx=3*)
Out[]=
1
We have that
x
0
3
and
f(
x
0
)1
. Now we need to calculate the value of the first derivative with respect to
x
at
x
0
3
.
D[f[x],x]/.x->3(*Valueofthederivativeoffatx=3*)
Out[]=
-3
The slope is
-3
. We now have all the values to substitute into (3), which is done in (4).
x
1
=
x
0
-
f(
x
0
)
f'(
x
0
)
​​
x
1
=3-
1
-3
​​
x
1
=3+
1
3
​​
x
1
=
10
3
≈3.333
(
4
)
We calculate the equation for the tangent line at
x3
in (5) such that we can graph it in Figure 10.2.2, in which we zoom in to a smaller interval.
y
1
-
y
0
=m(x-
x
0
)​​
y
1
=-3(x-3)+1​​
y
1
=-3x+9+1​​
y
1
=-3x+10
(
5
)
ShowPlot{-
3
(x-2)
+2,-3x+10},x,
5
2
,
7
2
,PlotLegends->"Expressions",PlotLabel->"Figure 10.2.2",AxesLabel->{"x","y"},GridLines->Automatic,ImageSize->Large,ListPlot[{{3,1}}]
Out[]=
-
3
(x-2)
+2
-3x+10
Solving (5) for
x
when
y0
shows the value
x
1
that we calculated in (4). This is the point where the (orange) tangent line intersects with the
x
axis in Figure 10.2.2. This is not too far from the root of
f
. We can iterate this process and now start at
x
1

10
3
. Using (3), this will allow us to calculate
x
2
. Below, we do this by calculating all the required values for (3).
In[]:=
f
10
3
(*Valueoffatx=
10
3
*)
Out[]=
-
10
27
In[]:=
D[f[x],x]/.x->
10
3
Out[]=
-
16
3
In (6), we calculate
x
2
from the values above.
x
2
=
x
1
-
f(
x
1
)
f'(
x
1
)
​​
x
2
=
10
3
-
-
10
27
-
16
3
​​
x
2
=
235
72
≈3.264
(
6
)
In (7) we calculate the equation for the tangent line to
f
at
x
10
3
and graph the tangent line in Figure 10.2.3. The point of intersection of the tangent line with the
x
axis is now even closer to the root of
f
.
y
2
-
y
1
=m(x-
x
1
)​​
y
2
=-
16
3
x-
10
3
-
10
27
​​
y
2
=-
16
3
x+
160
9
-
10
27
​​
y
2
=-
16
3
x+
470
27
(
7
)
ShowPlot-
3
(x-2)
+2,-
16
3
x+
470
27
,x,3,
7
2
,PlotLegends->"Expressions",PlotLabel->"Figure 10.2.3",AxesLabel->{"x","y"},GridLines->Automatic,ImageSize->Large,ListPlot
10
3
,-
10
27

Out[]=
-
3
(x-2)
+2
1
3
(-16)x+
470
27
This is very close to the approximate root calculated below using the Solve function.
​
The function is a second-degree polynomial and we expect two roots. Figure 10.2.4 visualizes the two roots.
We can use our user-defined function to calculate a root.
To confirm our results, we use the Roots function.
​
Newton’s method can be used for functions that are not polynomials.
​
In this problem we cannot use the Roots function as it only take polynomials as parameter.
​

10.3 Optimization

​
We create a user-defined function v below to represent the volume of the cylinder.
​

10.4 Optimization using numerical methods

It is important to note that the minimum that we calculate with gradient descent might only be a local minimum and not a global minimum.
In the case of finding a maximum, we use the iterative process in (13).
In the code cell below we create a user-defined function gradientDescentSingle to perform the iterative process of gradient descent. The parameters are the function, the unknown variable, the learning rate, and the maximum number of iterations.

10.5 Numerical differentiation

10.5.1 Definition of the derivate of a single variable function

Not all derivatives of single variable functions can be solved analytically. Instead, we use the definition of the derivative to calculate a numerical approximation.

10.5.2 Numerical differentiation

The values that we calculated for the derivative are indeed on the curve of the derivative itself.
Figure 10.5.2.8 visualizes the function and its derivative using the definition shown in the first line of (17).
​
Figure 10.5.2.9 shows the solution.
​