Skip to content

math.calculus

solve f(x) = 0, find slopes, limits and areas under curves, numerically

Solving equations, finding slopes, and measuring areas under curves, for any function of one number. The function is given as a lambda, like |x| x ** 2 - 2.

These work numerically, not by algebra: they try values until the answer is accurate to many digits, so their answers are floats that are very close but not always exact.

Tags: algebra

  • root an x where f(x) = 0
  • deriv the slope of f at x (its derivative), estimated from f just either side of x, or with n its nth derivative
  • minimize the x between lo and hi where f is smallest
  • maximize the x between lo and hi where f is largest
  • solve_system the point where every function in fs is 0 at once, found from a starting guess (Newton’s method)
  • limit what f(x) approaches as x approaches a, which can be inf or -inf
  • integrate the area under f between a and b (its integral), where a and b can be inf or -inf
root(f: fn, guess: num) -> float / root(f: fn, lo: num, hi: num) -> float

an x where f(x) = 0

Given a starting guess, finds the nearest one it can (Newton’s method). Given lo and hi where f is negative at one and positive at the other, always finds one in between (bisection)

1
root(|x| x ** 2 - 2, 1)1.41421
2
root(|x| x ** 3 - x - 1, 1, 2)1.32472
Example outputCtrl+Enter to run

See also deriv

deriv(f: fn, x: num, n?: int) -> float

the slope of f at x (its derivative), estimated from f just either side of x, or with n its nth derivative

Orders above 1 are less accurate, and go up to 6

1
deriv(|x| x ** 2, 3)6
2
deriv(sin, 0)1
3
deriv(|x| x ** 4, 1, 2)12
Example outputCtrl+Enter to run

See also integrate, root

minimize(f: fn, lo: num, hi: num) -> float

the x between lo and hi where f is smallest

Squeezes the range around the lowest point (golden-section search), so f should dip once between lo and hi. Accurate to about 8 digits

1
minimize(|x| (x - 2) ** 2 + 1, 0, 5)2
2
minimize(cos, 2, 5)3.14159
Example outputCtrl+Enter to run

See also maximize, root

maximize(f: fn, lo: num, hi: num) -> float

the x between lo and hi where f is largest

Same search as minimize, so f should have one peak between lo and hi

1
maximize(sin, 0, 3)1.5708
2
maximize(|x| x * (10 - x), 0, 10)5
Example outputCtrl+Enter to run

See also minimize, root

solve_system(fs: list, guess: list) -> list

the point where every function in fs is 0 at once, found from a starting guess (Newton’s method)

Each function takes one number per unknown and the guess is a list with one number each, like solve_system([|x, y| x + y - 3, |x, y| x - y - 1], [0, 0])

1
solve_system([|x, y| x + y - 3, |x, y| x - y - 1], [0, 0])[2, 1]
2
solve_system([|x, y| x ** 2 + y ** 2 - 4, |x, y| x - y], [1, 1])[1.41421, 1.41421]
Example outputCtrl+Enter to run

See also root

limit(f: fn, a: num, side: str = “both”) -> float

what f(x) approaches as x approaches a, which can be inf or -inf

Samples f closer and closer to a and extrapolates (Richardson). side is “both”, “left” (from below) or “right” (from above), and when the two sides differ, that is an error

1
limit(|x| sin(x) / x, 0)1
2
limit(|x| (1 + 1 / x) ** x, inf)2.71828
3
limit(|x| abs(x) / x, 0, "right")1
4
limit(|x| 1 / x ** 2, 0)∞
Example outputCtrl+Enter to run

See also deriv

integrate(f: fn, a: num|quantity, b: num|quantity) -> float|quantity

the area under f between a and b (its integral), where a and b can be inf or -inf

With plain numbers it splits the range finer where f bends most (adaptive Simpson’s rule), and with quantities it adds up 1000 even slices, so units carry through and speed over time gives distance

1
integrate(|x| x ** 2, 0, 3)9
2
integrate(sin, 0, pi)2
3
integrate(|x| exp(-x ** 2), -inf, inf)1.77245
Example outputCtrl+Enter to run

See also deriv

1
# √2 as the x where x² - 2 = 0
2
root(|x| x ** 2 - 2, 1)1.41421
3
# where cos(x) = x, searching between 0 and 1
4
root(|x| cos(x) - x, 0, 1)0.739085
5
# slope of x³ at 2
6
deriv(|x| x ** 3, 2)12
7
# sin(x)/x near 0, where it can't be computed directly
8
limit(|x| sin(x) / x, 0)1
9
# the area under a bell curve, squared, is pi
10
integrate(|x| exp(-x ** 2), -10, 10) ** 23.14159
11
# distance from speed: units carry through
12
integrate(|t| 9.8 m/s^2 * t, 0 s, 3 s)44.1 m
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.