Techno Blender
Digitally Yours.

Learning Math Through Code: Derivatives | by Harrison Hoffman | Mar, 2023

0 45


Gain a deeper understanding of derivatives with Python

Team Leibniz. Image by Author.

Mathematics is a notoriously difficult subject for many. Because of its cumulative and abstract nature, students can find it challenging to make connections and understand how math is applied. In my ongoing journey, I’ve found it extremely helpful to enhance my understanding of math concepts by implementing them in code.

Coding demands precision because computers can only execute a specific set of instructions. The need for exactness requires a logical and systematic approach to problem-solving, which can be incredibly beneficial for understanding the underlying concepts. Through coding, we can develop a more profound intuition for the idea being implemented. Moreover, coding allows us to experiment with, visualize and automate mathematical concepts in an interactive and hands-on way, which can bring theoretical concepts to life and enhance our learning experience.

In this article, we will try to gain a better understanding of derivatives by implementing the “forward difference quotient” approximation in python. While this is a simple implementation requiring little code, it gets to the heart of what derivatives represent.

Let’s begin by stating the definition of the derivative. Because there are plenty of freely available resources on derivatives, this explanation will not be comprehensive. The derivative of a function f(x) with respect to x is defined as:

The definition of a derivative in one dimension. Image by Author.

The derivative tells us, at a single point, the direction and rate at which a function is changing. By picking two points x and x + h, computing the slope of the function between the two points (i.e. (f(x+h) — f(x)) / h), and allowing h to get infinitely close to 0, we recover the instantaneous rate of change (the derivative) of the function at x.

The most abstract and perhaps difficult-to-understand component of the derivative is that h gets infinitely close to 0 without actually reaching 0. We can write a function in python that approximates this idea:

import numpy as np
from typing import Callable, Union

def derivative(f: Callable[[float], float], x: Union[float, np.ndarray], h: float = 0.001) -> Union[float, np.ndarray]:

"""
Approximate the derivative of a function f at point x using
the forward difference quotient defintion.

Parameters
----------
f : callable
A function that takes a float as input and returns a float.
x : float or ndarray
The point(s) at which to compute the derivative of f.
h : float, optional
The step size used in the finite difference approximation. Default is 0.001.

Returns
-------
float or ndarray
The derivative of f at point x, approximated using the forward
difference quotient.
"""

# If h gets too small, the computation becomes unstable
if h < 1e-5:

raise ValueError('h values less than 1e-5 are unstable. Consider increasing h')

return (f(x + h) - f(x)) / h

This function takes in a pure function of a single variable, and approximates the derivative at the point(s) specified in the x argument. The actual logic of this function resides in a single line of code, yet it approximates many derivatives within a tolerable error.

To see this in action, let’s approximate the derivative of the quadratic function. By the power rule (or by computing the limit of the difference quotient), we know that:

The derivative of the quadratic function. Image by Author.

As an example, the derivative of the function at x = 3 is 2*3 = 6. The following code approximates the derivative of the quadratic function at x = 3:

# Define the quadratic function
def f(x):

"""
The quadratic function f(x) = x^2
"""
return x**2

# Define the input(s) to compute derivatives for
x = 3

# Define the value of h used to approximate the derivative
h = 0.001

# Approximate the derivative
print(derivative(f, x, h))

# Output: 6.000999999999479

By setting h to be a small positive number close to 0, we extract an approximation to the derivative which is close to the true value. As h gets smaller (up to a certain tolerance), the approximation gets more accurate:

# Define a smaller h value to get a more accurate approximation
h = 1e-5

# Take the derivative
print(derivative(f, x, h))

# Output: 6.000009999951316

We can visualize this behavior for decreasing values of h:

As the value of h decreases, the derivative approximation gets more accurate (converging towards 6). Image by Author.

Another interesting example involves the trigonometric functions. From the definition of the derivative, we know the following:

The derivative of sin(x) is cos(x). Image by Author.

Using the derivative function, we can approximate this:

import numpy as np
import matplotlib.pyplot as plt

# Define the h value
h = 1e-5

# Define the domain
x = np.linspace(-10, 10, 1000)

# Approximate the derivative of sin(x) (should be close to cos(x))
f_prime = derivative(np.sin, x, h)

# Plot sin(x) vs the approximated derivative
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, np.sin(x), label='f(x) = sin(x)')
ax.plot(x, f_prime, label="f'(x) ~= cos(x)")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Approximated derivative of sin(x) (close to cos(x))')
ax.legend()
plt.show()

The approximated derivative of sin(x) resembles cos(x). Image by Author.

The derivative approximation implemented in this article is known as the “forward difference quotient” and is one of many ways to perform numerical differentiation. It’s important to note that this approximation is imperfect, as it tends to break down for small values of h. Moreover, in practice, we can compute exact derivatives of close-form functions, removing the need for approximations. The purpose of this article was to help the reader see how the math plays out through code and hopefully increase their intuition for derivatives. I would encourage the reader to test the code on different functions, explore other derivative approximations, and understand the respective strengths and weaknesses. Thanks for reading!

  1. Numerical Derivatives https://acme.byu.edu/0000017a-17ef-d8b9-adfe-77ef210e0000/vol1b-numericalderivatiation-2017-pdf


