Numeric Functions (39)

Numerical methods — integration, optimization, interpolation, ODE solvers

bezierCurve
Syntax
bezierCurve(controlPoints | t)
Type Signatures
Array, number, Array, Array
Parameters
NameTypeDescription
controlPointsArrayArray of [x,y] or [x,y,z] control points
tnumber|ArrayParameter value(s) in [0, 1]
Returns
Array — Point on the curve at parameter t,
Examples
bezierCurve([[0
0
See Also
interpolate cspline
bspline
Evaluate a B-spline curve at parameter t using De Boor\
Syntax
bspline(knots | controlPoints | degree
Type Signatures
Array, Array, number, number
Parameters
NameTypeDescription
knotsArrayKnot vector (non-decreasing, length = n+degree+2)
controlPointsArrayControl points (n+1 points, each a number or array)
degreenumberPolynomial degree (positive integer)
tnumberParameter value
Returns
number|Array — Point on the B-spline curve
Examples
bspline([0
0
0
1
1
chebyshevApprox
Compute a Chebyshev polynomial approximation of function f on [a, b] using n terms. Samples f at Chebyshev nodes and computes coefficients via the discrete cosine transform. Evaluates using Clenshaw\
Syntax
chebyshevApprox(f | a | b
Type Signatures
function, number, number, number
Parameters
NameTypeDescription
fFunctionFunction to approximate
anumberLeft endpoint of interval
bnumberRight endpoint of interval
nnumberNumber of Chebyshev terms (degree = n-1)
Returns
Object — Object with `coefficients` array and `evaluate(x)` method
Examples
chebyshevApprox(sin
0
pi
10)
cond
Compute the condition number of a matrix. p=2 (default) uses the ratio of largest to smallest singular value; p=1 and p=Infinity use the respective matrix norms.
Syntax
cond(A) | cond(A | p)
Type Signatures
Array, number
Parameters
NameTypeDescription
AArrayThe matrix (2D square array)
pnumberNorm type: 1, 2, or Infinity (default 2)
Returns
number — The condition number
Examples
cond([[1
0
See Also
rank nullspace det inv
cspline
Compute a natural cubic spline through a set of data points. Returns an object with an evaluate(t) method for smooth interpolation. Natural boundary conditions: second derivative is zero at both endpoints.
Syntax
cspline(x | y)
Type Signatures
Array, Array
Parameters
NameTypeDescription
xArrayArray of x-coordinates (strictly increasing)
yArrayArray of y-coordinates (same length as x)
Returns
Object — Object with `evaluate(t)` method and `coefficients` array
Examples
cspline([0
1
2
See Also
interpolate polyfit
curvefit
Fit a parametric model to data using the Levenberg-Marquardt algorithm. Returns an object with fitted params, residuals, and iteration count.
Syntax
curvefit(f | params0 | xdata
Type Signatures
function, Array, Array, Array, function, Array, Array, Array, Object
Parameters
NameTypeDescription
fFunctionModel function f(x, params) -> number
params0ArrayInitial parameter guess (1D array)
xdataArrayInput data (1D array)
ydataArrayOutput data (1D array)
optionsObjectOptions: tol (1e-8), maxIter (200), lambda (0.01)
Returns
Object — { params, residuals, iterations }
Examples
linear(x
p) = p[1
eventDetection
Solve an ODE system and detect events (zero crossings of event functions) using bisection.
Syntax
eventDetection(f | tSpan | y0
Parameters
NameTypeDescription
ffunctionODE function f(t, y) -> array
tSpanArray[t0, tf]
y0ArrayInitial state array
eventsArrayArray of event functions g_i(t, y) -> array of scalar values
optionsObjectOptional: {tol, maxStep, minStep, maxIter, bisectTol}
Returns
Object — {t, y, tEvents, yEvents, eventIndices}
expfit
Fit an exponential model y = a * exp(b * x) to data using linear regression on log-transformed data. All y values must be strictly positive. Returns an object with a, b, and predict(x).
Syntax
expfit(x | y)
Type Signatures
Array, Array
Parameters
NameTypeDescription
xArrayArray of x-coordinates
yArrayArray of y-coordinates (all must be > 0)
Returns
Object — Object with `a`, `b`, and `predict(x)` method
Examples
expfit([0
1
2
findRoot
Syntax
findRoot(f | x0) | findRoot(f
Type Signatures
function, number, function, number, number, function, number, Object, function, number, number, Object
Parameters
NameTypeDescription
fFunctionThe function to find the root of
x0numberInitial guess (Newton) or lower bound (Brent)
x1numberUpper bound (Brent only)
optionsObjectOptions: tol (1e-12), maxIter (100)
Returns
number — The root x where f(x) ≈ 0
Examples
h(x) = x^2 - 4
findRoot(h
1)
findRoot(cos
1
globalMinimize
Find the global minimum of a function using the differential evolution algorithm, a population-based stochastic method that does not require derivatives. bounds is an array of [lo, hi] pairs for each dimension. Returns an object with x, fval, and iterations.
Syntax
globalMinimize(f | bounds) | globalMinimize(f
Type Signatures
function, Array, function, Array, Object
Parameters
NameTypeDescription
fFunctionObjective function taking an array of numbers, returning a number
boundsArrayArray of [lo, hi] pairs for each dimension
optionsObjectOptions: populationSize (50), maxIter (1000), tol (1e-8), mutation (0.8), crossover (0.7)
Returns
Object — Result with properties: x (array), fval (number), iterations (number)
Examples
f(v) = (v[1
See Also
minimize maximize
gradient
Compute the numerical gradient of a scalar function at a given point using central differences. grad[i] = (f(x+h*e_i) - f(x-h*e_i)) / (2*h).
Syntax
gradient(f | x) | gradient(f
Type Signatures
function, Array, function, Array, number
Parameters
NameTypeDescription
fFunctionA function taking an array and returning a number
xArrayThe point at which to evaluate the gradient
hnumberStep size (default: 1e-7)
Returns
Array — The gradient vector at x
griddata
Interpolate scattered data at query points using the specified method. Methods: "nearest" (nearest-neighbor), "linear" (inverse distance weighting, IDW), "natural" (IDW with squared distances). Works in any dimension. Returns an array of interpolated values at each query point.
Syntax
griddata(points | values | xi)
Type Signatures
Array, Array, Array, Array, Array, Array, string
Parameters
NameTypeDescription
pointsArrayScattered data points (N x d array)
valuesArrayFunction values at data points (length N)
xiArrayQuery points (M x d array)
Returns
Array — Interpolated values at xi (length M)
Examples
griddata([[0
hessian
Compute the numerical Hessian matrix of a scalar function at a given point using second-order central differences. H[i][j] = (f(x+h*e_i+h*e_j) - f(x+h*e_i-h*e_j) - f(x-h*e_i+h*e_j) + f(x-h*e_i-h*e_j)) / (4*h^2).
Syntax
hessian(f | x) | hessian(f
Type Signatures
function, Array, function, Array, number
Parameters
NameTypeDescription
fFunctionA function taking an array and returning a number
xArrayThe point at which to evaluate the Hessian
hnumberStep size (default: 1e-4)
Returns
Array — The n×n Hessian matrix at x (2D array)
interpolate
Syntax
interpolate(points | x) | interpolate(points
Type Signatures
Array, number, Array, number, string
Parameters
NameTypeDescription
pointsArrayArray of [x, y] pairs, sorted by x
xnumberThe x value to interpolate at
methodstringInterpolation method: 'linear', 'lagrange', 'cubic'
Returns
number — The interpolated y value
Examples
interpolate([[0
0
See Also
curvefit nintegrate
leastSquares
Solve a nonlinear least squares problem using the Gauss-Newton method with backtracking line search and Levenberg-Marquardt regularization. Minimizes the sum of squared residuals returned by f(params, data). Returns an object with x, resnorm, iterations, and converged.
Syntax
leastSquares(f | x0 | data)
Type Signatures
function, Array, any, function, Array, any, Object
Parameters
NameTypeDescription
fFunctionResidual function: (params: Array, data: any) => Array of residuals
x0ArrayInitial parameter guess
dataanyData passed to f as second argument
optionsObjectOptions: tol (1e-8), maxIter (200), h (1e-7, finite diff step)
Returns
Object — Result with properties: x (array), resnorm (number), iterations (number), converged (boolean)
linprog
Solve a linear programming problem: minimize c\
Syntax
linprog(c | A | b)
Type Signatures
Array, Array, Array, Array, Array, Array, Array, Array
Parameters
NameTypeDescription
cArrayObjective coefficient vector (length n)
AArrayInequality constraint matrix (m x n)
bArrayInequality right-hand side vector (length m)
AeqArrayEquality constraint matrix (meq x n)
beqArrayEquality right-hand side vector (length meq)
Returns
Object — Result with properties: x (array), fval (number), status (string)
Examples
linprog([-1
-2
See Also
quadprog minimize
linsolve
Solve a linear system Ax = b using Gaussian elimination with partial pivoting. A is a 2D array, b is a 1D array.
Syntax
linsolve(A | b)
Type Signatures
Array, Array
Parameters
NameTypeDescription
AArrayCoefficient matrix (2D array, m x n)
bArrayRight-hand side vector (1D array, length m)
Returns
Array — Solution vector x
Examples
linsolve([[2
1
loess
Compute locally weighted scatterplot smoothing (LOESS/LOWESS). For each point x_i, fits a weighted linear regression using neighboring data points with a tricube weight function. The span parameter controls what fraction of the data is used for each local fit.
Syntax
loess(x | y) | loess(x
Type Signatures
Array, Array, Array, Array, number
Parameters
NameTypeDescription
xArrayArray of x-coordinates (must be sorted ascending)
yArrayArray of y-coordinates (same length as x)
spannumberFraction of data to use per local fit (default: 0.75)
Returns
Array — Array of smoothed y values
Examples
loess([1
2
3
4
5
logfit
Fit a logarithmic model y = a + b * ln(x) to data using linear regression with the substitution u = ln(x). All x values must be strictly positive. Returns an object with a, b, and predict(x).
Syntax
logfit(x | y)
Type Signatures
Array, Array
Parameters
NameTypeDescription
xArrayArray of x-coordinates (all must be > 0)
yArrayArray of y-coordinates
Returns
Object — Object with `a`, `b`, and `predict(x)` method
Examples
logfit([1
2
4
8
maximize
Find a local maximum of a function by negating it and applying the Nelder-Mead simplex minimization. Works for any dimension without requiring derivatives. Returns an object with x, fval, iterations, and converged.
Syntax
maximize(f | x0) | maximize(f
Type Signatures
function, Array, function, Array, Object
Parameters
NameTypeDescription
fFunctionObjective function taking an array of numbers, returning a number
x0ArrayInitial guess (array of numbers)
optionsObjectOptions: tol (1e-8), maxIter (1000)
Returns
Object — Result with properties: x (array), fval (number), iterations (number), converged (boolean)
Examples
g(v) = -(v[1
minimize
Find a local minimum of a function using the Nelder-Mead simplex method. Works for any dimension without requiring derivatives. Returns an object with x, fval, iterations, and converged.
Syntax
minimize(f | x0) | minimize(f
Type Signatures
function, Array, function, Array, Object
Parameters
NameTypeDescription
fFunctionObjective function taking an array of numbers, returning a number
x0ArrayInitial guess (array of numbers)
optionsObjectOptions: tol (1e-8), maxIter (1000)
Returns
Object — Result with properties: x (array), fval (number), iterations (number), converged (boolean)
Examples
f(v) = (v[1
nintegrate
Numerically integrate a function over an interval using adaptive Gauss-Kronrod quadrature.
Syntax
nintegrate(f | a | b)
Type Signatures
function, number, number, function, number, number, Object
Parameters
NameTypeDescription
fFunctionThe function to integrate
anumberLower bound
bnumberUpper bound
optionsObjectOptions: tol (default 1e-10), maxDepth (default 50)
Returns
number — The numerical integral
Examples
f(x) = x^2
nintegrate(f
0
1)
nintegrate(sin
nullspace
Compute the null space (kernel) of a matrix. Returns an array of basis vectors for the null space.
Syntax
nullspace(A)
Parameters
NameTypeDescription
AArrayThe matrix (2D array, m x n)
Returns
Array — Array of basis vectors for the null space
Examples
nullspace([[1
2
See Also
rank cond linsolve
odeAdaptiveStep
Solve an ODE system using Dormand-Prince RK45 with automatic adaptive step size control.
Syntax
odeAdaptiveStep(f | tSpan | y0)
Parameters
NameTypeDescription
ffunctionODE function f(t, y) -> array (or scalar)
tSpanArray[t0, tf]
y0Array|numberInitial values (array for system, number for scalar)
optionsObjectOptional: {tol, maxStep, minStep, maxIter}
Returns
Object — {t: number[], y: number[][]}
padeApproximant
Compute the [m/n] Pade approximant from Taylor series coefficients. Given coefficients c[0], c[1], ... of a Taylor series, returns the rational function P(x)/Q(x) where P has degree m and Q has degree n. Often converges faster than Taylor series, especially near poles. Returns an object with numerat
Syntax
padeApproximant(coeffs | m | n)
Type Signatures
Array, number, number
Parameters
NameTypeDescription
coeffsArrayTaylor series coefficients [c0, c1, c2, ...]
mnumberDegree of numerator polynomial
nnumberDegree of denominator polynomial
Returns
Object — Object with `numerator`, `denominator`, and `evaluate(x)` fields
Examples
padeApproximant([1
1
0.5
1/6
1/24
pchip
Compute a Piecewise Cubic Hermite Interpolating Polynomial (PCHIP). Unlike cubic splines, PCHIP preserves monotonicity: the interpolant will not oscillate between data points. Uses the Fritsch-Carlson method to compute shape-preserving slopes. Returns an object with an evaluate(t) method.
Syntax
pchip(x | y)
Type Signatures
Array, Array
Parameters
NameTypeDescription
xArrayStrictly increasing x-coordinates
yArrayy-coordinates (same length as x)
Returns
Object — Object with `evaluate(t)` method
Examples
pchip([0
1
2
3
See Also
cspline interpolate
polyfit
Fit a polynomial of given degree to data points using least-squares regression. Returns coefficients [c0, c1, ...] where p(x) = c0 + c1*x + c2*x^2 + ...
Syntax
polyfit(x | y | degree)
Type Signatures
Array, Array, number
Parameters
NameTypeDescription
xArrayArray of x-coordinates
yArrayArray of y-coordinates (same length as x)
degreenumberDegree of the polynomial to fit
Returns
Array — Coefficients [c0, c1, ..., cdegree]
Examples
polyfit([0
1
2
3
powerfit
Fit a power-law model y = a * x^b to data using linear regression on doubly log-transformed data: ln(y) = ln(a) + b*ln(x). All x and y values must be strictly positive. Returns an object with a, b, and predict(x).
Syntax
powerfit(x | y)
Type Signatures
Array, Array
Parameters
NameTypeDescription
xArrayArray of x-coordinates (all must be > 0)
yArrayArray of y-coordinates (all must be > 0)
Returns
Object — Object with `a`, `b`, and `predict(x)` method
Examples
powerfit([1
2
3
4
quadprog
Solve a quadratic programming problem: minimize (1/2)x\
Syntax
quadprog(H | f | A
Type Signatures
Array, Array, Array, Array, Array, Array, Array, Array, Array, Array
Parameters
NameTypeDescription
HArrayQuadratic cost matrix (n x n), symmetric positive definite
fArrayLinear cost vector (length n)
AArrayInequality constraint matrix (m x n)
bArrayInequality right-hand side (length m)
AeqArrayEquality constraint matrix (meq x n)
beqArrayEquality right-hand side (length meq)
Returns
Object — Result with properties: x (array), fval (number), status (string), iterations (number)
Examples
quadprog([[2
0
See Also
linprog minimize
rank
Compute the rank of a matrix (number of linearly independent rows or columns) using Gaussian elimination.
Syntax
rank(A) | rank(A | tol)
Type Signatures
Array, number
Parameters
NameTypeDescription
AArrayThe matrix (2D array)
tolnumberTolerance for zero detection (default: auto)
Returns
number — The rank of the matrix
Examples
rank([[1
0
rbfInterpolate
Radial Basis Function (RBF) interpolation for scattered data in any dimension. Fits weights by solving a linear system, then evaluates f(x) = sum_i w_i * phi(||x - x_i||). Available kernels: "gaussian", "multiquadric", "inverseMultiquadric", "thinPlateSpline". Returns an object with an evaluate(poin
Syntax
rbfInterpolate(points | values | kernel)
Type Signatures
Array, Array, string
Parameters
NameTypeDescription
pointsArrayArray of N coordinate arrays (each has same dimension d)
valuesArrayArray of N function values at the corresponding points
kernelstringKernel type: 'gaussian', 'multiquadric',
Returns
Object — Object with `evaluate(point)` method
Examples
rbfInterpolate([[0
residue
Compute the residues and poles of a rational function P(s)/Q(s) given polynomial coefficient arrays (highest power first). For each simple pole p_i: residue = P(p_i) / Q\
Syntax
residue(num | den)
Type Signatures
Array, Array
Parameters
NameTypeDescription
numArrayNumerator polynomial coefficients [highest power first]
denArrayDenominator polynomial coefficients [highest power first]
Returns
Object — { residues: Array, poles: Array }
Examples
residue([1
See Also
zpk2tf freqz polyfit
simpsons
Syntax
simpsons(f | a | b)
Type Signatures
function, number, number, function, number, number, number
Parameters
NameTypeDescription
fFunctionThe function to integrate
anumberLower bound
bnumberUpper bound
nnumberNumber of intervals (must be even, default 100)
Returns
number — The numerical integral
Examples
g(x) = x^2
simpsons(g
0
1)
simpsons(sin
See Also
nintegrate trapz
solveBVP
Solve a boundary value problem for an ODE system using the shooting method.
Syntax
solveBVP(f | bc | tSpan
Parameters
NameTypeDescription
ffunctionODE right-hand side f(t, y) -> array
bcfunctionBoundary condition residual bc(ya, yb) -> array (should be zero)
tSpanArray[t0, tf]
yGuessArrayInitial guess for the solution at t0
optionsObjectOptional: {tol, maxIter, shootingTol, nSteps}
Returns
Object — {t: number[], y: number[][]}
solveODE
Numerical Integration of Ordinary Differential Equations.
Syntax
solveODE(func | tspan | y0)
Parameters
NameTypeDescription
funcfunctionThe forcing function f(t,y)
tspanArray | MatrixThe time span
y0number | BigNumber | Unit | Array | MatrixThe initial value
optionsObjectOptional configuration options
Returns
Object — Return an object with t and y values as arrays
Examples
f(t
y) = y
tspan = [0
4
solveODESystem
Solve a system of coupled ordinary differential equations using RK4 or adaptive RK45.
Syntax
solveODESystem(f | tSpan | y0)
Parameters
NameTypeDescription
ffunctionThe function f(t, y) returning array dy/dt
tSpanArray[t0, tf] or array of output times
y0ArrayInitial state array
optionsObjectOptional: {method, tol, maxStep, minStep, maxIter}
Returns
Object — {t: number[], y: number[][]}
solvePDE
Solve a 1D PDE using the method of lines: discretize in space, then integrate in time with RK4.
Syntax
solvePDE(f | xSpan | tSpan
Parameters
NameTypeDescription
ffunctionPDE function f(t, u, x, dx) -> array of du/dt
xSpanArray[x0, xf]
tSpanArray[t0, tf]
u0function|ArrayInitial condition: function u0(x) or array of values
optionsObjectOptional: {nx, nt, nSteps}
Returns
Object — {x: number[], t: number[], u: number[][]}
stiffODESolver
Solve a stiff ODE system using Backward Differentiation Formula (BDF) order 1 or 2 with Newton iteration.
Syntax
stiffODESolver(f | tSpan | y0)
Parameters
NameTypeDescription
ffunctionODE function f(t, y) -> array
tSpanArray[t0, tf]
y0ArrayInitial state array
optionsObjectOptional: {order, tol, maxStep, minStep, maxIter, newtonMaxIter, h}
Returns
Object — {t: number[], y: number[][]}
trapz
Numerically integrate discrete data using the trapezoidal rule. y and x are arrays of the same length, or y is an array and dx is a scalar step size.
Syntax
trapz(y | x) | trapz(y
Type Signatures
Array, Array, Array, number
Parameters
NameTypeDescription
yArrayArray of function values
xArray|numberArray of x coordinates, or scalar step size dx
Returns
number — The numerical integral
Examples
trapz([0
1
4
9
See Also
nintegrate simpsons