Skip to content

Commit 1ad1ed3

Browse files
committed
weddle's integration rule
1 parent 9a572de commit 1ad1ed3

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

maths/weddles_rule.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
from math import *
2+
3+
4+
def get_inputs():
5+
"""
6+
Get user input for the function, lower limit, and upper limit.
7+
8+
Returns:
9+
tuple: A tuple containing the function as a string, the lower limit (a), and the upper limit (b) as floats.
10+
11+
Example:
12+
>>> from unittest.mock import patch
13+
>>> inputs = ['1/(1+x**2)', 1.0, -1.0]
14+
>>> with patch('builtins.input', side_effect=inputs):
15+
... get_inputs()
16+
('1/(1+x**2)', 1.0, -1.0)
17+
"""
18+
func = input("Enter function with variable as x: ")
19+
a = float(input("Enter lower limit: "))
20+
b = float(input("Enter upper limit: "))
21+
return func, a, b
22+
23+
24+
def compute_table(func, a, b, acc):
25+
"""
26+
Compute the table of function values based on the limits and accuracy.
27+
28+
Args:
29+
func (str): The mathematical function with the variable 'x' as a string.
30+
a (float): The lower limit of the integral.
31+
b (float): The upper limit of the integral.
32+
acc (int): The number of subdivisions for accuracy.
33+
34+
Returns:
35+
tuple: A tuple containing the table of values and the step size (h).
36+
37+
Example:
38+
>>> compute_table('1/(1+x**2)', 1, -1, 1)
39+
([0.5, 0.4235294117647058, 0.36, 0.3076923076923077, 0.26470588235294124, 0.22929936305732482, 0.2], -0.3333333333333333)
40+
"""
41+
h = (b - a) / (acc * 6)
42+
table = [0 for _ in range(acc * 6 + 1)]
43+
for j in range(acc * 6 + 1):
44+
x = a + j / (acc * 6)
45+
table[j] = eval(func)
46+
return table, h
47+
48+
49+
def apply_weights(table):
50+
"""
51+
Apply Simpson's rule weights to the values in the table.
52+
53+
Args:
54+
table (list): A list of computed function values.
55+
56+
Returns:
57+
list: A list of weighted values.
58+
59+
Example:
60+
>>> apply_weights([0.0, 0.866, 1.0, 0.866, 0.0, -0.866, -1.0])
61+
[4.33, 1.0, 5.196, 0.0, -4.33]
62+
"""
63+
add = []
64+
for i in range(1, len(table) - 1):
65+
if i % 2 == 0 and i % 3 != 0:
66+
add.append(table[i])
67+
if i % 2 != 0 and i % 3 != 0:
68+
add.append(5 * table[i])
69+
elif i % 6 == 0:
70+
add.append(2 * table[i])
71+
elif i % 3 == 0 and i % 2 != 0:
72+
add.append(6 * table[i])
73+
return add
74+
75+
76+
def compute_solution(add, table, h):
77+
"""
78+
Compute the final solution using the weighted values and table.
79+
80+
Args:
81+
add (list): A list of weighted values from apply_weights.
82+
table (list): A list of function values.
83+
h (float): The step size (h) calculated from the limits and accuracy.
84+
85+
Returns:
86+
float: The final computed integral solution.
87+
88+
Example:
89+
>>> compute_solution([4.33, 6.0, 0.0, -4.33], [0.0, 0.866, 1.0, 0.866, 0.0, -0.866, -1.0], 0.5235983333333333)
90+
0.7853975
91+
"""
92+
return 0.3 * h * (sum(add) + table[0] + table[-1])
93+
94+
95+
if __name__ == "__main__":
96+
from doctest import testmod
97+
testmod()
98+
99+
func, a, b = get_inputs()
100+
acc = 1
101+
solution = None
102+
103+
while acc <= 100000:
104+
table, h = compute_table(func, a, b, acc)
105+
add = apply_weights(table)
106+
solution = compute_solution(add, table, h)
107+
acc *= 10
108+
109+
print(f'Solution: {solution}')

0 commit comments

Comments
 (0)