Numerical Methods
nroot
A numerical root of an expression in a bracket where it changes sign (Brent's method). Returns a record: x, residual, the final bracket (lower, upper), iterations, evaluations and status. In place of the expression, a function written with fn works too, by name or in place: nroot(fn(x) x^2 - 2, x, 0, 2).
expr (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
lower (number) -- left end of the bracket
upper (number) -- right end of the bracket
rk4
Solves y' = f(t, y) with fixed-step classical Runge-Kutta (RK4). Returns a record with the final t and y, iterations, evaluations and status. The right-hand side may be a function written with fn: rk4(fn(t, y) y, t, y, 0, 1, 0.01, 100).
f (symbolic) -- the right-hand side f(t, y), e.g. y - t
t (string) -- the independent variable, unquoted
y (string) -- the dependent variable, unquoted
t0 (number) -- initial t
y0 (number) -- initial y
h (number) -- step size
steps (number) -- number of steps
bisection
A root by bisection in a bracket where the function changes sign. Slow but always keeps the root bracketed.
f (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
lower (number) -- left end
upper (number) -- right end
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
newton
A root by Newton's method from a starting guess. Needs the derivative; not guaranteed to converge.
f (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
df (symbolic) -- the derivative, e.g. 2*x
dvar (string) -- its variable
x0 (number) -- starting guess
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
secant
A root by the secant method from two starting guesses; not guaranteed to converge.
f (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
x0 (number) -- first guess
x1 (number) -- second guess
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
nderiv
A numerical derivative by central differences with Richardson extrapolation, with an error estimate.
f (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
at (number) -- the point
h (number) -- the step, e.g. 0.001
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
nderivForward
A numerical derivative by forward differences with Richardson extrapolation.
f (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
at (number) -- the point
h (number) -- the step
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
nintegrate
A numerical definite integral by adaptive Simpson's rule, with an error estimate: the calculator's integral where there is no antiderivative. Once it has converged its value is a number (N = nintegrate(...), then {{N:3dp}}, pi*N); N.status and N.error_estimate stay readable. Written in working as the integral it evaluates. A function written with fn works in place of the expression: nintegrate(fn(x) x^2, x, 0, 1).
f (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
lower (number) -- left end
upper (number) -- right end
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
quad
A second name for nintegrate, the name common among programming languages: the same code and the same answer. The ends must be finite: an integral to inf is refused, because this rule cannot tell a slow tail from a divergent one (use defint, which does the improper forms it can settle).
f (symbolic) -- the function, e.g. exp(-x^2)
var (string) -- its variable, unquoted
lower (number) -- left end
upper (number) -- right end
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
noClosedForm
Lists the integrals known to have no antiderivative in elementary functions, one line each: the form, the special function it needs, and a worked value (mpmath's) over a range. integrate says "no closed form" only for these, up to a constant factor and a linear inside; anything else it cannot do it calls "no method yet", since it may still have one. For a number over a range, use nintegrate or quad.
trapezoid
The composite trapezoid rule with n panels (refined once to estimate the error).
f (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
lower (number) --
upper (number) --
n (number) -- number of panels
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
simpson
The composite Simpson rule with n panels (n even; refined once to estimate the error).
f (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
lower (number) --
upper (number) --
n (number) -- number of panels, even
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
lerp
Linear interpolation: the value at x on the line through (x0, y0) and (x1, y1).
x0 (number) --
y0 (number) --
x1 (number) --
y1 (number) --
x (number) --
euler
Solves y' = f(t, y) with fixed-step Euler steps.
f (symbolic) -- the right-hand side f(t, y), e.g. y - t
t (string) -- the independent variable
y (string) -- the dependent variable
t0 (number) -- initial t
y0 (number) -- initial y
h (number) -- step size
steps (number) -- number of steps
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
rk45
Solves y' = f(t, y) up to a target t with adaptive Runge-Kutta-Fehlberg 4(5) steps.
f (symbolic) -- the right-hand side f(t, y), e.g. y - t
t (string) -- the independent variable
y (string) -- the dependent variable
t0 (number) -- initial t
y0 (number) -- initial y
t_end (number) -- the target t
h0 (number) -- first step size
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
nminimize
The minimum of a function on an interval by Brent's method (the function should have one minimum there).
f (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
lower (number) --
upper (number) --
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
goldenMin
The minimum of a function on an interval by golden-section search.
f (symbolic) -- the function, e.g. x^2 - 2
var (string) -- its variable, unquoted
lower (number) --
upper (number) --
tol (number) -- optional: tolerance for the result, e.g. 1e-6 (default 1e-12)
fitLine
The least-squares line y = intercept + slope*x through data points, with the residual sum of squares.
xs (array) -- x values
ys (array) -- y values
fitLinear
The least-squares line y = a x + b, as a graphing calculator's LinReg(ax+b) gives it, with r, the correlation coefficient. With its residuals (y minus the model at each x, in the order given), r_squared, and model, the fitted function of x to plot or evaluate.
xs (array) -- the x values
ys (array) -- the y values, one for each x
fitQuadratic
The least-squares quadratic y = a x^2 + b x + c (a calculator's QuadReg); at least 3 points. With its residuals (y minus the model at each x, in the order given), r_squared, and model, the fitted function of x to plot or evaluate.
xs (array) -- the x values
ys (array) -- the y values, one for each x
fitCubic
The least-squares cubic y = a x^3 + b x^2 + c x + d (CubicReg); at least 4 points. With its residuals (y minus the model at each x, in the order given), r_squared, and model, the fitted function of x to plot or evaluate.
xs (array) -- the x values
ys (array) -- the y values, one for each x
fitQuartic
The least-squares quartic y = a x^4 + b x^3 + c x^2 + d x + e (QuartReg); at least 5 points. With its residuals (y minus the model at each x, in the order given), r_squared, and model, the fitted function of x to plot or evaluate.
xs (array) -- the x values
ys (array) -- the y values, one for each x
fitExponential
The exponential model y = a b^x as a calculator's ExpReg finds it: the least-squares line through (x, ln y), so every y must be positive; r_squared is that line's. With its residuals (y minus the model at each x, in the order given), r_squared, and model, the fitted function of x to plot or evaluate.
xs (array) -- the x values
ys (array) -- the y values, one for each x
fitLogarithmic
The logarithmic model y = a + b ln x (LnReg), least squares in ln x; every x must be positive. With its residuals (y minus the model at each x, in the order given), r_squared, and model, the fitted function of x to plot or evaluate.
xs (array) -- the x values
ys (array) -- the y values, one for each x
fitSinusoidal
The sinusoidal model y = a sin(b x + c) + d (SinReg), least squares over every b from one cycle over twice the data's span to two samples a cycle; a > 0 and -pi < c <= pi. At least 4 points; more than a cycle of data fits best. With its residuals (y minus the model at each x, in the order given), r_squared, and model, the fitted function of x to plot or evaluate.
xs (array) -- the x values
ys (array) -- the y values, one for each x
fitMultiple
Multiple linear regression, y = c0 + c1 x1 + ... + ck xk by least squares: a record with coefficients (the intercept first, then one for each variable in the order given), standard_errors (NaN when the points only just fit), residual_sd, r_squared, adjusted_r_squared and residuals. Each variable is centred and scaled before the normal equations are solved in extended precision, so variables of very different sizes keep their digits. Needs more points than coefficients.
xs (number[][]) -- one list for each explanatory variable, all as long as ys
ys (number[]) -- the values to explain
newton2
Newton's method for two equations f1 = 0, f2 = 0 in two variables, from a starting point; the Jacobian is exact (symbolic).
equations (expression list) -- [f1, f2]
x (name) --
y (name) --
x0 (number) --
y0 (number) --
fitOneParameter
The parameter value in [lower, upper] that minimizes an objective (e.g. a sum of squared errors).
objective (expression) --
p (name) --
lower (number) --
upper (number) --
newtonInterp
The interpolating polynomial through the points (xs, ys), in Newton's divided-difference form, evaluated at x.
xs (array) --
ys (array) --
x (number) --
spline
The natural cubic spline through the points (xs, ys), evaluated at x.
xs (array) --
ys (array) --
x (number) --
fft
The discrete Fourier transform of a list whose length is a power of 2 (real or complex).
ifft
The inverse discrete Fourier transform.
globalMin
The least value of an expression of one to four variables on a box, found by a grid, a fixed pseudo-random sample and a polish of the best starts: a record with value, at (the point), samples and status. A numerical search, not a proof: a narrow dip between grid points can be missed. A point where the expression is undefined (a division by zero) is skipped.
f (symbolic) -- the expression, e.g. a^2 + b^2 - 2*a*b
vars (string) -- its variable, or its variables in square brackets, up to four: x or [a, b, c]
lower (number) -- the box's lower end, the same for every variable
upper (number) -- the box's upper end
globalMax
The greatest value of an expression of one to four variables on a box (as globalMin).
f (symbolic) -- the expression, e.g. a^2 + b^2 - 2*a*b
vars (string) -- its variable, or its variables in square brackets, up to four: x or [a, b, c]
lower (number) -- the box's lower end, the same for every variable
upper (number) -- the box's upper end
inequalityCheck
Tests that an expression is at least 0 everywhere on a box: a record with holds, minimum (the least value found), at (where), tight (the least value is 0, so the bound is attained there) and samples. When holds is false, at is a counterexample. Numerical evidence, not a proof. Write a claim A >= B as A - B.
f (symbolic) -- the expression, e.g. a^2 + b^2 - 2*a*b
vars (string) -- its variable, or its variables in square brackets, up to four: x or [a, b, c]
lower (number) -- the box's lower end, the same for every variable
upper (number) -- the box's upper end
identityCheck
Tests that an expression is 0 everywhere on a box (it checks that two sides of an identity agree): a record with holds (the greatest |value| found is at most 1e-9), worst, at and samples. Write a claim A = B as A - B. Numerical evidence, not a proof; large values can need a looser reading of worst.
f (symbolic) -- the expression, e.g. a^2 + b^2 - 2*a*b
vars (string) -- its variable, or its variables in square brackets, up to four: x or [a, b, c]
lower (number) -- the box's lower end, the same for every variable
upper (number) -- the box's upper end