GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — NumberMethods08/integral_calculation.py at main · nmkmms/NumberMethods08 Expand file tree/ Copy pathintegral_calculation.pyMore file actions 70 lines (47 loc) · 1.51 KB / Copy pathintegral_calculation.pyFile metadata and controls70 lines (47 loc) · 1.51 KB 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 import numpy as np # ========= INPUT ======== # Limits A, B = 0, np.pi # Function F = np.sin # Maximum of absolute value of function in [A, B] M = 1 # Number of partitions N = 20 # ===== END OF INPUT ===== def main(): print(f'FUNCTION: {F.__name__}\n') # Calculate integrals step = (B - A) / N for method in (rectangle_method, trapezoid_method, simpson_method): total_sum = 0 for i in np.arange(A, B, step): ai, bi = i, i + step total_sum += method(ai, bi, F) print(f'{method.__name__}') print(f'\tValue: {total_sum:.7f}') print(f'\tTheoretical estimated error: ' f'{theoretical_error_estimation(M, step, A, B, method.__name__):.7f}') def rename(newname): def decorator(f): f.__name__ = newname return f return decorator @rename('Rectangle method') def rectangle_method(a, b, func): return abs(func((a + b) / 2) * (b - a)) @rename('Trapezoid method') def trapezoid_method(a, b, func): return abs((b-a) * (func(a) + func(b)) / 2) @rename('Simpson method') def simpson_method(a, b, func): return abs((b - a) * (func(a) + 4 * func((a + b) / 2) + func(b)) / 6) def theoretical_error_estimation(m, h, a, b, type_): if type_ == 'Rectangle method': return m * (h ** 2) * (b - a) / 24 elif type_ == 'Trapezoid method': return m * (h ** 2) * (b - a) / 12 elif type_ == 'Simpson method': return m * (h ** 2) * (b - a) / 2880 if __name__ == '__main__': main() |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |