import numpy as np import matplotlib.pyplot as plt a = 0 b = 1 nodes = [a,b] def Adaptiv(f,a,b,tol): # beregn T1, T2 og E2: T1 = (b-a)*(f(a)+f(b))/2 T2 = (b-a)*(f(a)+2*f((a+b)/2)+f(b))/4 E2 = (T2-T1)/3 if np.abs(E2) <= tol: return(T2+E2) else: c = (a+b)/2 nodes.append(c) Q1 = Adaptiv(f,a,c,tol/2) Q2 = Adaptiv(f,c,b,tol/2) return(Q1+Q2) def f(x): return(np.sin(x)) def f2(x): return(1/(1+25*x**2)) def f3(x): return(np.sqrt(x)) tol = 1e-4 res = Adaptiv(f3,a,b,tol) nodes.sort() nodes = np.asarray(nodes) plt.plot(nodes,f3(nodes),'.') plt.plot(nodes,np.zeros(np.size(nodes)),'r.') plt.show()