forked from Zura1101/NUMERICAL-METHODS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpson1:8.py
More file actions
36 lines (24 loc) · 877 Bytes
/
Copy pathSimpson1:8.py
File metadata and controls
36 lines (24 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Simpson's rule
import math
import sympy
def func(x):
fx = 1 / (1 + x * x)
return float(fx)
# driver function
lower_limit = 0
upper_limit = 1
interval_limit = 6
# Function to perform calculations
def calculate(lower_limit, upper_limit, interval_limit):
interval_size = (float(upper_limit - lower_limit) / interval_limit)
sum = func(lower_limit) + func(upper_limit);
# Calculates value till integral limit
for i in range(1, interval_limit):
if (i % 3 == 0):
sum = sum + 2 * func(lower_limit + i * interval_size)
else:
sum = sum + 3 * func(lower_limit + i * interval_size)
return ((float(3 * interval_size) / 8) * sum)
integral_res = calculate(lower_limit, upper_limit, interval_limit)
# rounding the final answer to 6 decimal places
print(round(integral_res, 4))