In the last lesson, we saw that the derivative was the rate of change, and that the derivative of a straight line is a constant. Let's explore non-linear functions and their derivatives in this lesson!
You will be able to:
- Understand that derivatives are the instantaneous rate of change of a function
- Understand how to calculate a derivative
- Understand how to express taking a derivative at a given point, and evaluating a function at a given point mathematically
Let's have another look at our the plot you saw previously:
import matplotlib.pyplot as plt
% matplotlib inline
import numpy as np
def jog(miles):
return 6*miles
fig, ax = plt.subplots(figsize=(7.5,5.5))
x = np.linspace(0, 3.5, 100)
plt.plot(x, jog(x), label = "distance given # hours")
plt.hlines(y=6, xmin=0, xmax=1, linestyle = "dashed", color= 'lightgrey')
plt.vlines(x=1, ymin=0, ymax=6, linestyle = "dashed", color= 'lightgrey')
plt.hlines(y=12, xmin=0, xmax=2, linestyle = "dashed", color= 'lightgrey')
plt.vlines(x=2, ymin=0, ymax=12, linestyle = "dashed", color= 'lightgrey')
plt.vlines(x=2, ymin=6, ymax=12, color="darkorange", label = "y2 - y1 = 12 - 6 = 6")
plt.hlines(y=6, xmin=1, xmax=2, color="lightgreen", label = "x2 - x1 = 2 - 1 = 1")
ax.legend(loc='upper left', fontsize='large')
plt.ylabel("distance in miles")
plt.xlabel("number of hours")
plt.show()
Where our function was given by
- Essentially, the derivative is the rate of change of a function
- Graphically this is rise over run
- Which can be calculated by taking two points,
$(x_1, y_1)$ and$(x_2, y_2)$ and calculating$\frac{y_2 - y_1}{x_2 - x_1}$
Finally, we said that when we have a function
So we saw previously that the derivative is the rate of change of our function. We express this as $ f'(x) = \frac{\Delta f}{\Delta x}$. So far we have only calculated the derivatives with linear functions. As we'll see, things becomes trickier when working with more complicated functions.
For example, let's imagine that we are coaching our runner to perform in a track meet.
We may want to know how well our track start does at one part of the race, say the starting point, versus another point later in the race. Then we will know what to focus on in practice. We can imagine the distance travelled by our track star's distance through time as represented by the function
def f(x):
return np.square(x)
fig, ax = plt.subplots(figsize=(7.5,5.5))
x = np.linspace(0, 7, 100)
plt.plot(x, f(x))
plt.ylabel("distance in feet")
plt.xlabel("number of seconds")
plt.show()
The graph shows that from seconds zero through seven, our track runner gets faster over time.
Now if we want to see how quickly our track star at the 2nd second as opposed to some other second, what would we do? Well even if we knew nothing about derivatives, we would likely get a stop watch and at second 2 would use it to calculate the speed. Let's say that we start our stopwatch at second 2 and stop our stopwatch one second later.
def f(x):
return np.square(x)
fig, ax = plt.subplots(figsize=(7.5,5.5))
x = np.linspace(0, 4, 100)
plt.plot(x, f(x))
plt.hlines(y=9, xmin=0, xmax=3, linestyle = "dashed", color= 'lightgrey')
plt.vlines(x=2, ymin=0, ymax=4, linestyle = "dashed", color= 'lightgrey')
plt.hlines(y=4, xmin=0, xmax=2, linestyle = "dashed", color= 'lightgrey')
plt.vlines(x=3, ymin=0, ymax=9, linestyle = "dashed", color= 'lightgrey')
plt.vlines(x=3, ymin=4, ymax=9, color="darkorange", label = "y2 - y1 = 9 - 4 = 5")
plt.hlines(y=4, xmin=2, xmax=3, color="lightgreen", label = "x2 - x1 = 3 - 2 = 1")
# tangent line
x_dev = np.linspace(1.5, 3.5, 100)
a = 2
delta_a = 1
fprime = (f(a+delta_a)-f(a))/delta_a
tan = f(a)+fprime*(x_dev-a)
# plot of the function and the tangent
plt.plot(x_dev, tan, color = "black", linestyle="dashed")
ax.legend(loc='upper left', fontsize='large')
plt.ylabel("distance in feet")
plt.xlabel("number of seconds")
plt.show()
As the graph above shows, we measure the change at second two by starting our stopwatch at second 2 and stopping it one second later. So turning this into our formula for calculating a derivative of:
we do the following:
- Set
$x = 2$ , as that's the point we want to calculate the rate of change at - Set
$\Delta x = 1$ , as that's the number of seconds that elapsed on our stopwatch
and plugging in these values, we have:
So our rate of change at second number 2, with a
Simplifying our calculation of
-
$f(3) = (3)^2 = 9$ is the output at$x = 3$ and -
$f(2) = (2)^2 = 4$ is the output at$x = 2$ so
Let's take another close look at the straight line in the graph above. That straight line is a supposed to be the rate of change of the function at the point
Here is the problem:
- in our formula of $ f'(x) = \dfrac{f(x + \Delta x) - f(x)}{\Delta x} $, we are seeing the rate of change not just where
$x = 2$ , but from the period from$x = 2$ to$x = 3$ . Recall that the derivative is supposed to be the instantaneous rate of change! However, it seems that this interval does not reflect the instantaneous nature!
In other words, the runner would tell us that we are not capturing their speed at precisely second two:
This is because in between the clicks of our stopwatch from seconds two to three, our runner is getting faster and while we are supposed to be calculating his speed just at second two, our calculation includes his increase in speed from seconds two to three.
Therefore, the black dashed has a larger rate of change than the blue line because we have included this increase in speed at second three.
A mathematician would make the same point that we are not actually calculating the derivative:
Our derivative means we are calculating how fast a function is changing at any given moment, and precisely at that moment. And unlike in where our functions were linear, here the rate of change of our function is always changing. The larger our value of
$\Delta x$ , the less our derivative reflects the rate of change at just that point.
If you were holding a stopwatch and someone asked you to calculate their speed at second number 2, how could you be more accurate? Well, you would want decrease the change in seconds. Of course, our runner could continue to protest and say that we are still influenced by the speed at other times.
However, the mathematician has a solution to this. To calculate the rate of change at precisely one point, the solution is to use our imagination. We calculate the derivative with a $\Delta $ of 1, then calculate it again with a
** The derivative of a function is a change in the function's output across
$\Delta x$ , as $\Delta x $ approaches zero **.
In this example, by decreasing
$ \Delta x $ | $ \frac{\Delta y}{\Delta x} $ |
---|---|
1 | 5 |
.1 | 4.1 |
.01 | 4.01 |
.001 | 4.001 |
Another way to see how we approach the derivative is by seeing how a line becomes more tangent to the curve as
Tangent to the curve means that our line is just touching the curve.
The more that a line is tangent to the curve at a point, the more it's slope matches the derivative.
Ok, let's get a sense of what we mean by tangent to the curve. The black dashed line below is a line whose slope is calculated by using our derivative function, with $ \Delta x = 1$. As you can see it is **not tangent to our function,
def make_plot(delta_a):
lab= "delta x = " + str(delta_a)
plt.plot(x, f(x), label = lab)
plt.hlines(y=9, xmin=1, xmax=3, linestyle = "dashed", color= 'lightgrey')
plt.vlines(x=2, ymin=1, ymax=4, linestyle = "dashed", color= 'lightgrey')
plt.hlines(y=4, xmin=1, xmax=2, linestyle = "dashed", color= 'lightgrey')
plt.vlines(x=3, ymin=1, ymax=9, linestyle = "dashed", color= 'lightgrey')
# tangent line
x_dev = np.linspace(1.5, 3.2, 100)
a = 2
fprime = (f(a+delta_a)-f(a))/delta_a
tan = f(a)+fprime*(x_dev-a)
# plot of the function and the tangent
plt.plot(x_dev, tan, color = "black", linestyle="dashed")
plt.legend(loc="upper left", bbox_to_anchor=[0, 1],
ncol=2, fancybox=True);
return
x = np.linspace(1, 3.5, 100)
fig, ax = plt.subplots(figsize=(6,4))
make_plot(1)
If our black dashed line had the same slope, or rate of change, as our function at that
Let's look again using a smaller
Below are the plots of our lines using our derivative formula for when
x = np.linspace(1, 3.5, 100)
fig, ax = plt.subplots(figsize=(10,8))
# create 4 subplots
plt.subplot(221)
make_plot(delta_a = 1)
plt.subplot(222)
make_plot(delta_a = 0.1)
plt.subplot(223)
make_plot(delta_a = 0.01)
plt.subplot(224)
make_plot(delta_a = 0.001)
Going to the top left figure to the bottom right figure, you can see that for a smaller
As you can see, as $\Delta x $ approaches zero, $f'(2) $ approaches $ 4 $.
So to describe the above, at the point
When
Or, better yet, we can update and correct our definition of derivative to be:
So the derivative is the change in output as we just nudge our input. That is how we calculate instantaneous rate of change. We can determine the runners speed at precisely second number 2, by calculating the runner's speed over shorter and shorter periods of time, to see what that number approaches.
One final definition before we go. Instead of
Above is the formula for the derivative for all types of functions linear and nonlinear.
In this section, we learned about derivatives. A derivative is the instantaneous rate of change of a function. To calculate the instantaneous rate of change of a function, we see the value that $\dfrac{\Delta y}{\Delta x} $ approaches as $\Delta x $ approaches zero. This way, we are not calculating the rate of change of a function across a given distance. Instead we are finding the rate of change at a specific moment.