mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-30 14:13:44 +00:00
adding doctests to maths/trapezoidal_rule.py (#12193)
* adding doctests to trapezoidal_rule.py * adding algorithm delta-star transformation * updating DIRECTORY.md * delete file star_delta_transform.py * updating DIRECTORY.md * modified: ../DIRECTORY.md --------- Co-authored-by: MRJPEREZR <MRJPEREZR@users.noreply.github.com>
This commit is contained in:
parent
d496d5611f
commit
1909f2272f
|
@ -5,13 +5,25 @@ This method is the classical approach of suming 'Equally Spaced Abscissas'
|
||||||
|
|
||||||
method 1:
|
method 1:
|
||||||
"extended trapezoidal rule"
|
"extended trapezoidal rule"
|
||||||
|
int(f) = dx/2 * (f1 + 2f2 + ... + fn)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def method_1(boundary, steps):
|
def method_1(boundary, steps):
|
||||||
# "extended trapezoidal rule"
|
"""
|
||||||
# int(f) = dx/2 * (f1 + 2f2 + ... + fn)
|
Apply the extended trapezoidal rule to approximate the integral of function f(x)
|
||||||
|
over the interval defined by 'boundary' with the number of 'steps'.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
boundary (list of floats): A list containing the start and end values [a, b].
|
||||||
|
steps (int): The number of steps or subintervals.
|
||||||
|
Returns:
|
||||||
|
float: Approximation of the integral of f(x) over [a, b].
|
||||||
|
Examples:
|
||||||
|
>>> method_1([0, 1], 10)
|
||||||
|
0.3349999999999999
|
||||||
|
"""
|
||||||
h = (boundary[1] - boundary[0]) / steps
|
h = (boundary[1] - boundary[0]) / steps
|
||||||
a = boundary[0]
|
a = boundary[0]
|
||||||
b = boundary[1]
|
b = boundary[1]
|
||||||
|
@ -26,13 +38,40 @@ def method_1(boundary, steps):
|
||||||
|
|
||||||
|
|
||||||
def make_points(a, b, h):
|
def make_points(a, b, h):
|
||||||
|
"""
|
||||||
|
Generates points between 'a' and 'b' with step size 'h', excluding the end points.
|
||||||
|
Args:
|
||||||
|
a (float): Start value
|
||||||
|
b (float): End value
|
||||||
|
h (float): Step size
|
||||||
|
Examples:
|
||||||
|
>>> list(make_points(0, 10, 2.5))
|
||||||
|
[2.5, 5.0, 7.5]
|
||||||
|
|
||||||
|
>>> list(make_points(0, 10, 2))
|
||||||
|
[2, 4, 6, 8]
|
||||||
|
|
||||||
|
>>> list(make_points(1, 21, 5))
|
||||||
|
[6, 11, 16]
|
||||||
|
|
||||||
|
>>> list(make_points(1, 5, 2))
|
||||||
|
[3]
|
||||||
|
|
||||||
|
>>> list(make_points(1, 4, 3))
|
||||||
|
[]
|
||||||
|
"""
|
||||||
x = a + h
|
x = a + h
|
||||||
while x < (b - h):
|
while x <= (b - h):
|
||||||
yield x
|
yield x
|
||||||
x = x + h
|
x = x + h
|
||||||
|
|
||||||
|
|
||||||
def f(x): # enter your function here
|
def f(x): # enter your function here
|
||||||
|
"""
|
||||||
|
Example:
|
||||||
|
>>> f(2)
|
||||||
|
4
|
||||||
|
"""
|
||||||
y = (x - 0) * (x - 0)
|
y = (x - 0) * (x - 0)
|
||||||
return y
|
return y
|
||||||
|
|
||||||
|
@ -47,4 +86,7 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user