A tangent line touches a curve at a single point only. For a function $$y = f(x)$$, its derivative at $$x$$ is the function $$f^{\prime}$$ defined by

$f^{\prime}(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}$

provided that the limit exists. If $$f^{\prime}(x)$$ exists, then we say that $$f$$ is differentiable at $$x$$.

The key concept here is that we’re taking the difference quotient between any two points of a function and reducing that difference $$f(x + h) - f(x)$$ until it approaches $$0$$. Because each difference quotient is a slope, the operation eventually leads us to taking the slope of an infinitesimally small difference such that we can consider it a single point. The slope at that point is the derivative of the function at that point.

When a function is not defined at a point, or is discontinuous at a point, then it is not differentiable at that point. We can use sympy to perform algebraic and numeric differentiation. Let’s take the function $$f(x) = x^2$$, and find $$f^{\prime}$$, $$f^{\prime}(-3)$$, and $$f^{\prime}(4)$$.

from sympy import *

x = Symbol("x")

fprime = diff(x**2, x)
fprime
## 2*x
fprime.evalf(subs = {x: -3})
## -6.00000000000000
fprime.evalf(subs = {x: 4})
## 8.00000000000000