Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer - 16w

Data Science and Machine Learning - 16 wks

Search from all Lessons


LoginGet Started
← Back to Lessons

Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge

Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast
Edit on Github
Open in Colab

Calculus Derivatives with python

Introduction to Derivatives

In [12]:
import matplotlib.pyplot as plt
import numpy as np
import sympy as sy

Motivation: Slope of a Line

slope=Y2Y1X2X1 slope = \frac{Y2 - Y1}{X2 - X1}

Consider:

f(x)=x+1  and  g(x)=x2+1 f(x) = x + 1 \ \ and \ \ g(x) = x^2 + 1

In [14]:
def f(x): return x + 1
def g(x): return x**2 + 1
x = np.linspace(-2,2,100)
In [7]:
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()
Out[7]:
<matplotlib.legend.Legend at 0x7f17c94f31f0>
No description has been provided for this image
In [9]:
x2 = np.linspace(1,1.1)
In [15]:
plt.plot(x2,f(x2),label='f')
plt.plot(x2,g(x2),label='g')
plt.legend()
plt.title('Zooming in on x=[1,1.1]x = [1,1.1]\nNow they both look Linear!')
Out[15]:
Text(0.5, 1.0, 'Zooming in on x=[1,1.1]x = [1,1.1]\nNow they both look Linear!')
No description has been provided for this image
In [17]:
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 x=[1,1.001,100]x = [1,1.001,100]\nNow they both look Linear!')
Out[17]:
Text(0.5, 1.0, 'Zooming in on x=[1,1.001,100]x = [1,1.001,100]\nNow they both look Linear!')
No description has been provided for this image

Definition

f(x)=limh0f(x+h)f(x)h  f'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h} \

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.

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 ff in terms of some variable xx, we will be able to get the derivative of this expression with sy.diff(f, x). A simple example follows.

In [18]:
def f(x):
    return x**2 - 2*x + 1
In [19]:
def df(x):
    h = 0.000001
    return (f(x + h) - f(x))/h
In [20]:
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[20]:
Text(0.5, 1.0, 'Function and its Derivative')
No description has been provided for this image

A function a its derivative

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]:
Text(0.5, 1.0, 'Another function and its Derivative')
No description has been provided for this image

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]:
Text(0.5, 1.0, 'Just the Derivative')
No description has been provided for this image
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 aa can be thought of as the slope of the line through aa and some other point a+Δxa + \Delta x where Δx\Delta x is very small.

Again, we would have something like:

f(a)=f(a+Δx)f(a)Δxf'(a) = \frac{f(a + \Delta x) - f(a)}{\Delta x}

for some arbitrarily small value of Δx\Delta x. We can also understand the expression above as providing us a means of approximating values close to x=ax = a using the tangent line. If we rearrange terms, notice:

f(a+Δx)=f(a)Δx+f(a)f(a + \Delta x) = f'(a)\Delta x + f(a)

What this does, is tells us the slope of the line tangent to the graph at the point (a,f(a))(a, f(a)). Suppose we have the function f(x)=x2f(x) = x^2, and we want to know the slope of the tangent line at x=2x = 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 [13]:
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 [14]:
tan_plot(2)
No description has been provided for this image
In [15]:
def g(x):
    return x*(x+2)*(x - 3)

def dg(x):
    h = 0.000001
    return ((g(x+h)-g(x))/h)
In [16]:
x = np.linspace(-3, 4, 1000)
plt.plot(x, g(x))
plt.axhline(color = 'black')
plt.axvline(color = 'black')
Out[16]:
<matplotlib.lines.Line2D at 0x11dc14e80>
No description has been provided for this image
In [17]:
x = sy.Symbol('x')
df = sy.diff(g(x), x)
df = sy.simplify(df)
a, b = sy.solve(df, x)
In [18]:
a, b
Out[18]:
(1/3 + sqrt(19)/3, -sqrt(19)/3 + 1/3)
In [19]:
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[19]:
[<matplotlib.lines.Line2D at 0x11e006898>]
No description has been provided for this image
In [20]:
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 [21]:
tan_plot(a, b)
No description has been provided for this image