ApasScript.

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).

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).

bisection

A root by bisection in a bracket where the function changes sign. Slow but always keeps the root bracketed.

newton

A root by Newton's method from a starting guess. Needs the derivative; not guaranteed to converge.

secant

A root by the secant method from two starting guesses; not guaranteed to converge.

nderiv

A numerical derivative by central differences with Richardson extrapolation, with an error estimate.

nderivForward

A numerical derivative by forward differences with Richardson extrapolation.

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).

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).

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).

simpson

The composite Simpson rule with n panels (n even; refined once to estimate the error).

lerp

Linear interpolation: the value at x on the line through (x0, y0) and (x1, y1).

euler

Solves y' = f(t, y) with fixed-step Euler steps.

rk45

Solves y' = f(t, y) up to a target t with adaptive Runge-Kutta-Fehlberg 4(5) steps.

nminimize

The minimum of a function on an interval by Brent's method (the function should have one minimum there).

goldenMin

The minimum of a function on an interval by golden-section search.

fitLine

The least-squares line y = intercept + slope*x through data points, with the residual sum of squares.

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.

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.

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.

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.

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.

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.

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.

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.

newton2

Newton's method for two equations f1 = 0, f2 = 0 in two variables, from a starting point; the Jacobian is exact (symbolic).

fitOneParameter

The parameter value in [lower, upper] that minimizes an objective (e.g. a sum of squared errors).

newtonInterp

The interpolating polynomial through the points (xs, ys), in Newton's divided-difference form, evaluated at x.

spline

The natural cubic spline through the points (xs, ys), evaluated at x.

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.

globalMax

The greatest value of an expression of one to four variables on a box (as globalMin).

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.

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.