Skip to content

Instantly share code, notes, and snippets.

@jsdir
Created May 30, 2018 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsdir/312233b838da69a3beb28a0c85b19842 to your computer and use it in GitHub Desktop.
Save jsdir/312233b838da69a3beb28a0c85b19842 to your computer and use it in GitHub Desktop.
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
integrand(1)
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment