Skip to content

Numerical Integration

This module features quadrature rules and other numerical methods for computing definite integrals.

Gauss_legendre(a, b, n, func)

Calculating the integral of a function for given bounds via Gauß-Legendre

Parameters:

Name Type Description Default
a float

Lower bound of the integral

required
b float

Upper bound of the integral

required
n int

Number of subintervals

required
func Callable

Function that needs to be integrated

required

Returns:

Name Type Description
res float

Value of the integration

Source code in openum/numint.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def Gauss_legendre(a: float, b: float, n: int, func:Callable) -> float:
    """
    Calculating the integral of a function for given bounds via Gauß-Legendre

    Args:
        a (float): Lower bound of the integral
        b (float): Upper bound of the integral
        n (int): Number of subintervals
        func (Callable): Function that needs to be integrated

    Returns:
        res (float): Value of the integration
    """
    if n > 5 or n < 1:
        raise ValueError("Please use a number from 1 to 5")

    A = gauss_legendre_data[n]["weights"]
    x_v = gauss_legendre_data[n]["points"]
    x_transformed = [transform_G_L(r, b, a) for r in x_v]  # Transform nodes for use

    total = sum(A[i] * func(x_transformed[i]) for i in range(len(x_transformed)))
    res = 0.5 * (b - a) * total  # Multiply by the scaling factor
    return res

Newton_cotes(n, a, b, func)

Calculating the integral of a function for given bounds via Newton-Cotes

Parameters:

Name Type Description Default
a float

Lower bound of the integral

required
b float

Upper bound of the integral

required
n int

Number of subintervals

required
func Callable

Function that needs to be integrated

required

Returns:

Name Type Description
sum_integral float

Value of the integration

Source code in openum/numint.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def Newton_cotes(n: int, a: float, b: float, func:Callable) -> float:
    """
    Calculating the integral of a function for given bounds via Newton-Cotes

    Args:
        a (float): Lower bound of the integral
        b (float): Upper bound of the integral
        n (int): Number of subintervals
        func (Callable): Function that needs to be integrated

    Returns:
        sum_integral (float): Value of the integration
    """
    h, x_values = precalc(n, a, b)
    f_v = [func(x_i) for x_i in x_values]
    sum_integral = 0

    for i in range(0, n - 2, 2):  # Calculating the value for every odd step
        sum_integral += h / 3 * (f_v[i] + 4 * f_v[i + 1] + f_v[i + 2])

    return sum_integral

Romberg(n, a, b, func)

Calculating the integral of a function for given bounds via Romberg

Parameters:

Name Type Description Default
n int

Number of subintervals

required
a float

Lower bound of the integral

required
b float

Upper bound of the integral

required
func Callable

Function that needs to be integrated

required

Returns:

Name Type Description
value float

Value of the integration

Source code in openum/numint.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def Romberg(n: int, a: float, b: float, func:Callable) -> float:
    """
    Calculating the integral of a function for given bounds via Romberg

    Args:
        n (int): Number of subintervals
        a (float): Lower bound of the integral
        b (float): Upper bound of the integral
        func (Callable): Function that needs to be integrated

    Returns:
        value (float): Value of the integration
    """
    I_1 = Simpson_1_3(n, a, b, func)  # Calculate two integrals via Simpson
    I_2 = Simpson_1_3(2 * n, a, b, func)
    value = I_2 + (I_2 - I_1) / (2**4 - 1)  # Error correction
    return value

Simpson_1_3(n, a, b, func)

Calculating the integral of a function for given bounds via Simpson 1/3

Parameters:

Name Type Description Default
n int

Number of subintervals

required
a float

Lower bound of the integral

required
b float

Upper bound of the integral

required
func Callable

Function that needs to be integrated

required

Returns:

Name Type Description
result float

Value of the integration

