'''Programma per integrare e^x da 0 a 1''' import numpy as np import matplotlib.pyplot as plt def rettangoli_naif(h,f): inte=0.0 for i in range(f.shape[0]-1): inte+=f[i] inte*=h return inte def trapezi(h,f): inte=f[0]/2.0+f[-1]/2.0 for i in range(1,f.shape[0]-1): inte+=f[i] inte*=h return inte def main(): ris=np.exp(1.0)-1.0 ris_naif=[] ris_trap=[] mesh=[] for n in (10,100,1000,10000): h=1.0/n#larghezza intervalli x=np.zeros((n+1),dtype=np.float64) f=np.zeros((n+1),dtype=np.float64) for i in range(n+1): x[i]=h*i f[i]=np.exp(x[i]) ris_naif.append(rettangoli_naif(h,f)) ris_trap.append(trapezi(h,f)) mesh.append(n) for i in range(len(mesh)): print(str(ris_naif[i]-ris)+'\t'+str(ris_trap[i]-ris)) fig, ax = plt.subplots() plt.plot(mesh,ris_naif-ris) plt.plot(mesh,ris_trap-ris) plt.show(block=True) fig, ax = plt.subplots() ax.semilogx() ax.semilogy() plt.plot(mesh,abs(ris_naif-ris)) plt.plot(mesh,abs(ris_trap-ris)) plt.show(block=True) if __name__ == "__main__": main()