Gain a deeper understanding of derivatives with Python

Team Leibniz. Image by Author.

Mathematics is a notoriously difficult subject for many. Because of its cumulative and abstract nature, students can find it challenging to make connections and understand how math is applied. In my ongoing journey, I’ve found it extremely helpful to enhance my understanding of math concepts by implementing them in code.

Coding demands precision because computers can only execute a specific set of instructions. The need for exactness requires a logical and systematic approach to problem-solving, which can be incredibly beneficial for understanding the underlying concepts. Through coding, we can develop a more profound intuition for the idea being implemented. Moreover, coding allows us to experiment with, visualize and automate mathematical concepts in an interactive and hands-on way, which can bring theoretical concepts to life and enhance our learning experience.

In this article, we will try to gain a better understanding of derivatives by implementing the “forward difference quotient” approximation in python. While this is a simple implementation requiring little code, it gets to the heart of what derivatives represent.

Let’s begin by stating the definition of the derivative. Because there are plenty of freely available resources on derivatives, this explanation will not be comprehensive. The derivative of a function f(x) with respect to x is defined as:

The definition of a derivative in one dimension. Image by Author.

The derivative tells us, at a single point, the direction and rate at which a function is changing. By picking two points x and x + h, computing the slope of the function between the two points (i.e. (f(x+h) — f(x)) / h), and allowing h to get infinitely close to 0, we recover the instantaneous rate of change (the derivative) of the function at x.

The most abstract and perhaps difficult-to-understand component of the derivative is that h gets infinitely close to 0 without actually reaching 0. We can write a function in python that approximates this idea:

import numpy as np
from typing import Callable, Union

def derivative(f: Callable[[float], float], x: Union[float, np.ndarray], h: float = 0.001) -> Union[float, np.ndarray]:

"""
Approximate the derivative of a function f at point x using
the forward difference quotient defintion.

Parameters
----------
f : callable
A function that takes a float as input and returns a float.
x : float or ndarray
The point(s) at which to compute the derivative of f.
h : float, optional
The step size used in the finite difference approximation. Default is 0.001.

Returns
-------
float or ndarray
The derivative of f at point x, approximated using the forward
difference quotient.
"""

# If h gets too small, the computation becomes unstable
if h < 1e-5:

raise ValueError('h values less than 1e-5 are unstable. Consider increasing h')

return (f(x + h) - f(x)) / h

This function takes in a pure function of a single variable, and approximates the derivative at the point(s) specified in the x argument. The actual logic of this function resides in a single line of code, yet it approximates many derivatives within a tolerable error.

To see this in action, let’s approximate the derivative of the quadratic function. By the power rule (or by computing the limit of the difference quotient), we know that:

The derivative of the quadratic function. Image by Author.

As an example, the derivative of the function at x = 3 is 2*3 = 6. The following code approximates the derivative of the quadratic function at x = 3:

# Define the quadratic function
def f(x):

"""
The quadratic function f(x) = x^2
"""
return x**2

# Define the input(s) to compute derivatives for
x = 3

# Define the value of h used to approximate the derivative
h = 0.001

# Approximate the derivative
print(derivative(f, x, h))

# Output: 6.000999999999479

By setting h to be a small positive number close to 0, we extract an approximation to the derivative which is close to the true value. As h gets smaller (up to a certain tolerance), the approximation gets more accurate:

# Define a smaller h value to get a more accurate approximation
h = 1e-5

# Take the derivative
print(derivative(f, x, h))

# Output: 6.000009999951316

We can visualize this behavior for decreasing values of h:

As the value of h decreases, the derivative approximation gets more accurate (converging towards 6). Image by Author.

Another interesting example involves the trigonometric functions. From the definition of the derivative, we know the following:

The derivative of sin(x) is cos(x). Image by Author.

Using the derivative function, we can approximate this:

import numpy as np
import matplotlib.pyplot as plt

# Define the h value
h = 1e-5

# Define the domain
x = np.linspace(-10, 10, 1000)

# Approximate the derivative of sin(x) (should be close to cos(x))
f_prime = derivative(np.sin, x, h)

# Plot sin(x) vs the approximated derivative
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x, np.sin(x), label='f(x) = sin(x)')
ax.plot(x, f_prime, label="f'(x) ~= cos(x)")
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('Approximated derivative of sin(x) (close to cos(x))')
ax.legend()
plt.show()

The approximated derivative of sin(x) resembles cos(x). Image by Author.

The derivative approximation implemented in this article is known as the “forward difference quotient” and is one of many ways to perform numerical differentiation. It’s important to note that this approximation is imperfect, as it tends to break down for small values of h. Moreover, in practice, we can compute exact derivatives of close-form functions, removing the need for approximations. The purpose of this article was to help the reader see how the math plays out through code and hopefully increase their intuition for derivatives. I would encourage the reader to test the code on different functions, explore other derivative approximations, and understand the respective strengths and weaknesses. Thanks for reading!

  1. Numerical Derivatives https://acme.byu.edu/0000017a-17ef-d8b9-adfe-77ef210e0000/vol1b-numericalderivatiation-2017-pdf

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment