-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewtoncotes.py
More file actions
35 lines (28 loc) · 802 Bytes
/
newtoncotes.py
File metadata and controls
35 lines (28 loc) · 802 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
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
import numpy as np
def f(x):
return 1/np.sqrt(x**2 + 1)
def rectangle(f,a,b,n):
h = (b-a)/(n-1)
xs = a + np.arange(n-1)*h
fs = f(xs)
return h*np.sum(fs)
def trapezoid(f,a,b,n):
h = (b-a)/(n-1)
xs = a + np.arange(n)*h
cs = np.ones(n); cs[0] = 0.5; cs[-1] = 0.5
contribs = cs*f(xs)
return h*np.sum(contribs)
def simpson(f,a,b,n):
h = (b-a)/(n-1)
xs = a + np.arange(n)*h
cs = 2*np.ones(n)
cs[1::2] = 4; cs[0] = 1; cs[-1] = 1
contribs = cs*f(xs)
return (h/3)*np.sum(contribs)
if __name__ == '__main__':
ans = np.log(1 + np.sqrt(2))
print(ans)
for integrator in (rectangle, trapezoid, simpson):
print(integrator(f, 0., 1., 51), end=" ")