In a laboratory class at Uni, one version of my code failed, while another passed, and I was hoping someone could explain why these are any different?
The version that failed was:
import scipy as sc
def trapez(f, a, b, n)
h = (b - a) / n
c = list(range(1, n))
return (h / 2) * (f(a) + f(b) + 2 * sum(f(a + i * h) for i in c))
def finderror(n)
def f(x):
return x ** 2
l = sc.integrate.quad(f, -1, 2)
return l[0] - trapez(f, -1, 2, n)
The version that passed was:
import scipy.integrate as sc
def trapez(f, a, b, n):
h = (b - a) / n
c = list(range(1, n))
return (h / 2) * (f(a) + f(b) + 2 * sum(f(a + i * h) for i in c))
def finderror(n):
def f(x):
return x ** 2
l = sc.quad(f, -1, 2)
return l[0] - trapez(f, -1, 2, n)
Both of these returned the exact same answers, hence my confusion at one 'failing'.
Thank you in advance for any advice! (I have already completed this, so this isn't me trying to get answers)