1def simpson(f,a,b):
2 return (b-a)/6. * (f(a) + 4*f((a+b)/2.) + f(b))
3
4def simad(f,a,b,tol=1e-8,hmin=1e-10):
5
6 S = (a,a)
7
8 N = (a,b)
9
10 A = N
11
12 J = 0.
13
14 JA = 0.
15
16 nfe = 0
17
18 info = 0
19 while (S != (a,b)):
20 print(S,N,A)
21
22 h = A[1]-A[0]
23 while (h > hmin):
24 J0 = simpson(f,A[0],A[1])
25 JA = simpson(f,A[0],A[0]+h/2.)
26 JA += simpson(f,A[0]+h/2.,A[1])
27 nfe += 9
28
29 est = np.fabs(J0-JA)/10.
30
31 if (est < tol*h/(b-a)):
32 break
33 else:
34 A = (A[0],A[0]+h/2.)
35 h = h/2.
36 print("\t",S,N,A,h,est)
37 if (h < hmin):
38 print("Atencao! h < hmin !")
39 info = -1
40 break
41 J += JA
42 S = (a, A[1])
43 N = (A[1], b)
44 A = N
45 return J, nfe, info