Numerical Methods (labtoolbox.numerical)#

Numerical methods of LabToolbox.

Includes integration, root finding, and interpolation utilities.

labtoolbox.numerical.boole(f, a, b, n=None, varname=None, max_step=0.1, **kwargs)[source]#

Approximate the definite integral of a function using Boole’s Rule.

Parameters:
  • f (callable) – The function to integrate. Must accept the integration variable as a named argument.

  • a (float) – Lower limit of integration.

  • b (float) – Upper limit of integration.

  • n (int, optional) – Number of Boole segments. Must be equal or greater than 1. If not provided, an optimal value is estimated to ensure segment width ≤ max_step.

  • varname (str, optional) – Name of the integration variable as expected by f. If not provided and f is a lambda or function with one positional argument, it is inferred automatically.

  • max_step (float, optional) – Maximum width of a Boole segment. Only used if n is not provided. Default is 0.1.

  • **kwargs – Additional parameters passed to f.

Returns:

Approximation of the definite integral using Boole’s Rule.

Return type:

float

labtoolbox.numerical.lagrange(f, constraints, x0, tol=1e-06)[source]#

Solve a constrained optimization problem using Lagrange multipliers.

Parameters:
  • f (callable) – The objective function f. Must return a scalar (float).

  • constraints (list of callables) – List of constraint functions [g_1(x), …, g_m(x)], where each g_i(x) takes a 1D numpy array and returns a scalar (float). Each g_i(x) = 0 defines a constraint.

  • x0 (array_like) – Initial guess for the solution, a 1D array of length n (number of variables).

  • tol (float, optional) – Tolerance for the solver (used in scipy.optimize.fsolve). Defaults to 1e-6.

Returns:

  • x_opt: The optimal point (1D array of length n).

  • lambda_opt: The Lagrange multipliers (1D array of length m, where m is the number of constraints).

Return type:

tuple of (ndarray, ndarray)

Notes

The solver may not converge for poorly conditioned problems or bad initial guesses.

Examples

>>> from labtoolbox.numerical import lagrange
>>> import numpy as np
>>> # Minimize f(x, y) = x^2 + y^2 subject to x + y = 1
>>> f = lambda x: x[0]**2 + x[1]**2
>>> constraints = [lambda x: x[0] + x[1] - 1]
>>> x0 = np.array([0.5, 0.5])
>>> x_opt, lambda_opt = lagrange(f, constraints, x0)
>>> print(x_opt, lambda_opt)  # Expected: x_opt ≈ [0.5, 0.5], lambda_opt ≈ [1.0]
[0.5 0.5] [1.00000002]
labtoolbox.numerical.newton(f, x0, fprime=None, varname=None, tol=1e-10, maxiter=50, dx=1e-06, **kwargs)[source]#

Find the root of a scalar function using the Newton-Raphson method.

Parameters:
  • f (callable) – Function whose root is to be found. Must accept the variable of interest as a named argument.

  • x0 (float) – Initial guess.

  • fprime (callable, optional) – Derivative function. If None, numerical differentiation is used.

  • varname (str, optional) – Name of the variable with respect to which we take the root. Required if f has multiple arguments.

  • tol (float, optional) – Absolute tolerance for convergence. Default is 1e-10.

  • maxiter (int, optional) – Maximum number of iterations. Default is 50.

  • dx (float, optional) – Step size for numerical differentiation. Default is 1e-6.

  • **kwargs – Additional keyword arguments passed to f (and fprime if provided).

Returns:

Approximated root of the function.

Return type:

float

labtoolbox.numerical.romberg(f, a, b, varname=None, tol=1e-08, max_iter=10, **kwargs)[source]#

Perform numerical integration using Romberg’s method.

Parameters:
  • f (callable) – The function to integrate. Must accept the integration variable as a named argument.

  • a (float) – Lower limit of integration.

  • b (float) – Upper limit of integration.

  • varname (str, optional) – Name of the integration variable. If None, it’s inferred automatically (only if f has one arg).

  • tol (float, optional) – Desired absolute tolerance. Default is 1e-8.

  • max_iter (int, optional) – Maximum number of Romberg iterations. Default is 10.

  • **kwargs – Additional keyword arguments passed to f.

Returns:

Approximation of the integral.

Return type:

float