import matplotlib.pyplot as plt
import numpy as np
import sympy as sy
def f(x): return x + 1
def g(x): return x**2 + 1
x = np.linspace(-2,2,100)
plt.plot(x,f(x),label='f')
plt.plot(x,g(x),label='g')
plt.plot(0,f(0),'ro')
plt.plot(1,f(1),'ro')
plt.legend()
x2 = np.linspace(1,1.1)
plt.plot(x2,f(x2),label='f')
plt.plot(x2,g(x2),label='g')
plt.legend()
plt.title('Zooming in on \nNow they both look Linear!')
x2 = np.linspace(1,1.001,100)
plt.plot(x2,f(x2),label='f')
plt.plot(x2,g(x2),label='g')
plt.legend()
plt.title('Zooming in on \nNow they both look Linear!')
Definition
Note: h is the distance, gets smaller and smaller so we can get the smalles possible variation of x (slope)
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.
First, we can investigate the derivative of a function using Sympy's diff
function. If we enter a symbolic expression in terms of some variable , we will be able to get the derivative of this expression with sy.diff(f, x)
. A simple example follows.
def f(x):
return x**2 - 2*x + 1
def df(x):
h = 0.000001
return (f(x + h) - f(x))/h
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")
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")
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?
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")
x = sy.Symbol('x')
df = sy.diff(f(x), x)
df = sy.expand(df)
df
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 can be thought of as the slope of the line through and some other point where is very small.
Again, we would have something like:
for some arbitrarily small value of . We can also understand the expression above as providing us a means of approximating values close to 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 . Suppose we have the function , and we want to know the slope of the tangent line at . 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.
def f(x):
return x**2
def df(x):
h = 0.00001
return (f(x + h) - f(x))/h
df(2)
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')
tan_plot(2)
def g(x):
return x*(x+2)*(x - 3)
def dg(x):
h = 0.000001
return ((g(x+h)-g(x))/h)
x = np.linspace(-3, 4, 1000)
plt.plot(x, g(x))
plt.axhline(color = 'black')
plt.axvline(color = 'black')
x = sy.Symbol('x')
df = sy.diff(g(x), x)
df = sy.simplify(df)
a, b = sy.solve(df, x)
a, b
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')
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')
tan_plot(a, b)