In this lab, we will practice our knowledge of derivatives. Remember that our key formula for derivatives, is
- Learn how to represent linear and nonlinear functions in code
- Then, because our calculation of a derivative relies on seeing the output at an initial value and the output at that value plus
$\Delta x$ , we need anoutput_at
function - Then we will be able to code the
$\Delta f$ function that sees the change in output between the initial$x$ and that initial$x$ plus the$\Delta x$ - Finally, we will calculate the derivative at a given
$x$ value,derivative_at
You will be able to:
- Use python functions to demonstrate derivatives of functions
- Describe what a derivative means in the context of a real-world example
We are about to learn to take the derivative of a function in code. But before doing so, we need to learn how to express any kind of function in code. This way when we finally write our functions for calculating the derivative, we can use them with both linear and nonlinear functions.
For example, we want to write the function $f(x) = 2x^2 + 4x - 10 $ in a way that allows us to easily determine the exponent of each term.
This is our technique: write the formula as a numpy array. For example, for a function
arr = np.array([7, 3])
arr[0] # 7
arr[1] # 3
Take the following function as an example:
We can use a N-dimensional array to represent this:
import numpy as np
array_1 = np.array([[4, 2], [4, 1], [-10, 0]])
np.shape(array_1)
So each row in the np.array
represents a different term in the function. The first column is the term's constant and the second column is the term's exponent. Thus [4, 2]
and [-10, 0]
because
We'll refer to this
np.array
as "array of terms", orarray_of_terms
.
Ok, so give this a shot. Write $ f(x) = 4x^3 + 11x^2 $ as an array of terms. Assign it to the variable array_2
.
array_2 = None
Now that we can represent a function in code, let's write a Python function called term_output
that can evaluate what a single term equals at a value of
- For example, when
$x = 2$ , the term$3x^2 = 3*2^2 = 12 $ . - So we represent
$3x^2$ in code as(3, 2)
, and: -
term_output((3, 2), 2)
should return 12
def term_output(array, input_value):
pass
term_output(np.array([3, 2]), 2) # 12
Hint: To raise a number to an exponent in python, like 3^2 use the double star, as in:
3**2 # 9
Now write a function called output_at
, when passed an array_of_terms
and a value of
- For example, we'll use
output_at
to calculate$f(x) = 3x^2 - 11$ . - Then
output_at([np.array([[3, 2], [-11, 0]]), 2)
should return$f(2) = 3*2^2 - 11 = 1$ . Storenp.array([[3, 2], [-11, 0]])
asarray_3
.
def output_at(array_of_terms, x_value):
pass
array_3 = None
Verify that
# 1
What value does
# 16
Now we can use our output_at
function to display our function graphically. We simply declare a list of x_values
and then calculate output_at
for each of the x_values
.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(12,6))
x_values = np.linspace(-30, 30, 100)
y_values = list(map(lambda x: output_at(array_3, x), x_values))
plt.plot(x_values, y_values, label = "3x^2 - 11")
ax.legend(loc="upper center",fontsize='large')
plt.show()
Let's start with a function,
lin_function = None
We can plot the function by calculating outputs at a range of output_at
function to calculate the output at each individual
fig, ax = plt.subplots(figsize=(12,6))
x_values = np.linspace(0, 5, 100)
y_values = list(map(lambda x: output_at(lin_function, x), x_values))
plt.plot(x_values, y_values, label = "4x + 15")
ax.legend(loc="upper center",fontsize='large')
plt.show()
Ok, time to do what we are here for: derivatives. Remember that the derivative is the instantaneous rate of change of a function, and is expressed as:
We can see from the formula above that delta_f
that, given a list_of_terms
, an x_value
, and a value
Hint Don't forget about the
output_at
function. Theoutput_at
function takes a list of terms and an$x$ value and returns the corresponding output. So reallyoutput_at
is equivalent to$f(x)$ , provided a function and a value of x.
def delta_f(array_of_terms, x_value, delta_x):
return output_at(array_of_terms, x_value + delta_x) - output_at(array_of_terms, x_value)
delta_f(lin_function, 2, 1) # 4
So for
Let's show
x_value = None
delta_x = None
fig, ax = plt.subplots(figsize=(10,6))
x_values = np.linspace(0, 5, 100)
y_values = list(map(lambda x: output_at(lin_function, x), x_values))
plt.plot(x_values, y_values, label = "4x + 15")
# delta x
y_val = output_at(lin_function, x_value)
hline_lab= 'delta x = ' + str(delta_x)
plt.hlines(y=y_val, xmin= x_value, xmax= x_value + delta_x, color="lightgreen", label = hline_lab)
# delta f
y_val_max = output_at(lin_function, x_value + delta_x)
vline_lab = 'delta f = ' + str(y_val_max-y_val)
plt.vlines(x = x_value + delta_x , ymin= y_val, ymax=y_val_max, color="darkorange", label = vline_lab)
ax.legend(loc='upper left', fontsize='large')
plt.show()
Write a function, derivative_at
that calculates array_of_terms
, an x_value
for the value of delta_x
, which represents
Let's try this for
def derivative_of(array_of_terms, x_value, delta_x):
pass
Now let's use this function along with our stored x_value
and delta_x
.
# 4.0
Ok, now that we have written a Python function that allows us to plot our list of terms, we can write a function called tangent_line
that outputs the necessary terms to plot the slope of the function between initial
def tangent_line(array_of_terms, x_value, line_length = 4, delta_x = .01):
y = output_at(array_of_terms, x_value)
derivative_at = derivative_of(array_of_terms, x_value, delta_x)
x_dev = np.linspace(x_value - line_length/2, x_value + line_length/2, 50)
tan = y + derivative_at *(x_dev - x_value)
return {'x_dev':x_dev, 'tan':tan, 'lab': " f' (x) = " + str(derivative_at)}
Our
tangent_line
function takes as argumentslist_of_terms
,x_value
, which is where our line should be tangent to our function,line_length
as the length of our tangent line, anddelta_x
which is our$\Delta x$ .
The return value of
tangent_line
is a dictionary that represents the tangent line at that value of$x$ . It usesoutput_at()
to calculate the function value at a particular$x$ and thederivative_of()
function you wrote above to calculate the slope of the tangent line. Next, it usesline_length
along with thenp.linspace
to generate an array of x-values to be used as an input to generate the tangent linetan
.
Let's look at the output of the tangent_line()
, using our lin_function
, line_length
equal to 2.
tan_line = tangent_line(lin_function, 2, line_length = 2, delta_x = .1)
tan_line
Now, let's plot our function, rate_of_change
line.
fig, ax = plt.subplots(figsize=(10,6))
x_values = np.linspace(0, 5, 100)
y_values = list(map(lambda x: output_at(lin_function, x), x_values))
plt.plot(x_values, y_values, label = "4x + 15")
# tangent_line
plt.plot(tan_line['x_dev'], tan_line['tan'], color = "yellow", label = tan_line['lab'])
# delta x
y_val = output_at(lin_function, x_value)
hline_lab= 'delta x = ' + str(delta_x)
plt.hlines(y=y_val, xmin= x_value, xmax= x_value + delta_x, color="lightgreen", label = hline_lab)
# delta f
y_val_max = output_at(lin_function, x_value + delta_x)
vline_lab = 'delta f = ' + str(y_val_max-y_val)
plt.vlines(x = x_value + delta_x , ymin= y_val, ymax=y_val_max, color="darkorange", label = vline_lab)
ax.legend(loc='upper left', fontsize='large')
plt.show()
So that function highlights the rate of change is moving at precisely the point
fig, ax = plt.subplots(figsize=(10,4))
x_values = np.linspace(0, 5, 100)
function_values = list(map(lambda x: output_at(lin_function, x),x_values))
derivative_values = list(map(lambda x: derivative_of(lin_function, x, delta_x), x_values))
# plot 1
plt.subplot(121)
plt.plot(x_values, function_values, label = "f (x)")
plt.legend(loc="upper left", bbox_to_anchor=[0, 1], ncol=2, fancybox=True)
# plot 2
plt.subplot(122)
plt.plot(x_values, derivative_values,color="darkorange", label = "f '(x)")
plt.legend(loc="upper left");
plt.show()
In this section, we coded out our function for calculating and plotting the derivative. We started by seeing how we can represent different types of functions. Then we moved onto writing the output_at
function which evaluates a provided function at a value of x. We calculated delta_f
by subtracting the output at initial x value from the output at that initial x plus delta x. After calculating delta_f
, we moved onto our derivative_at
function, which simply divided delta_f
from delta_x
.
In the final section, we plotted out some of our findings. We introduced the tangent_line
function to get the slope for a function between an initial