Skip to Content

QUAD

Overview

The QUAD function computes the definite integral of a real-valued function over a specified interval using adaptive quadrature. This is useful for numerically integrating functions where an analytical solution is difficult or impossible. The function to be integrated is provided as a table of x and y values (sampled points), and linear interpolation is used between points. The integration is performed using the scipy.integrate.quad method, which is based on the QUADPACK Fortran library and uses adaptive subdivision and extrapolation for high accuracy:

abf(x)dx\int_a^b f(x) dx

where f(x)f(x) is the function defined by the input table, and aa and bb are the integration limits. For more details, see the scipy.integrate.quad documentation.

This example function is provided as-is without any representation of accuracy.

Usage

To use the function in Excel:

=QUAD(function_table, a, b, [epsabs], [epsrel])
  • function_table (2D list, required): Table of two columns, where the first column is x values and the second is y values. Must have at least two rows, with x values strictly increasing.
  • a (float, required): Lower limit of integration.
  • b (float, required): Upper limit of integration.
  • epsabs (float, optional, default=1.49e-8): Absolute error tolerance.
  • epsrel (float, optional, default=1.49e-8): Relative error tolerance.

The function returns a single value (float): the estimated value of the definite integral, or an error message (string) if the input is invalid.

Examples

Example 1: Integrate f(x)=x2f(x) = x^2 from 0 to 4

Inputs:

function_tableabepsabsepsrel
00041.49e-81.49e-8
11
24
39
416

Excel formula:

=QUAD({0,0;1,1;2,4;3,9;4,16}, 0, 4)

Expected output:

Result
22.00

This means the integral of x2x^2 from 0 to 4 is approximately 22.00.

Example 2: Integrate f(x)=sin(x)f(x) = \sin(x) from 0 to π\pi

Inputs:

function_tableabepsabsepsrel
0003.14161.49e-81.49e-8
1.57081
3.14160

Excel formula:

=QUAD({0,0;1.5708,1;3.1416,0}, 0, 3.1416)

Expected output:

Result
1.571

This means the integral of sin(x)\sin(x) from 0 to π\pi is approximately 1.571.

Example 3: Integrate f(x)=exf(x) = e^{-x} from 0 to 5

Inputs:

function_tableabepsabsepsrel
01051.49e-81.49e-8
2.50.0821
50.0067

Excel formula:

=QUAD({0,1;2.5,0.0821;5,0.0067}, 0, 5)

Expected output:

Result
1.464

This means the integral of exe^{-x} from 0 to 5 is approximately 1.464.

Example 4: Integrate f(x)=2x+1f(x) = 2x + 1 from 1 to 3

Inputs:

function_tableabepsabsepsrel
13131.49e-81.49e-8
25
37

Excel formula:

=QUAD({1,3;2,5;3,7}, 1, 3)

Expected output:

Result
10.00

This means the integral of 2x+12x+1 from 1 to 3 is 10.00.

Python Code

from scipy.integrate import quad as scipy_quad def quad(function_table, a, b, epsabs=1.49e-8, epsrel=1.49e-8): """ Numerically integrate a function defined by a table of x, y values over [a, b] using adaptive quadrature. Args: function_table: 2D list, each row is [x, y]. a: Lower limit of integration. b: Upper limit of integration. epsabs: Absolute error tolerance (default: 1.49e-8). epsrel: Relative error tolerance (default: 1.49e-8). Returns: The estimated value of the definite integral (float), or an error message (str) if input is invalid. This example function is provided as-is without any representation of accuracy. """ # Validate function_table if not isinstance(function_table, list) or len(function_table) < 2: return "Invalid input: function_table must be a 2D list with at least two rows." try: xs = [float(row[0]) for row in function_table] ys = [float(row[1]) for row in function_table] except Exception: return "Invalid input: function_table must contain numeric values." if any(x2 <= x1 for x1, x2 in zip(xs, xs[1:])): return "Invalid input: x values in function_table must be strictly increasing." def f(x): # Linear interpolation if x <= xs[0]: return ys[0] if x >= xs[-1]: return ys[-1] for i in range(1, len(xs)): if x < xs[i]: x0, x1 = xs[i-1], xs[i] y0, y1 = ys[i-1], ys[i] return y0 + (y1 - y0) * (x - x0) / (x1 - x0) return ys[-1] try: result, _ = scipy_quad(f, float(a), float(b), epsabs=float(epsabs), epsrel=float(epsrel)) except Exception as e: return f"scipy.quad error: {e}" return round(result, 3)

Live Notebook

Edit this function in a live notebook.

Live Demo

Last updated on