Mar-29-2019, 11:35 AM
I think that
But to change it you'll have to think about
For now...
Maybe you can use numpy.trapz.
h
should be a function of n
...But to change it you'll have to think about
trapezoidal(f,a,b,n)
and check if it's really returning what are you expecting.For now...
from scipy import integrate import matplotlib.pyplot as plt a=0 b=1 n=500 h=(b-a)/(n) def f(x): return x**2 def trapezoidal(f,a,b,n): return (h/2)*(f(a)+f(b))+h*( sum(f(a+h*i) for i in range(1, (n-1)))) print(trapezoidal(f,a,b,n)) # Why this? lambda x: f(x) c= integrate.quad(f,0,1) print(c) error_list=[abs(trapezoidal(f,a,b,n)-c[0]) for n in range (300,900)] print(error_list) plt.plot(error_list, [(b-a)/(n) for n in range(300, 900)]) plt.show()Like I said, you need to check the Math.
Maybe you can use numpy.trapz.