Understanding Derivatives
Learn how derivatives measure instantaneous rate of change and visualize tangent lines to understand the geometric interpretation of differentiation.
Problem statement
Given a function f(x) = x², find the derivative at a point x = a and visualize the tangent line at that point.
Example
Input: f(x) = x², a = 2
Output: f'(2) = 4
Tangent line: y = 4x − 4
Intuition
The derivative represents the instantaneous rate of change of a function. Geometrically, it's the slope of the tangent line to the curve at a specific point.
Key concept
While the average rate of change uses two points, the derivative captures the rate of change at a single instant by taking the limit as the interval approaches zero.For f(x) = x², the derivative is f'(x) = 2x. At x = 2, the slope is 4, meaning the function is increasing at a rate of 4 units vertically for every 1 unit horizontally.
Interactive visualization
Drag the point along the curve to see how the derivative (tangent line slope) changes.
Try it: Drag the red point along the curve to see how the tangent line slope changes. Notice that the derivative f'(x) = 2x gives the exact slope at each point.
Implementation
import numpy as np
import matplotlib.pyplot as plt
def derivative(f, x, h=1e-5):
"""
Calculate derivative using numerical differentiation
f'(x) ≈ (f(x + h) - f(x - h)) / (2h)
"""
return (f(x + h) - f(x - h)) / (2 * h)
def visualize_derivative(f, x_point):
# Create x values
x = np.linspace(x_point - 2, x_point + 2, 100)
y = f(x)
# Calculate derivative at point
slope = derivative(f, x_point)
# Tangent line: y = slope * (x - x_point) + f(x_point)
tangent_y = slope * (x - x_point) + f(x_point)
# Plot
plt.plot(x, y, label='f(x)')
plt.plot(x, tangent_y, label=f'Tangent (slope={slope:.2f})')
plt.scatter([x_point], [f(x_point)], color='red', zorder=5)
plt.legend()
plt.grid(True)
plt.show()
# Example: f(x) = x²
f = lambda x: x**2
visualize_derivative(f, x_point=2) # Derivative at x=2 is 4Computational complexity
Time complexity
O(1)
Analytical derivative: using calculus rules, the derivative is computed instantly.
Space complexity
O(1)
Constant space: only stores the result value.
Note
Numerical differentiation (as shown in the code) requires O(1) function evaluations but may have precision errors. Symbolic differentiation using calculus rules is exact.Related topics
Integration Basics
The inverse operation of differentiation
Optimization Problems
Finding maxima and minima using derivatives
Taylor Series
Coming soonApproximating functions using derivatives
Discussion
Questions, ideas or a different approach? Join the conversation.