Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding doctests in simpson_rule.py #10269

Merged
merged 23 commits into from
Oct 11, 2023
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5014fa5
Adding doctests in simpson_rule.py
AasheeshLikePanner Oct 11, 2023
4e562d6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2023
7c4e6c7
Update maths/simpson_rule.py
AasheeshLikePanner Oct 11, 2023
3412f56
Update maths/simpson_rule.py
AasheeshLikePanner Oct 11, 2023
2ac9c79
Adding doctests in simpson_rule.py
AasheeshLikePanner Oct 11, 2023
115b882
Merge remote-tracking branch 'origin/simpson_rule_doctests' into simp…
AasheeshLikePanner Oct 11, 2023
70ca156
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2023
a1a8aed
Adding doctests in simpson_rule.py
AasheeshLikePanner Oct 11, 2023
56c5017
Merge remote-tracking branch 'origin/simpson_rule_doctests' into simp…
AasheeshLikePanner Oct 11, 2023
af6d1a5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2023
22c7713
Adding doctests in simpson_rule.py
AasheeshLikePanner Oct 11, 2023
2996ee7
Merge remote-tracking branch 'origin/simpson_rule_doctests' into simp…
AasheeshLikePanner Oct 11, 2023
4ee0d8d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2023
5702d00
Adding doctests in simpson_rule.py
AasheeshLikePanner Oct 11, 2023
75775ad
Merge remote-tracking branch 'origin/simpson_rule_doctests' into simp…
AasheeshLikePanner Oct 11, 2023
7394365
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2023
8c70689
Adding doctests in simpson_rule.py
AasheeshLikePanner Oct 11, 2023
39fa849
Merge remote-tracking branch 'origin/simpson_rule_doctests' into simp…
AasheeshLikePanner Oct 11, 2023
ec03589
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 11, 2023
a9b57a8
Update simpson_rule.py
cclauss Oct 11, 2023
dc36faf
Adding doctests in simpson_rule.py
AasheeshLikePanner Oct 11, 2023
cffa0ee
Merge remote-tracking branch 'origin/simpson_rule_doctests' into simp…
AasheeshLikePanner Oct 11, 2023
47b85a3
Adding doctests in simpson_rule.py
AasheeshLikePanner Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions maths/simpson_rule.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
"""
Numerical integration or quadrature for a smooth function f with known values at x_i

This method is the classical approach of suming 'Equally Spaced Abscissas'
This method is the classical approach of summing 'Equally Spaced Abscissas'

method 2:
"Simpson Rule"

"""


def method_2(boundary, steps):
def method_2(boundary: list[int], steps: int) -> float:
# "Simpson Rule"
# int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn)
"""
Calculate the definite integral of a function using Simpson's Rule.
:param boundary: A list containing the lower and upper bounds of integration.
:param steps: The number of steps or resolution for the integration.
:return: The approximate integral value.

>>> round(method_2([0, 2, 4], 10), 10)
2.6666666667
>>> round(method_2([2, 0], 10), 10)
-0.2666666667
>>> round(method_2([-2, -1], 10), 10)
2.172
>>> round(method_2([0, 1], 10), 10)
0.3333333333
>>> round(method_2([0, 2], 10), 10)
2.6666666667
>>> round(method_2([0, 2], 100), 10)
2.5621226667
>>> round(method_2([0, 1], 1000), 10)
0.3320026653
>>> round(method_2([0, 2], 0), 10)
Traceback (most recent call last):
...
ZeroDivisionError: Number of steps must be greater than zero
>>> round(method_2([0, 2], -10), 10)
Traceback (most recent call last):
...
ZeroDivisionError: Number of steps must be greater than zero
"""
if steps <= 0:
raise ZeroDivisionError("Number of steps must be greater than zero")

h = (boundary[1] - boundary[0]) / steps
a = boundary[0]
b = boundary[1]
Expand Down Expand Up @@ -41,11 +73,14 @@ def f(x): # enter your function here
def main():
a = 0.0 # Lower bound of integration
b = 1.0 # Upper bound of integration
steps = 10.0 # define number of steps or resolution
boundary = [a, b] # define boundary of integration
steps = 10.0 # number of steps or resolution
boundary = [a, b] # boundary of integration
y = method_2(boundary, steps)
print(f"y = {y}")


if __name__ == "__main__":
import doctest

doctest.testmod()
main()