Per calcolare un valore approssimato di pi greco utilizza
.
L’integrale definito viene approssimato con il metodo dei rettangoli (altezze di sinistra)
Con n = 100 si ottiene pi greco = 3.151576…
Web VPython 3.2
#-------------------------------------------------------- Problema
def f(x): return 1/(1+x**2)
a=0
b=1
#--------------------------------------------------------
N_MAX = 25
g1 = graph(fast = False, title = "Rettangoli con altezze di sinistra",
xtitle = "x", ytitle = "f(x)")
g2 = graph(fast = False, title = "Approssimazione di pi greco",
xtitle = "n", ytitle = "pi greco")
g_int_num = gvbars(graph = g1, color = color.magenta)
g_punti = gdots (graph = g1, color = color.blue)
g_PIGRECO = gcurve(graph = g2, color = color.green, width=5)
g_pigreco = gcurve(graph = g2, color = color.blue, markers = True)
#--------------------------------------------------------
def integra(n):
COPPIE1 = []
COPPIE2 = []
h=(b-a)/n
S=0
x=a
h2 = h/2
for i in range(n):
y = f(x)
S += y
COPPIE1.append([x , y])
COPPIE2.append([x+h2, y])
x += h
return h*S, COPPIE1, COPPIE2
def approssima(x):
return 4*x
#--------------------------------------------------------
for n in range(1, N_MAX+1):
rate(5)
int_def, DATA1, DATA2 = integra(n)
pigreco = approssima(int_def)
print("({:2d}) {:.6f} --> {:.6f}".format(n, int_def, pigreco))
g_int_num.delta = 1/n
g_int_num.data = DATA2
g_punti.data = DATA1
g_PIGRECO.plot(n, pi)
g_pigreco.plot(n, pigreco)