Source code in openum/numint.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def Simpson_1_3(n: int, a: float, b: float, func:Callable) -> float:
    """
    Calculating the integral of a function for given bounds via Simpson 1/3

    Args:
        n (int): Number of subintervals
        a (float): Lower bound of the integral
        b (float): Upper bound of the integral
        func (Callable): Function that needs to be integrated

    Returns:
        result (float): Value of the integration
    """
    h, x_values = precalc(n, a, b)
    f_values = [func(x_i) for x_i in x_values]

    total = f_values[0] + f_values[-1]  # Sum of the values of the integral limits

    for i in range(1, len(x_values) - 1):  # Sum all other values based on the number of steps
        if i % 2 == 0:  # Multiply even-indexed values by 2
            total += 2 * f_values[i]
        else:  # Multiply odd-indexed values by 4
            total += 4 * f_values[i]

    result = h / 3 * total  # Get the result
    return result

Simpson_3_8(n, a, b, func)

Calculating the integral of a function for given bounds via Simpson's 3/8 rule

Parameters:

Name Type Description Default
n int

Number of subintervals

required
a float

Lower bound of the integral

required
b float

Upper bound of the integral

required
func Callable

Function that needs to be integrated

required

Returns:

Name Type Description
result float

Value of the integration

Source code in openum/numint.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
def Simpson_3_8(n: int, a: float, b: float, func:Callable) -> float:
    """
    Calculating the integral of a function for given bounds via Simpson's 3/8 rule

    Args:
        n (int): Number of subintervals
        a (float): Lower bound of the integral
        b (float): Upper bound of the integral
        func (Callable): Function that needs to be integrated

    Returns:
        result (float): Value of the integration
    """
    n1 = (n // 3) * 3  # Ensure n1 is a multiple of 3
    n2 = n - n1
    x = np.linspace(a, b, n + 1)
    y = func(x)

    h1 = (x[n1] - a) / n1 if n1 > 0 else 0
    h2 = (b - x[n1]) / n2 if n2 > 0 else 0

    m1 = sum(y[i] for i in range(1, n1, 3))
    m2 = sum(y[i] for i in range(2, n1, 3))
    m3 = sum(y[i] for i in range(3, n1, 3))

    I_Simpson = (3 * h1 / 8) * (y[0] + 3 * m1 + 3 * m2 + 2 * m3 + y[n1])
    I_Trapezoid = (h2 / 2) * (y[n1] + y[-1]) if n2 > 0 else 0

    return I_Simpson + I_Trapezoid

Trapezoidal(n, a, b, func)

Calculating the integral of a function for given bounds via Trapezoidal rule

Parameters:

Name Type Description Default
a float

Lower bound of the integral

required
b float

Upper bound of the integral

required
n int

Number of subintervals

required
func Callable

Function that needs to be integrated

required

Returns:

Name Type Description
I float

Value of the integration

Source code in openum/numint.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def Trapezoidal(n: int, a: float, b: float, func:Callable) -> float:
    """
    Calculating the integral of a function for given bounds via Trapezoidal rule

    Args:
        a (float): Lower bound of the integral
        b (float): Upper bound of the integral
        n (int): Number of subintervals
        func (Callable): Function that needs to be integrated

    Returns:
        I (float): Value of the integration
    """
    h, x_values = precalc(n, a, b)
    y = [func(x_i) for x_i in x_values]
    total = sum(y[1:-1])  # Middle values
    I = h / 2 * (y[0] + 2 * total + y[-1])  # Calculating the integral
    return I

precalc(n, a, b)

Calculating the stepsize and the x_values

Parameters:

Name Type Description Default
n int

Number of subintervals

required
a float

Lower bound of the integral

required
b float

Upper bound of the integral

required

Returns:

Name Type Description
h float

Stepsize

x_values list

x values

Source code in openum/numint.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def precalc(n: int, a: float, b: float)-> tuple[float, list]:
    """
    Calculating the stepsize and the x_values

    Args:
        n (int): Number of subintervals
        a (float): Lower bound of the integral
        b (float): Upper bound of the integral

    Returns:
        h (float): Stepsize
        x_values (list): x values 
    """
    n = int(n)
    h = (b - a) / (n - 1)  # Calculating values for integration
    x_values = np.linspace(a, b, n)
    return h, x_values