Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

math.calculus

numeric roots, derivatives and integrals of functions

Example results generated on 2026-10-08. Ones using now, today or randomness will differ when you run them: press Run on any example to run it in your browser, after editing it if you like.

Functions

functiondescription
root(f: fn, guess: num) / root(f: fn, lo: num, hi: num)an x where f(x) = 0: Newton’s method from a guess, or bisection between lo and hi where f changes sign
deriv(f: fn, x: num)f’(x) by central difference
integrate(f: fn, a: num|quantity, b: num|quantity)∫ f from a to b by Simpson’s rule (1000 steps); units carry through

root

root(f: fn, guess: num) / root(f: fn, lo: num, hi: num): an x where f(x) = 0: Newton’s method from a guess, or bisection between lo and hi where f changes sign

root(|x| x ** 2 - 2, 1)
# → 1.41421
root(|x| x ** 3 - x - 1, 1, 2)
# → 1.32472

See also: deriv

deriv

deriv(f: fn, x: num): f’(x) by central difference

deriv(|x| x ** 2, 3)
# → 6
deriv(sin, 0)
# → 1

See also: integrate, root

integrate

integrate(f: fn, a: num|quantity, b: num|quantity): ∫ f from a to b by Simpson’s rule (1000 steps); units carry through

integrate(|x| x ** 2, 0, 3)
# → 9
integrate(sin, 0, pi)
# → 2

See also: deriv

More examples

calculus

# √2 the hard way
root(|x| x ** 2 - 2, 1)
# → 1.41421
# where cos meets x
root(|x| cos(x) - x, 0, 1)
# → 0.739085
# slope of x³ at 2
deriv(|x| x ** 3, 2)
# → 12
# area under a bell curve
integrate(|x| exp(-x ** 2), -10, 10) ** 2
# → 3.14159
# distance from speed
integrate(|t| 9.8 m/s^2 * t, 0 s, 3 s)
# → 44.1 m