In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import sympy as sy
Smaller and Smaller Intervals¶
GOALS:
- Understand a definition of the derivative
- Interpret derivative as a function
- Interpret derivative as slope of tangent line
In the last notebook, we were investigating the connection between a sequence of values and smaller and smaller intervals for differences between these terms. Now, we will investigate some formal definitions for this operation of successively smaller differences as a Derivative. The big idea is that we have a function \(f\), we can define it’s derivative as:
We can interpret the derivative in a few ways. As we started looking at the difference between terms, we can consider the derivative as a measure of the rate of change of some sequence of numbers.
Similarly, if we have a closed form sequence rather than a sequence we can regard the derivative as a function that represents the rate of change of our original function.
Derivative as Function with Python¶
First, we can investigate the derivative of a function using Sympy’s
diff
function. If we enter a symbolic expression \(f\) in terms
of some variable \(x\), we will be able to get the derivative of
this expression with sy.diff(f, x)
. A simple example follows.
In [2]:
x = sy.Symbol('x')
f = x**2 - 2*x + 1
sy.diff(f, x)
Out[2]:
2*x - 2
In [3]:
sy.diff(f, x, 2)
Out[3]:
2
Similarly, we can define a function that is a good approximation for the
derivative by assigning a very small change \(dx\). For example, we
define the function above in our familiar fashion, and use the
definition above for a derivative function df
.
In [4]:
def f(x):
return x**2 - 2*x + 1
In [5]:
def df(x):
h = 0.000001
return (f(x + h) - f(x))/h
In [6]:
x = np.linspace(-4, 4, 1000)
plt.plot(x, f(x), label = '$f(x)$')
plt.plot(x, df(x), label = '$f\'(x)$')
plt.axhline(color = 'black')
plt.axvline(color = 'black')
plt.legend(loc = 'best', frameon = False)
plt.title("Function and its Derivative")
Out[6]:
<matplotlib.text.Text at 0x117b7aef0>
In [7]:
def f(x):
return (x -2)*(x -1) * (x + 1)*(x + 3)*(x +5)
In [8]:
x = np.linspace(-5, 2, 1000)
plt.plot(x, f(x), label = '$f(x)$')
plt.plot(x, df(x), label = '$f\'(x)$')
plt.axhline(color = 'black')
plt.axvline(color = 'black')
plt.legend(frameon = False)
plt.title("Another function and its Derivative")
Out[8]:
<matplotlib.text.Text at 0x118135828>
We can glean important information from the plot of the derivative about the behavior of the function we are investigating, particularly the maximum and minimum values. Perhaps we would only have the plot of the derivative, can you tell where the max and min values occur?
In [9]:
x = np.linspace(-5, 2, 1000)
plt.plot(x, df(x), label = '$f\'(x)$')
plt.axhline(color = 'black')
plt.axvline(color = 'black')
plt.legend(frameon = False)
plt.title("Just the Derivative")
Out[9]:
<matplotlib.text.Text at 0x1182b8e10>
In [10]:
x = sy.Symbol('x')
df = sy.diff(f(x), x)
df = sy.expand(df)
df
Out[10]:
5*x**4 + 24*x**3 - 6*x**2 - 72*x + 1
Derivative as Tangent Line¶
We can also interpret the derivative as the slope of a tangent line, or better yet an approximation for this tangent line. For example, the derivative at some point \(a\) can be thought of as the slope of the line through \(a\) and some other point \(a + \Delta x\) where \(\Delta x\) is very small.
Again, we would have something like:
for some arbitrarily small value of \(\Delta x\). We can also understand the expression above as providing us a means of approximating values close to \(x = a\) using the tangent line. If we rearrange terms, notice:
What this does, is tells us the slope of the line tangent to the graph at the point \((a, f(a))\). Suppose we have the function \(f(x) = x^2\), and we want to know the slope of the tangent line at \(x = 2\). We can define a function as usual, and a function for the derivative. We can use these to write an equation of the line tangent to the graph of the function at this point.
In [11]:
def f(x):
return x**2
def df(x):
h = 0.00001
return (f(x + h) - f(x))/h
In [12]:
df(2)
Out[12]:
4.000010000027032
In [15]:
def tan_plot(a):
x = np.linspace((a-4), (a+4), 1000)
y = df(a)*(x - a) + f(a)
plt.plot(x, f(x))
plt.plot(a, f(a), 'o', markersize = 10)
plt.plot(x, y, '--k')
plt.axhline(color = 'black')
plt.axvline(color = 'black')
In [16]:
tan_plot(2)
In [17]:
def g(x):
return x*(x+2)*(x - 3)
def dg(x):
h = 0.000001
return ((g(x+h)-g(x))/h)
In [18]:
x = np.linspace(-3, 4, 1000)
plt.plot(x, g(x))
plt.axhline(color = 'black')
plt.axvline(color = 'black')
Out[18]:
<matplotlib.lines.Line2D at 0x1189c43c8>
In [19]:
x = sy.Symbol('x')
df = sy.diff(g(x), x)
df = sy.simplify(df)
a, b = sy.solve(df, x)
In [20]:
a, b
Out[20]:
(1/3 + sqrt(19)/3, -sqrt(19)/3 + 1/3)
In [21]:
x = np.linspace(-3, 4, 1000)
plt.plot(x, g(x))
plt.axhline(color = 'black')
plt.axvline(color = 'black')
plt.plot(a, g(a), 'o')
plt.plot(b, g(b), 'o')
Out[21]:
[<matplotlib.lines.Line2D at 0x118a7fe10>]
In [22]:
def tan_plot(a, b):
x = np.linspace(-5,5, 1000)
y1 = dg(a)*(x - a) + g(a)
y2 = dg(b)*(x - b) + g(b)
plt.plot(x, g(x))
plt.plot(a, g(a), 'o', markersize = 10)
plt.plot(b, g(b), 'o', markersize = 10)
plt.plot(x, y1, '--k')
plt.plot(x, y2, '--k')
plt.ylim(-15, 15)
plt.xlim(-4,4)
plt.axhline(color = 'black')
plt.axvline(color = 'black')
In [23]:
tan_plot(a, b)