Lesson 11 of 15

Differentiability

Differentiability

The derivative formalizes the idea of instantaneous rate of change.

Definition

A function (f) is differentiable at (c) if the following limit exists:

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

Key Facts

  • If (f) is differentiable at (c), then (f) is continuous at (c)
  • The converse is false: (f(x) = |x|) is continuous at 0 but not differentiable there

Numerical Differentiation

We can approximate the derivative using a small (h):

def derivative(f, x, h=1e-8):
    return (f(x + h) - f(x)) / h

A better approximation uses the central difference:

f(x)f(x+h)f(xh)2hf'(x) \approx \frac{f(x + h) - f(x - h)}{2h}

This is more accurate because it cancels the second-order error term.

Higher Derivatives

The second derivative can be approximated by:

f(x)f(x+h)2f(x)+f(xh)h2f''(x) \approx \frac{f(x + h) - 2f(x) + f(x - h)}{h^2}

Your Task

Implement:

  1. derivative(f, x, h) -- central difference approximation: ((f(x+h) - f(x-h)) / (2h))
  2. second_derivative(f, x, h) -- second derivative approximation: ((f(x+h) - 2f(x) + f(x-h)) / h^2)
  3. is_differentiable(f, x, h) -- checks if left and right derivatives are approximately equal (within 0.01 of each other)
Pyodide loading...
Loading...
Click "Run" to execute your code.