import math
import scipy.integrate as integrate
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return math.sqrt(2)*math.sin(math.pi * x)
h = 0.1
def f_approx(x):
return ((-1/2)*f(x-2*h) + f(x-1*h) - f(x+1*h) + (1/2)*f(x+2*h))/(h**3)
def f_approx_4(x):
return ((1/8)*f(x-3*h) - f(x-2*h) + (13/8)*f(x-h) - (13/8)*f(x+h) + f(x + 2*h) - (1/8)*f(x + 3*h))/(h**3)
def f_exact(x):
return -math.sqrt(2)*(math.pi**3)*math.cos(math.pi*x)
def integrand(x):
return ((f_approx_4(x) - f_exact(x)) / f_exact(x)) ** 2
3.113442238266441e-07
integrate.quad(integrand, 0, 1)
(3.1134422382661596e-07, 5.551115123125783e-17)
X = np.linspace(0, 1, 100)
vfunc = np.vectorize(integrand)
plt.plot(X, vfunc(X))
plt.show()