Algorithme de Brouncker
On construit plusieurs rectangles les uns au-dessus des autres en divisant en deux à chaque étape leur largeur.
Xmin = -0.2
Xmax = 2.2
Ymin = -0.4
Ymax = 1.5
GradX = 0.1
GradY = 0.1
traceG()
traceX()
traceY()
segment([1,0.05],[1,-0.05])
segment([2,0.05],[2,-0.05])
texte("1",[0.97,-0.25])
texte("2",[1.97,-0.25])
function f(x){
return 1/x
}
couleur = bleu
graphe(f,0,2.5)
curseur("k",1,1,10,1)
s = 0.5
H = [0,s]
couleur = noir
transparence = 0.2
peinture = vert
rectangle([1,0.5],1,0.5)
for(i=2;i<={k};i++){
for(j=1; j < Math.pow(2,i-1);j = j+2){
pas = 1/Math.pow(2,i-1)
h = f(1+j/Math.pow(2,i-1))
H.push(h)
H.sort()
k = 0
while( h - H[k] ){
k = k+1
}
hmin = H[k-1]
rectangle([1+(j-1)/Math.pow(2,i-1),h],pas,h-hmin)
s = s + pas*(h-hmin)
}
}
texte("Somme des aires = "+s,[0.2,1.1])
texte("k = "+{k},[0.2,1.0])
def f(x):
return 1.0/x
def brouncker(f,a,b,n):
s = min( f(a),f(b) )*(b-a)
H = [0,s]
for i in range(2,n+1):
for j in range(1,2**(i-1),2):
pas = 1.0*(b-a)/2**(i-1)
h = f(a+j*pas)
H.append(h)
H.sort(key = float)
k = 0
while h-H[k] > 0:
k = k+1
hmin = H[k-1]
s = s + pas*(h-hmin)
return s
print(brouncker(f,1,2,3))